Rahul Lokurte
Rahul Lokurte’s Blog

Rahul Lokurte’s Blog

AWS S3 Using CDK

AWS S3 Using CDK

Rahul Lokurte
·Jul 11, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

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

s3-resource-created.png

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.

s3-bucket-created.png

We can also see the KMS which we have created.

s3-kms-created.png

Conclusion

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

Did you find this article valuable?

Support Rahul Lokurte by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this