studywithnova logo
  • Home 
  • About 
  1.   Posts
  1. Home
  2. Posts
  3. Simple Tutorial AWS CDK Deployment

Simple Tutorial AWS CDK Deployment

Posted on November 10, 2024 • 4 min read • 821 words
Share via
studywithnova
Link copied to clipboard

On this page
Here the keyword:   So, the step by step tutorial:   Key Benefits   Use Case  
Simple Tutorial AWS CDK Deployment
Photo by Nova

I decided to learn more about AWS CDK (Cloud Development Kit). In 2023, I had the opportunity to be a speaker at the AWS Summit Online ASEAN.

My presentation was about AWS CDK, and you can find my video and slides slides online.

One important question I discussed was: Infrastructure is always changing, so how can we make it repeatable and predictable? AWS CDK provides different levels of abstraction, while AWS CloudFormation is declarative. It is always a good idea to refer to the official documentations for accurate information.

I want to make another easy project today about simple AWS CDK Deployment.

Here the keyword:  

  1. Deploys an AWS Lambda function
  2. Creates an Amazon S3 bucket
  3. Grants the Lambda function read/write access to the S3 bucket

πŸ“Œ What Does This Project Do?

  • CDK deploys the infrastructure (Lambda & S3).
  • Lambda can process events & interact with S3 (e.g., store logs, process uploaded files).
  • You get an automated, repeatable cloud setup using Infrastructure as Code (IaC).

Alt text
AWS CDK Lambda S3 Architecture

So, the step by step tutorial:  

  1. Install AWS CLI
sudo apt update
sudo apt install awscli -y

after installation

aws --version
  1. Install AWS CDK (If you don’t install)
npm install -g aws-cdk

but, I failed using this, you must using sudo

sudo npm install -g aws-cdk

then check your cdk, its sucess install or not by

cdk --version
  1. You have AWS account with IAM user must have permissions to perform actions that CDK will use to manage AWS resources (Permission: AdministratorAccess) and you have Access key ID and Secret access key, then open your terminal
aws configure

Please input your data:

  • AWS_ACCESS_KEY_ID=“your-access-key”
  • AWS_SECRET_ACCESS_KEY=“your-secret-key”
  • Default AWS_REGION=“your-region”, e.g. us-west-2
  • Default output format= e.g. json
  1. Make directory, example name : my-cdk-project, and open directory
mkdir my-cdk-project && cd my-cdk-project
  1. Initialization cdk app
cdk init app --language typescript
  1. Look and familiar with the files and directory
ls
  1. then open lib/my-cdk-project-stack.ts add this code
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as path from 'path';

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

    // Create an S3 bucket
    const bucket = new s3.Bucket(this, 'MyBucket', {
      versioned: true,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });

    // Create a Lambda function
    const lambdaFunction = new lambda.Function(this, 'MyLambda', {
      runtime: lambda.Runtime.NODEJS_14_X,
      handler: 'index.handler',
      code: lambda.Code.fromAsset(path.join(__dirname, '../lambda')), // Ensure correct path
    });

    // Grant Lambda permissions to access the S3 bucket
    bucket.grantReadWrite(lambdaFunction);
  }
}

but, when first I tried, this code will not working, because Node.js 14.x being deprecated in AWS Lambda, AWS no longer supports creating or updating Lambda functions using nodejs14.x, so you must change to NODEJS_18_X

  1. Make lambda directory, then add index.js in it
mkdir lambda && cd lambda
touch index.js

add this code in index.js

exports.handler = async function(event) {
  console.log('Lambda function triggered!');
  console.log(event);

  return {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
  };
};
  1. The directory structure like this:
my-cdk-project/
β”‚-- bin/
β”‚-- lib/
β”‚   β”œβ”€β”€ my_cdk_project-stack.ts   # Defines S3 & Lambda resources
β”‚-- lambda/                        # Lambda function directory (new)
β”‚   β”œβ”€β”€ index.js                   # Lambda function code
β”‚-- test/
β”‚-- cdk.json
β”‚-- package.json
β”‚-- tsconfig.json
β”‚-- README.md
  • bin/ β†’ Entry point for the CDK application
  • lib/ β†’ Contains the main stack definition (e.g., creating S3, Lambda, etc.)
  • lambda/ β†’ Contains the code for your AWS Lambda function
  • test/ β†’ For writing tests for your CDK application
  • cdk.json β†’ Configuration file for AWS CDK
  • package.json β†’ Manages dependencies for the project
  • tsconfig.json β†’ TypeScript configuration
  • README.md β†’ Documentation for the project
  1. Build CDK project, then if you changing the code, you must run this (again)
npm run build
  1. This command will prepares your AWS environment by creating necessary resources that the CDK will use during the deployment of stacks.
cdk bootstrap

Alt text
This is my capture

  1. then, you’ll be able to deploy your infrastructure
cdk deploy

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

Alt text
deploy captures
Alt text
Your deployment was success

  1. Look at your AWS Console
  • CloudFormation, your new stacks there
    Alt text
    CloudFormation
  • S3
    Alt text
    S3
  • Lambda
    Alt text
    Lambda Function

Key Benefits  

AWS CDK helps automate cloud deployment, so you don’t need to manually set up AWS services. With CDK, you can write code to easily create and manage AWS resources.

It is also serverless and cost-effective. AWS Lambda only runs when triggered, which helps reduce costs. Since there are no servers to manage, it makes things simpler.

CDK makes your application scalable and secure. Amazon S3 can store unlimited data safely, while IAM roles help control access between Lambda and S3, ensuring security.

Then, from this simple tutorial, you can develop any idea, example:

Use Case  

  • Process Uploaded Images: Lambda resizes images when uploaded to S3.
  • Log Processing: Store logs in S3 and analyze them with Lambda.
  • Event-Driven Automation: Trigger Lambda on S3 file changes (e.g., send notifications).

That’s all, thank you for reading and happy coding!

fijne dag,

Nova

 From Dream to Reality, My Journey to AWS re:Invent 2024
Mastering AI in Cloud Computing, Embrace Innovation and Practical Skills! 
On this page:
Here the keyword:   So, the step by step tutorial:   Key Benefits   Use Case  
Copyright Β© 2025 studywithnova All rights reserved. | Powered by Hinode.
studywithnova
Code copied to clipboard