Building an Echo Lambda with AWS CDK and CloudShell

· 2min · Pragmatic AI Labs

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.

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

Want expert ML/AI training? Visit paiml.com

For hands-on courses: DS500 Platform

Based on this article's content, here are some courses that might interest you:

  1. AWS AI Analytics: Building High-Performance Systems with Rust (3 weeks) Build high-performance AWS AI analytics systems using Rust, focusing on efficiency, telemetry, and production-grade implementations
  2. AWS Advanced AI Engineering (1 week) Production LLM architecture patterns using Rust, AWS, and Bedrock.
  3. 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.
  4. CLI Automation with AWS Cloud Shell and Amazon Q: Building Modern DevOps Workflows (4 weeks) Master CLI automation and DevOps workflows using AWS Cloud Shell and Amazon Q, with Docker and CDK integration
  5. AWS AI Analytics: Enhancing Analytics Pipelines with AI (3 weeks) Transform analytics pipelines with AWS AI services, focusing on performance and cost optimization

Learn more at Pragmatic AI Labs