Deploy Lambda functions with TypeScript and CDK for Terraform
You can use the Cloud Development Kit for Terraform (CDKTF) to define advanced deployment configurations in a familiar programming language such as TypeScript, Python, or Go.
CDKTF stacks let you manage multiple Terraform configurations in the same CDKTF application. They can save you from writing repetitive CDKTF code, while allowing you to independently manage the resources in each stack. You can also use the outputs of one stack as inputs for another.
Each stack generates its own Terraform workspace so that you can manage each stack independently. You can use stacks to connect multiple deployments across your application, simplifying your infrastructure workflow. In CDKTF v0.4+, asset constructs let you manage assets for resources that need them, such as template_file
, S3 bucket objects, or Lambda function archive files.
In this tutorial, you will deploy a CDKTF application made up of two stacks, each containing a simple AWS Lambda function written in TypeScript. In the process, you will use TerraformAsset
to package your Lambda deployment artifact and define stacks to manage the Lambda deployments independently.
Prerequisites
The tutorial assumes that you are familiar with the CDK for Terraform workflow. If you are new to CDK for Terraform itself, refer first to the Get Started with CDK for Terraform tutorials.
For this tutorial, you will need:
- Terraform v1.2+
- CDK for Terraform v0.15+
- an AWS account
- AWS Credentials configured for use with Terraform
Terraform and CDKTF will use credentials set in your environment or through other means as described in the Terraform documentation.
Note
Some of the infrastructure in this tutorial does not qualify for the AWS free tier. Destroy the infrastructure at the end of the guide to avoid unnecessary charges. We are not responsible for any charges that you incur.
Explore CDKTF application
In your terminal, clone the sample repository.
Navigate to the cloned repository.
This directory contains the three subdirectories.
- The
lambda-hello-world
directory hosts a Lambda handler written in TypeScript. Every Lamdba function must have a handler. When users invoke the Lambda function, it runs the handler code. This function returns "Hello world!". - The
lambda-hello-name
directory also hosts a Lambda handler written in TypeScript. This function returns "Hello NAME!", whereNAME
is the value for thename
query parameter. IfNAME
is undefined, the handler will return "Hello there!". - The
cdktf
directory hosts the CDKTF application written in TypeScript. You will use this directory to deploy thelambda-hello-world
andlambda-hello-name
Lambda functions.
Notice that both Lambda functions contain pre-compiled dist
directories, so you do not have to compile them in this tutorial. In addition, notice that the directories containing the Lambda handlers are not in the cdktf
directory. This is best practice — you should not have application code in your infrastructure directory.
Navigate to the cdktf
directory.
Open main.ts
, which contains the main CDKTF application. This application defines the LambdaStack
, a CDKTF stack you will use to deploy the lambda-hello-world
and lambda-hello-name
functions.
This file uses the preconfigured AWS provider (@cdktf/provider-aws
). Importing
the library allows you to use your code editor's autocomplete functionality to
help you write the CDK application code.
In the next sections, you will review the LambdaStack
code and use it to
deploy two different Lambda functions.
Examine the code
In this example, the LambdaStack
groups all the resources necessary to deploy
a Lambda function.
First the code creates a random value to ensure that the resource names for your Lambda function and IAM role are unique.
The example code configures the AWS provider to deploy your functions to the
us-west-2
region.
The TerraformAsset
construct
helps you manage assets and expose them to your CDK resources. This code uses
the TerraformAsset
to package the compiled handler code into an archive and
assigns it to a variable named asset
.
Tip
This file imports TerraformAsset
and AssetType
from the cdktf
library.
Next the code creates an S3 bucket to host the archive containing your Lambda function and uploads it to the bucket.
Next the example code creates an IAM role for your Lambda function and the
function itself. Notice that the IAM role references the lambdaRolePolicy
object defined at the beginning of the file. The stack also grants the Lambda
role access to write to CloudWatch.
Then the code creates an API gateway that targets your Lambda function. This
will make your function accessible via an HTTPS endpoint. The LambdaPermission
gives your API gateway endpoint permission to invoke your Lambda function.
Inspect function definitions
Toward the bottom of the main.ts
file, you will find definitions for the lambda-hello-world
and lambda-hello-name
functions using LambdaStack
.
Notice how each definition maps to the respective function's properties: the lambda-hello-world
's path
points to ../lambda-hello-world/dist
and its stageName
points to hello-world
. Once you have created the LambdaStack
class, you can use it to deploy as many similarly configured Lambda functions as you want.
View application stacks
Install the dependencies for the CDKTF application.
When you create a new CDKTF application from scratch, the cdktf init
command
will install these dependencies. The application from the sample repository is
already initialized, so you can use npm install
to install the application's
dependencies.
Now, run cdktf provider
to install the
random
and
aws
providers. This configuration uses the random
provider to ensure the IAM role
name is unique.
List all the stacks defined in your CDKTF application. Notice these map to the new LambdaStack
definitions declared in main.ts
.
Deploy Hello World function
Deploy the lambda-hello-world
stack. Remember to confirm the deploy by
choosing Approve
.
Open the url
output in your web browser to confirm the API gateway returns "Hello world!".
Deploy Hello Name function
Deploy the lambda-hello-name
stack. Remember to confirm the deploy with Approve
.
Open the url
output in your web browser to confirm the API gateway returns "Hello there!".
Append ?name=Terry
to the URL. The API gateway will respond with "Hello Terry!".
Inspect synthesized stacks
CDKTF stacks have their own configuration and state files.
In your cdktf
directory, find the cdktf.out
directory. CDKTF generates this directory when it synthesizes the Terraform configuration from the application code when you run cdktf synth
or cdktf deploy
.
Notice how there are two directories in cdktf.out/stacks
. Each directory corresponds with the stack defined in the application code and contains the generated Terraform configuration, plan file, and assets.
In addition, you will find terraform.lambda-hello-name.tfstate
and
terraform.lambda-hello-name.tfstate
in your cdktf
directory. The cdktf
deploy STACK_NAME
generates a state file named terraform.STACK_NAME.tfstate
where STACK_NAME
is the stack name. Like any Terraform state file, you should
not commit these files into source control.
Clean up resources
Destroy the infrastructure you created in this tutorial.
First, destroy the lambda-hello-world
stack. Remember to confirm the destroy with Approve
.
Now, destroy the lambda-hello-name
stack. Remember to confirm the destroy with Approve
.
Next steps
In this tutorial, you deployed and destroyed two different Lambda functions using CDKTF. In the process, you learned how to use TerraformAsset
to package your Lambda deployment artifact and stacks to independently manage the Lambda deployments.
You can also connect multiple stacks together to define multiple deployments in a single CDKTF application.
For more information on topics covered in this tutorial, check out the following resources.
- Inspect the code for a fully integrated approach in
fully-integrated
branch. This approach usesyarn
based monorepo setup and introduces aNodejsFunction
Construct which bundles the code transparently viaesbuild
. Visit this PR includes additional context and instructions. - Read more about constructs in the CDKTF Constructs documentation.
- Read more about assets in the CDKTF Assets documentation.
- Read more about CDKTF releases.