Project Setup: Initializing the Web App Backend with AWS CDK
Learn how to structure your serverless backend, define Lambda functions in CDK, and deploy your first project stack for our production-ready web app.
Previously in this course, we covered Automating Infrastructure with AWS CDK and Writing Your First Lambda Function. Now that you understand the individual components, it’s time to move from isolated experiments to a structured project. In this lesson, we will initialize our formal backend repository, define our primary Lambda function using TypeScript, and deploy the initial stack that will serve as the foundation for our entire serverless application.
The Backend Architecture
In a professional environment, "Project Setup" is about more than just creating a folder. It’s about establishing a predictable structure where your Infrastructure as Code (IaC) and your application code live in harmony. We want a layout that makes it trivial to add new features—like database tables or API endpoints—as the course progresses.
For our project, we will adopt the following structure:
/lib: Contains your CDK stack definitions./lambda: Contains the source code for your functions./bin: The entry point for your CDK application.
Initializing the Project Repository
First, ensure you have your environment ready by Configuring the AWS CLI. Once verified, we will bootstrap our project.
- Create a new directory and initialize the CDK project:
Bashmkdir my-serverless-app && cd my-serverless-app cdk init app --language typescript
- Your project now contains a
lib/my-serverless-app-stack.tsfile. This is where we will define our infrastructure.
Defining the Lambda Function in CDK
Instead of using the console, we will now define our Lambda function directly in the CDK stack. This ensures that every time we deploy, our infrastructure is updated programmatically.
Open lib/my-serverless-app-stack.ts and update it to include a Node.js Lambda function:
TYPESCRIPTimport * as cdk from CE9178">'aws-cdk-lib'; import * as lambda from CE9178">'aws-cdk-lib/aws-lambda'; import { Construct } from CE9178">'constructs'; export class MyServerlessAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define the Lambda function new lambda.Function(this, CE9178">'MyBackendFunction', { runtime: lambda.Runtime.NODEJS_18_X, handler: CE9178">'index.handler', code: lambda.Code.fromAsset(CE9178">'lambda'), }); } }
Next, create the lambda/index.js file to hold your logic:
JAVASCRIPTexports.handler = async (event) => { console.log("Event received:", JSON.stringify(event)); return { statusCode: 200, body: JSON.stringify({ message: "Backend initialized successfully!" }), }; };
Deploying the Backend Stack
With the code in place, we can now deploy this to AWS. Run the following command in your terminal:
Bashcdk deploy
CDK will synthesize your TypeScript into a CloudFormation template and push it to AWS. Once completed, you will have a live Lambda function managed entirely by your code. You can verify this by checking the AWS Lambda console, where you will see your function prefixed with your stack name.
Hands-on Exercise
To solidify this setup, perform the following steps:
- Modify your
lambda/index.jsto return a unique identifier (like a timestamp). - Deploy the change using
cdk deploy. - Invoke the function using the AWS CLI:
aws lambda invoke --function-name <YOUR_FUNCTION_NAME> response.json - Inspect the
response.jsonfile to see your updated message.
Common Pitfalls
- Asset Paths: Ensure your
lambdafolder is in the root directory. If CDK cannot find the directory specified inCode.fromAsset, the deployment will fail immediately. - Stack Names: If you are working in a shared account, add a prefix to your stack name in
bin/my-serverless-app.tsto avoid collisions with other students. - Node Version: Always ensure your local Node.js version matches the
Runtimespecified in your CDK stack to avoid unexpected behavior.
FAQ
Q: Do I need to manually upload my code to Lambda now?
A: No. cdk deploy handles the zipping and uploading of your lambda/ folder to an S3 bucket and updates the Lambda configuration automatically.
Q: Can I have multiple Lambda functions in one stack?
A: Yes, simply instantiate the lambda.Function construct multiple times within your stack class with unique IDs.
Recap
We have successfully transitioned from manual console configuration to a robust, code-driven backend setup. By using CDK to define our Lambda functions, we’ve ensured our environment is reproducible, version-controlled, and ready for the more complex integrations ahead.
Up next: Introduction to Amazon S3 — where we will learn how to store and manage static assets for our project.
Work with me

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.


