An example cdktf project that uses Golang, to deploy containerized application on AWS Lambda 📦
For reference:
- CDK for Terraform official doc by HashiCorp
- Introduction to CDK for Terraform by HashiCorp
- Introducing the Cloud Development Kit for Terraform (Preview) by AWS
- CDK for Terraform (
cdktf
) v0.12.2 - Docker
- Go
We assume you already have Docker and Go installations in your laptop, so let's install the cdktf
command.
# https://learn.hashicorp.com/tutorials/terraform/cdktf-install
# See the official docs above to install cdktf command
# Here is an example for macOS with Homebrew
❯ brew install cdktf
❯ cdktf --version
0.12.2
Let's download dependencies and generate code.
# This may take several minutes to complete.
❯ cdktf get
⠧ downloading and generating modules and providers...
Generated go constructs in the output directory: generated
The generated code depends on jsii-runtime-go. If you haven't yet installed it, you can run go mod tidy to automatically install it.
# Install jsii to let Go code to interact with CDK
❯ go mod tidy
Because AWS Lambda only supports Amazon ECR private repositories today, you need to copy a sample container image from Amazon ECR Public (or bring your own Lambda-enabled container image) to your own Amazon ECR private repository.
# Pull public container image that supports AWS Lambda
❯ docker pull public.ecr.aws/toricls/go-hello-world:latest
# Make sure you're using expected AWS profile or AWS credentials
# Create your Amazon ECR private repository if you don't have yet
# We use Oregon region here but of course you can choose your preferred AWS region instead
❯ AWS_REGION=us-west-2
❯ aws ecr create-repository --repository-name go-hello-world
❯ ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
❯ docker tag public.ecr.aws/toricls/go-hello-world:latest "${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/go-hello-world:latest"
❯ docker push "${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/go-hello-world:latest"
The cdktf code in this repository requires two environment variable to deploy - TF_VAR_AWS_REGION
and TF_VAR_CONTAINER_IMG_ARCH
.
TF_VAR_AWS_REGION
- Choose AWS region where you used in the previous steps for the container imageTF_VAR_CONTAINER_IMG_ARCH
- This value depends on your current environment in general, so just choose the architecture of your container image you pushed in the previous steps. If you're not familiar with, then choose "arm64" if you're using Apple Sillicon (e.g. M1) Mac, and choose "x86_64" instead if you're on Intel Mac. This should work in most cases.
Note that cdktf
command here requires AWS profile or AWS credentials to create resources in your AWS account.
❯ TF_VAR_AWS_REGION="us-west-2" TF_VAR_CONTAINER_IMG_ARCH="arm64" cdktf deploy --auto-approve
...
❯ cdktf destroy --auto-approve
...
cdktf
generates and stores the Terraform template under auto-generated cdktf.out
directory.
❯ cat ./cdktf.out/stacks/cdktf-go-example/cdk.tf.json
If you want to see the template without deploying actual resources, then use cdktf synth
command as:
❯ TF_VAR_AWS_REGION="us-west-2" TF_VAR_CONTAINER_IMG_ARCH="arm64" cdktf synth
Generated Terraform code for the stacks: cdktf-go-example
❯ cat ./cdktf.out/stacks/cdktf-go-example/cdk.tf.json
- Fork (https://github.com/toricls/cdktf-golang-aws-lambda-container/fork)
- Create a feature branch
- Commit your changes
- Rebase your local changes against the master branch
- Create a new Pull Request