AWS CDK

Use the AWS CDK (Cloud Development Kit) with LocalStack

AWS CDK

Overview

The AWS Cloud Development Kit (CDK) is an IaC (Infrastructure-as-Code) 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

cdklocal is a thin wrapper script for using the AWS CDK library against local APIs provided by LocalStack.

Installation

The cdklocal command line is published as an npm library:

  1. # Install globally
  2. npm install -g aws-cdk-local aws-cdk
  3. # Verify it installed correctly
  4. cdklocal --version
  5. # e.g. 1.65.5

Local node_modules

Using cdklocal locally (e.g. within the node_modules of your repo instead of globally installed) does not work at the moment for some setups, so make sure you install both aws-cdk and aws-cdk-local with the -G flag.

Usage

cdklocal can be used as a drop-in replacement of where you would otherwise use cdk when targeting the AWS Cloud.

  1. $ cdklocal --help

Configuration

The following environment variables can be configured:

  • EDGE_PORT: Port under which LocalStack edge service is accessible (default: 4566)
  • LOCALSTACK_HOSTNAME: Target host under which LocalStack edge service is accessible (default: localhost)
  • LAMBDA_MOUNT_CODE: Whether to use local Lambda code mounting (via setting __local__ S3 bucket name)

Example

Make sure that LocalStack is installed and successfully started with the required services before running the example

  1. $ curl http://localhost:4566/health

The CDK command line ships with a sample app generator to run a quick test for getting started.

  1. # create sample app
  2. mkdir /tmp/test; cd /tmp/test
  3. cdklocal init sample-app --language=javascript
  4. # deploy the sample app
  5. cdklocal deploy
  6. > Do you wish to deploy these changes (y/n)? y

Once the deployment is done, you can inspect the created resources via the awslocal command line

  1. $ awslocal sns list-topics
  2. {
  3. "Topics": [
  4. {
  5. "TopicArn": "arn:aws:sns:us-east-1:000000000000:TestStack-TestTopic339EC197-79F43WWCCS4Z"
  6. }
  7. ]
  8. }

External resources

Community resources

Last modified October 11, 2021: minor: rename folder Integrations->integrations (68c86019)