AWS CDK + Localstack (API Gateway, Lambda, SQS, DynamoDB, TypeScript)

Listen to this Post

Using an offline approach to iterate over your cloud designs can make sense in some cases. Pushing code changes frequently could waste time and cost some $$$s depending on what services you’re using. Using offline approaches like Localstack may be a good solution.

Localstack emulates most AWS services and could speed up your iterations as you can keep everything local. If you have many team members working independently, you may need to have many parallel cloud-based environments, and doing it all locally may be better. Once you are closer to the final versions, it is always recommended to deploy to AWS to understand the true behavior without emulation.

Read the full article here

You Should Know:

1. Setting Up Localstack

To get started with Localstack, install it using Docker:

docker run --rm -it -p 4566:4566 -p 4571:4571 localstack/localstack 

Verify it’s running:

curl http://localhost:4566/health | jq 

2. AWS CDK with Localstack

Install AWS CDK:

npm install -g aws-cdk 

Initialize a new CDK project:

mkdir cdk-localstack-demo && cd cdk-localstack-demo 
cdk init app --language typescript 

Configure AWS CLI to use Localstack:

aws configure set aws_access_key_id test 
aws configure set aws_secret_access_key test 
aws configure set default.region us-east-1 
aws configure set default.endpoint_url http://localhost:4566 

3. Deploying Serverless Components

Define a Lambda function in `lib/cdk-localstack-demo-stack.ts`:

import  as cdk from 'aws-cdk-lib'; 
import  as lambda from 'aws-cdk-lib/aws-lambda';

export class CdkLocalstackDemoStack extends cdk.Stack { 
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { 
super(scope, id, props);

const demoLambda = new lambda.Function(this, 'DemoLambda', { 
runtime: lambda.Runtime.NODEJS_14_X, 
code: lambda.Code.fromAsset('lambda'), 
handler: 'index.handler', 
}); 
} 
} 

4. Testing DynamoDB Locally

Create a DynamoDB table:

aws dynamodb create-table \ 
--table-name TestTable \ 
--attribute-definitions AttributeName=id,AttributeType=S \ 
--key-schema AttributeName=id,KeyType=HASH \ 
--billing-mode PAY_PER_REQUEST \ 
--endpoint-url=http://localhost:4566 

Insert test data:

aws dynamodb put-item \ 
--table-name TestTable \ 
--item '{"id": {"S": "1"}, "data": {"S": "Localstack Test"}}' \ 
--endpoint-url=http://localhost:4566 

5. Simulating SQS Locally

Create an SQS queue:

aws sqs create-queue --queue-name TestQueue --endpoint-url=http://localhost:4566 

Send a test message:

aws sqs send-message \ 
--queue-url http://localhost:4566/000000000000/TestQueue \ 
--message-body "Hello Localstack!" \ 
--endpoint-url=http://localhost:4566 

What Undercode Say:

Localstack is a powerful tool for offline AWS development, reducing costs and speeding up iterations. However, always validate in real AWS before production.

Additional Useful Commands:

  • List S3 Buckets (Localstack):
    aws s3 ls --endpoint-url=http://localhost:4566 
    

  • Invoke Lambda Manually:

    aws lambda invoke --function-name DemoLambda output.txt --endpoint-url=http://localhost:4566 
    

  • Check API Gateway Routes:

    aws apigateway get-rest-apis --endpoint-url=http://localhost:4566 
    

  • Clean Up Localstack:

    docker stop $(docker ps -aq) && docker rm $(docker ps -aq) 
    

Expected Output:

A fully functional local AWS environment for serverless testing with CDK, Lambda, DynamoDB, SQS, and API Gateway.

Read the full article here

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass βœ…

Join Our Cyber World:

πŸ’¬ Whatsapp | πŸ’¬ TelegramFeatured Image