Building an Echo Lambda with AWS CDK and CloudShell
Table of Contents
Introduction
AWS CloudShell comes with CDK pre-installed, making it an ideal environment for quick serverless deployments. Let's build an Echo Lambda function that randomly responds with light or dark echoes.
Do you want to learn AWS Advanced AI Engineering?
Production LLM architecture patterns using Rust, AWS, and Bedrock.
Check out our course!Project Setup
First, check CDK version and create project structure:
cdk --version # Verify installed version
mkdir -p ~/echo-cdk-tutorial/lib && cd ~/echo-cdk-tutorial
npm init -y
npm install aws-cdk-lib@2.158.0 # Pin to CloudShell version
Create necessary files:
touch cdk.json lib/echo-stack.js lib/Dockerfile lib/echo.js
Configuration Files
cdk.json:
{
"app": "node lib/echo-stack.js"
}
lib/echo-stack.js:
const { App, Stack } = require("aws-cdk-lib");
const { DockerImageFunction, DockerImageCode } = require("aws-cdk-lib/aws-lambda");
const path = require("path");
const app = new App();
class EchoStack extends Stack {
constructor(scope, id, props) {
super(scope, id, props);
const dockerfileDir = path.join(__dirname);
new DockerImageFunction(this, "EchoFunction", {
code: DockerImageCode.fromImageAsset(dockerfileDir),
functionName: "EchoFunction",
});
}
}
new EchoStack(app, "EchoStack");
Create Dockerfile:
echo "FROM public.ecr.aws/lambda/nodejs:20
COPY echo.js \${LAMBDA_TASK_ROOT}
CMD [ \"echo.handler\" ]" > lib/Dockerfile
Lambda function (lib/echo.js):
exports.handler = async (event) => {
const message = (event.body ? JSON.parse(event.body).message : event.message ||
event.queryStringParameters?.message ||
event || "").trim().toLowerCase();
const response = message === "echo"
? Math.random() < 0.5 ? "🌑 dark ECHO" : "💡 light ECHO"
: `You said: ${message}. Say 'echo' to play the tunnel game!`;
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: response }),
};
};
Deployment
Set region and deploy:
export AWS_DEFAULT_REGION=us-east-1
cdk synth
cdk deploy --require-approval never
Testing
Test regular message:
aws lambda invoke \
--function-name EchoFunction \
--cli-binary-format raw-in-base64-out \
--payload '{"message":"hello"}' \
response.json
cat response.json
Play the echo game:
aws lambda invoke \
--function-name EchoFunction \
--cli-binary-format raw-in-base64-out \
--payload '{"message":"echo"}' \
response.json
cat response.json
Cleanup
Remove all resources:
cdk destroy --force
cd ~ && rm -rf ~/echo-cdk-tutorial
Key Points
- CloudShell provides pre-installed CDK
- Pin CDK library version to match CloudShell
- Docker-based Lambda deployment is straightforward with CDK
- Amazon Q can help with CDK commands and bootstrapping
- Always clean up resources after testing
Recommended Courses
Based on this article's content, here are some courses that might interest you:
-
AWS Advanced AI Engineering (1 week)
Production LLM architecture patterns using Rust, AWS, and Bedrock. -
Enterprise AI Operations with AWS (2 weeks)
Master enterprise AI operations with AWS services -
Natural Language AI with Bedrock (1 week)
Get started with Natural Language Processing using Amazon Bedrock in this introductory course focused on building basic NLP applications. Learn the fundamentals of text processing pipelines and how to leverage Bedrock's core features while following AWS best practices. -
Rust-Powered AWS Serverless (4 weeks)
Learn to develop serverless applications on AWS using Rust and AWS Lambda. Master the fundamentals of serverless architecture while building practical applications and understanding performance optimizations. -
Natural Language Processing with Amazon Bedrock (2 weeks)
Build production NLP systems with Amazon Bedrock
Learn more at Pragmatic AI Labs