AWS CDK
Overview
Section titled “Overview”The AWS Cloud Development Kit (CDK) is an Infrastructure-as-Code (IaC) tool using general-purpose programming languages such as TypeScript/JavaScript, Python, Java, and .NET to programmatically define your cloud architecture on AWS.
AWS CDK CLI for LocalStack
Section titled “AWS CDK CLI for LocalStack”lstk cdk proxies the AWS CDK library against local APIs provided by LocalStack. It requires the AWS CDK CLI version 2.177.0 or newer on your PATH.
Installation
Section titled “Installation”Install lstk by following the lstk installation instructions.
You’ll also need the AWS CDK CLI installed separately:
# Install globallynpm install -g aws-cdk
# Verify it installed correctlycdk --versionlstk cdk can be used as a drop-in replacement of where you would otherwise use cdk when targeting the AWS Cloud.
lstk cdk --helpConfiguration
Section titled “Configuration”lstk cdk’s only lstk-specific flag (before the CDK action) is --region <region> (default us-east-1); CDK always targets the default LocalStack account 000000000000, so there is no --account flag.
The following environment variables can be configured:
AWS_ENDPOINT_URL: The endpoint URL (i.e., protocol, host, and port) to connect to LocalStack (default:http://localhost.localstack.cloud:4566)AWS_ENDPOINT_URL_S3: The S3-specific endpoint URL, required alongsideAWS_ENDPOINT_URLand must include.s3.to correctly identify S3 API callsLSTK_CDK_CMD: Binary to invoke (defaultcdk)AWS_REGION: Fallback for--region
Example
Section titled “Example”Make sure that LocalStack is installed and successfully started with the required services before running the example
curl http://localhost.localstack.cloud:4566/_localstack/healthThe CDK command line ships with a sample app generator to run a quick test for getting started.
# create sample appmkdir /tmp/test; cd /tmp/testlstk cdk init sample-app --language=javascript
# bootstrap localstack environmentlstk cdk bootstrap
# deploy the sample applstk cdk deploy> Do you wish to deploy these changes (y/n)? yOnce the deployment is done, you can inspect the created resources via the lstk aws command line
lstk aws sns list-topics { "Topics": [ { "TopicArn": "arn:aws:sns:us-east-1:000000000000:TestStack-TestTopic339EC197-79F43WWCCS4Z" } ]}Current Limitations
Section titled “Current Limitations”Updating CDK stacks
Section titled “Updating CDK stacks”Updating CDK stacks may result in deployment failures and inconsistent state within LocalStack. It is advisable to prioritize re-creating (deleting and re-deploying) over updating stacks.
Stacks with validated certificates
Section titled “Stacks with validated certificates”By default, stacks with validated certificates may not be deployed using the local lambda executor.
This originates from the way how CDK ensures the certificate is ready - it creates a single-file lambda function with a single dependency on aws-sdk which is usually preinstalled and available globally in lambda runtime.
When this lambda is executed locally from the /tmp folder, the package can not be discovered by Node due to the way how Node package resolution works.
CDK Version Compatibility
Section titled “CDK Version Compatibility”lstk cdk requires AWS CDK CLI 2.177.0 or newer, as noted in Installation.
If you’re using an older aws-cdk version, or the legacy cdklocal wrapper script, see CDK Version Compatibility on the deprecated wrapper scripts page for the relevant environment-variable workarounds.