Simple Tutorial AWS CDK Deployment
Posted on November 10, 2024 • 4 min read • 821 wordsI 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.
π What Does This Project Do?
sudo apt update
sudo apt install awscli -y
after installation
aws --version
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
aws configure
Please input your data:
mkdir my-cdk-project && cd my-cdk-project
cdk init app --language typescript
ls
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
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!'),
};
};
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
npm run build
cdk bootstrap
cdk deploy
Do you wish to deploy these changes (y/n)? type ‘y’
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:
That’s all, thank you for reading and happy coding!
fijne dag,
Nova