Terraform
Terraform is one of the most popular infrastructure as code (IaC) tools available today. It enables the automation of infrastructure across any cloud. Terraform allows you to build any kind of infrastructure, not just serverless. For this example, let's focus on deploying a Lambda function.
Deploying .NET Lambda functions using Terraform adds an additional complexity. Your .NET function code needs to be compiled before Terraform can deploy your resources. This is less of an issue for interpreted languages like NodeJS and Python as no compilation is required.
However, the first thing you'll need to do is install Terraform..
Once installed, create a new directory:
Then create a file called main.tf
.
Copy in the below code. There's a lot, don't worry I'll walk through it step by step:
Click Here for Code Sample
Let's also create a new Lambda function to use as an example.
mkdir Functionscd Functionsdotnet new lambda.EmptyFunction -n Dotnet.CDK.Lambda
Terraform Provider
The opening section of the file configures the AWS provider for Terraform. More documentation on this is found in the Terraform documentation.
S3 Bucket for Code Storage
When packaging Lambda applications as ZIP files an S3 bucket is required to store the code. This code creates a new S3 bucket using a random identifier. You can update the bucket
property if you wish to use a different bucket name.
Lambda ZIP Package
Terraform provides a data source for generating ZIP files. The publish directory of the Lambda function is specified as the source directory and a ZIP file is generated. This ZIP file is then uploaded to S3 using the aws_s3_object
resource.
CloudWatch
Additionally, a Amazon CloudWatch log group is defined to store logs generated by the Lambda function.
IAM
An IAM role and policy allow Lambda execution is then created to provide Lambda with the AWSLambdaBasicExecutionRole
required to run.
Lambda Function Resource
A Lambda function is defined using the aws_lambda_function
resource. Notice that the s3_bucket
, s3_key
and source_code_hash
properties are using values from the S3 and asset objects defined earlier.
Output Name
And finally the name of the Lambda function is output to the console.
Terraform Provider
The opening section of the file configures the AWS provider for Terraform. More documentation on this is found in the Terraform documentation.
S3 Bucket for Code Storage
When packaging Lambda applications as ZIP files an S3 bucket is required to store the code. This code creates a new S3 bucket using a random identifier. You can update the bucket
property if you wish to use a different bucket name.
Lambda ZIP Package
Terraform provides a data source for generating ZIP files. The publish directory of the Lambda function is specified as the source directory and a ZIP file is generated. This ZIP file is then uploaded to S3 using the aws_s3_object
resource.
CloudWatch
Additionally, a Amazon CloudWatch log group is defined to store logs generated by the Lambda function.
IAM
An IAM role and policy allow Lambda execution is then created to provide Lambda with the AWSLambdaBasicExecutionRole
required to run.
Lambda Function Resource
A Lambda function is defined using the aws_lambda_function
resource. Notice that the s3_bucket
, s3_key
and source_code_hash
properties are using values from the S3 and asset objects defined earlier.
Output Name
And finally the name of the Lambda function is output to the console.
Once defined, run the below commands from a terminal window in the same folder as the main.tf
file.
Once deployed, invoke the Lambda function in AWS using the below command replacing the OUTPUT_FUNCTION_NAME
with the value output to your terminal:
dotnet lambda invoke-function -n OUTPUT_FUNCTION_NAME -p hello