Create your first S3 bucket using AWS CDK
Tags: aws, cloudnative, devops, typescript
Published: 2021-05-28 18:52:00

#### Infrastructure as Code

Infrastructure as code is the process of managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

If you want to know more about Infrastructure as Code and and when to use different IaC tools take a look at this article of mine.

[What is Infrastructure-as-Code and how is Terraform, CDK Ansible different?] (https://dev.to/aws-builders/what-is-infrastructure-as-code-how-is-terraform-cdk-ansible-different-2fbe)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8uuqp1krawknxi4nffbr.png) 

#### AWS CDK

* The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework to define cloud infrastructure in code and provision it through AWS CloudFormation.

* It offers a high-level object-oriented abstraction to define AWS resources imperatively using the power of modern programming languages. Using the CDK's library of infrastructure constructs, you can easily encapsulate AWS best practices in your infrastructure definition and share it without worrying about boilerplate logic.

* The AWS CDK has first-class support for TypeScript, JavaScript, Python, Java, and C#.

If you want to know more about AWS CDK go through below article.

[Everything about AWS CDK?] (https://dev.to/aws-builders/everything-about-aws-cdk-489m)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zn6rle0w2nd92z0v70sz.png)

#### Some information before we start

* What is TypeScript

TypeScript lets you write JavaScript the way you really want to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript is pure object oriented with classes, interfaces and statically typed like C# or Java. The popular JavaScript framework Angular 2.0 is written in TypeScript. Mastering TypeScript can help programmers to write object-oriented programs and have them compiled to JavaScript, both on server side and client side.

* Why am I choosing TypeScript for todays example?

TypeScript is a fully-supported client language for the AWS CDK and is considered stable. Working with the AWS CDK in TypeScript uses familiar tools, including Microsoft's TypeScript compiler (tsc), Node.js and the Node Package Manager (npm). Also TypeScript has biggest community support for AWS CDK.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfe7kvcehdz56spyzb0m.png)
 
#### Pre requisite

Today we will be creating a S3 bucket in your AWS environment by writing some sample code in TypeScript language.

Find below steps as pre requisite. 

* To use the AWS CDK, you need an AWS account and a corresponding access key. If you don't have an AWS account yet, see [Create and Activate an AWS Account] (https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/)


* To find out how to obtain an access key ID and secret access key for your AWS account, see [Understanding and Getting Your Security Credentials] (https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html)

* To find out how to configure your workstation so the AWS CDK uses your credentials, see [Setting Credentials in Node.js](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html)

* Download the latest version of Node.js (this will enable NPM which is package manager for Node). All AWS CDK applications require Node.js 10.13 or later, even if you work in Python, Java, or C#. You may download a compatible version.

Or, if you have the AWS CLI installed, the simplest way to set up your workstation with your AWS credentials is to open a command prompt and type: 
```
aws configure
```

* Install AWS CLI latest version on Windows (or Linux or Mac based on what OS you are using)

* After installing Node.js, install the AWS CDK Toolkit (the cdk command):

```
npm install -g aws-cdk
```
* Test your installation
```
cdk --version
```

#### Here comes the final steps to create your bucket

* You need TypeScript itself. If you don't already have it, you can install it using npm.
```
npm install -g typescript
```

* You create a new AWS CDK project by invoking cdk init in an empty directory.
```
mkdir my-project
cd my-project
cdk init app --language typescript
```
Creating a project also installs the core module and its dependencies.

cdk init uses the name of the project folder to name various elements of the project, including classes, subfolders, and files.

* Use the Node Package Manager (npm), included with Node.js, to install and update AWS Construct Library modules for use by your apps, as well as other packages you need.

The AWS CDK core module is named @aws-cdk/core. AWS Construct Library modules are named like @aws-cdk/SERVICE-NAME. 

We will install S3 as we will be creating a bucket it in, so run below command
```
npm install @aws-cdk/aws-s3
```

* Your project's dependencies are maintained in package.json. You can edit this file to lock some or all of your dependencies to a specific version or to allow them to be updated to newer versions under certain criteria. To update your project's NPM dependencies to the latest permitted version according to the rules you specified in package.json:
```
npm update
```

* Now if you go in your projects lib folder, you will find a file names 'my-project-stack.tc'. That's your parent app. It will initially not have any code to create any infrastructure. Copy below code snippet and replace your code with it. 

```
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';

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

    //The code that defines your stack goes here
    new s3.Bucket(this, 'MyFirstBucket', {
      bucketName: 'my-first-bucket',
      publicReadAccess: true,
      removalPolicy: cdk.RemovalPolicy.DESTROY
  
    });
 }
}
```

* Finally run it
```
cdk deploy
```
Login to your AWS console and go to S3. If you have followed all the steps properly you will see 'my-first-bucket' in there.

* Now if you wish you can destroy the bucket using below command
```
cdk destroy
```