Local Development with AWS on LocalStack + CDK

Kobe
3 min readAug 6, 2021

--

In this article, we will look at how to set up Localstack in a node app. Localstack allows you to simulate a number of AWS services on your machine.

AWS Services — LocalStack

What is LocalStack?

LocalStack is an open-source emulator of real AWS services. It provides a test environment on our local machine with the same APIs as real AWS services. It provides an easy-to-use test/mocking framework for developing Cloud applications.

Note: LocalStack supporting only AWS cloud stack.

LocalStack spins up the following core Cloud APIs on your local machine.

  • ACM, API Gateway, CloudFormation, CloudWatch
  • CloudWatch Logs, DynamoDB, DynamoDB Streams
  • EC2, Elasticsearch Service, EventBridge (CloudWatch Events)
  • Firehose, IAM, Kinesis, KMS, Lambda, Redshift
  • Route53, S3, SecretsManager, SES, SNS
  • SQS, SSM, StepFunctions, STS

Why use LocalStack?

As a developer working for some projects are using cloud services, I always have trouble with the client about ( Hey guy could you give me the permission to access these services and I must take 2–3 days for the DevOps team setup create a new role blah blah )

I want to make some demonstrate however it can impact some costs related to the client and we need to debug also troubleshooting issue before making some decisions.

We have multiple teams working together however only one environment is available for our project ( We must overlap the code change, some bad data from another team can impact another one )

So I need a local environment — it can help our developer speed up development/testing and demo for our QA before we deliver a new build for QA. That reason I’m using LocalStack for my development.

Install & Setup LocalStack

  1. Install Docker if you haven’t already.

2. Install AWS CLI. While we won’t be working with “real” AWS, we will be using it to communicate with our local docker containers.

3. Add new file docker-compose.yml with the content below

version: "3.8"

services:
localstack:
container_name: AWS-DEVELOPMENT-WITH-LOCALSTACK
image: localstack/localstack:latest
network_mode: bridge
ports:
- "127.0.0.1:53:53"
- "127.0.0.1:53:53/udp"
- "127.0.0.1:443:443"
- "127.0.0.1:4566:4566"
- "127.0.0.1:4571:4571"
- "127.0.0.1:${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
environment:
- SERVICES=s3,dynamodb,sns,sqs,firehose,kinesis,ses,sts,cloudformation
- DEBUG=1
- DATA_DIR=/tmp/localstack/data
- PORT_WEB_UI=8080
- LAMBDA_EXECUTOR=local
- KINESIS_ERROR_PROBABILITY=1.0
- DOCKER_HOST=unix:///var/run/docker.sock
- HOST_TMP_FOLDER=./.localstack
volumes:
- './.localstack:/tmp/localstack'
- '/var/run/docker.sock:/var/run/docker.sock'

4. Starting our Container

Now that we have our docker-compose.yml in good shape, we can spin up the container: docker-compose up -d.

5. Make sure your container runs successfully, open the browser, and check our services running. http://localhost:4566/health

{
"services": {
"dynamodbstreams": "running",
"firehose": "running",
"kinesis": "running",
"s3": "running",
"ses": "running",
"sns": "running",
"sqs": "running",
"dynamodb": "running"
}
}

Install & Setup AWS-CDK & AWS CDK Local

The cdklocal the command line is published as an npm library:

$ npm install -g aws-cdk-local aws-cdk
...
$ cdklocal --version
1.117.0 (build 0047c98)

Create a sample app to verify our localstack

cdklocal init sample-app --language=typescript

the sample-app is required ( can’t change it ) just run and we will update to another name later ( in our case we just change the root folder to infra )

You can check out this repo from Github: here

Deploy our Application Stack.

cdklocal deploy — profile localstack

#init creates fake news credential — use for our localstack

cat ~/.aws/credentials[localstack]
aws_access_key_id = test
aws_secret_access_key = test
region = us-west-2
output = json

cdklocal deploy — profile localstack

IAM Statement ChangesResource : ${SampleAppQueue.Arn}
Effect : Allow
Action : sqs:SendMessage
Principal : Service:sns.amazonaws.com
Condition : "ArnEquals": { "aws:SourceArn": "${SampleAppTopic}" }
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)

Do you wish to deploy these changes (y/n)?

#### Type `y` and see our stack

ApplicationStack: deploying...
ApplicationStack: creating CloudFormation changeset...


✅ ApplicationStack

Stack ARN:arn:aws:cloudformation:us-east2:000000000000:stack/ApplicationStack/e67d04e2

Recheck our services

aws --endpoint-url=http://localhost:4566 sns list-topics

Output

"Topics": [
{
"TopicArn": "arn:aws:sns:us-east-2:000000000000:topic-d7ce6939"
}
]

How to Destroy our Application Stack

cdklocal destroy

(base) haithai@FVFY201CHV2H infra % cdklocal destroy
Are you sure you want to delete: ApplicationStack (y/n)? y
ApplicationStack: destroying...

✅ ApplicationStack: destroyed

aws --endpoint-url=http://localhost:4566 sns list-topics

{
"Topics": []
}

--

--

Kobe
Kobe

Written by Kobe

I’m working at KMS-Technology company. I love code (▀̿Ĺ̯▀̿ ̿) — Full Stack Software Engineer

No responses yet