AWS S3 Using CDK

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.
Search for a command to run...

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.
No comments yet. Be the first to comment.
This series will cover provisioning Aws resources using Cloud Development Kit.
The software packages need to be stored in a central repository as artifacts. The artifacts need to be stored securely. We should be able to publish new artifacts and also needs to share the artifacts across the organization. Amazon provides a manage...
Transformers, the backbone of many state-of-the-art NLP models such as BERT, GPT has revolutionized the way we approach natural language understanding tasks. One key innovation in transformers is their ability to handle entire sequences of tokens sim...

When an User prompts the questions to the LLM, it produces the response based on the training data. The pre-trained LLM data is its parametric data. It might not have an context specific to our query. LLM still produces an answer confidently. It is t...

In the fast-evolving field of artificial intelligence, Large Language Models (LLMs) have rapidly transformed how we approach language processing. These models, such as ChatGPT, Claude, and Bard, have proven their utility across a range of application...

Amazon S3 is a powerful, scalable storage service, but when multiple clients or processes attempt to write to the same S3 object simultaneously, things can get tricky. Since S3 doesn’t natively handle concurrent writes or provide object locking, it’s...

When working with distributed systems in a microservice architecture, we must always architect our services with failure in mind. When the transaction happens between a microservice, we have to handle the failures. The Saga pattern can be used in a f...

Amazon simple storage service (S3) is object storage built to store and retrieve unlimited amounts of data. We can use it to store various kinds of data. We can have data lakes to push the data into S3, we can collect the click events of a web application, we can also use it for data analytics.
Files on S3 are stored in buckets, which are unlimited in size and each bucket has a globally unique name.
In this blog post, we're going to create and deploy a new S3 bucket to AWS Using Cloud Development Kit.
Create a new directory on your system.
mkdir cdk-s3 && cd cdk-s3
We will use cdk init to create a new Javascript CDK project:
cdk init --language javascript
The cdk init command creates a number of files and folders inside the cdk-s3 directory to help us organize the source code for your AWS CDK app.
We can list the stacks in our app by running the below command. It will show CdkS3Stack.
_$ cdk ls
CdkS3Stack
Let us install AWS S3 construct library and AWS Key Management Service (KMS) to protect the keys.
npm install @aws-cdk/aws-s3 @aws-cdk/aws-kms
Edit the file lib/cdk-s3-stack.js to create an AWS lambda resource as shown below.
const cdk = require("@aws-cdk/core");
const kms = require("@aws-cdk/aws-kms");
const s3 = require("@aws-cdk/aws-s3");
class CdkS3Stack extends cdk.Stack {
/**
*
* @param {cdk.Construct} scope
* @param {string} id
* @param {cdk.StackProps=} props
*/
constructor(scope, id, props) {
super(scope, id, props);
// The code that defines your stack goes here
const myKmsKey = new kms.Key(this, "MyKey", {
enableKeyRotation: true,
});
const myBucket = new s3.Bucket(this, "MyBucket", {
bucketName: cdk.PhysicalName.GENERATE_IF_NEEDED,
encryption: s3.BucketEncryption.KMS,
encryptionKey: myKmsKey,
versioned: true,
});
}
}
module.exports = { CdkS3Stack };
The Bucket name will be Generated one.
We are using KMS encryption and the Key which we have defined myKmsKey .
Versioning is set to true.
Before deploying the AWS resource, we can take a look at what resources will be getting created by using the below command.
cdk diff

NOTE: If we have multiple profiles set in our system, we need to tell cdk to look into the particular profile. This can be done, by adding the below key-value in cdk.json which was generated when we created a CDK project.
"profile": "<YOUR_PROFILE_NAME>"
Now, once we are ok with the resources which will be created, we can deploy them using the below command
cdk deploy
Let us open the AWS Lambda console. We can see that the S3 bucket has been created.

We can also see the KMS which we have created.

In this blog post, we saw how to create an S3 Bucket and also KMS by using AWS Cloud Development Kit. We also saw various commands related to CDK for initiating projects, deploying the resources to AWS.
GitHub Repository: https://github.com/rahulmlokurte/aws-usage/tree/main/aws-cdk/cdk-s3