AWS Cloud Development Kit (CDK) - Create a VPC
Tags: aws, cloudskills, beginners, devops
Published: 2021-08-17 12:46:16


### Install CDK & create a custom VPC using **AWS Cloud Development Kit (CDK)**. 
As I couldn't find instructions on how to set up CDK from scratch, I decided to put together a set of instructions on how to do so. Please go through and let me know if you have any feedback on how to improve these instructions. 
Hope you find this helpful.

*The instructions below are for Mac OS*. 

###### 1. Install VS Code from https://code.visualstudio.com/

###### 2. Install HomeBrew
 

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install git
```

###### 3. Install AWS toolkit extension

```bash
Click on Extensions
Search for AWS Toolkit
Install and you will see AWS icon on the side bar
```
###### 4. Install AWS CLI

```bash
Cmd + Shift + P
Search for AWS: Create Credentials Profile
Create an AWS user with programmatic access
```
###### 5. Create a user in AWS Console with **Programmatic access** 
[Creating IAM user (console)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html#id_users_create_console) 
 
 
###### 6. Configure AWS Cli  

```bash
Configure the following credentials
/Users/[username]/.aws/credentials
[default]
aws_access_key_id = <key>
aws_secret_access_key = <secretkey>
```
```bash
/Users/[username]/.aws/config
[default]
region = ap-southeast-2
output=json 
```
```bash
To Verify
aws —version
or
Click on AWS icon on the side bar and you should see the region and the AWS services
```
###### 7. Install Node.js

 ```bash
Open the Terminal app and type brew update. This updates Homebrew with a list of the latest version of Node.
Type “brew install node”
```

> Note: if you receive this error “fatal: Could not resolve HEAD to a
> revision” then try to resolve it as mentioned in the following link
> https://stackoverflow.com/questions/65951726/trying-to-install-aws-sam-cli-through-brew-on-ubuntu-using-brew-tap-aws-tap-co

###### 8. Install CDK

```bash
npm install -g aws-cdk
``` 
> Note: If you notice an error “error eacces permission denied access '/usr/local/lib/node_modules'”
> ls -la /usr/local/lib/node_modules
> sudo chown -R $USER:admin /usr/local/lib/node_modules
```bash
To Verify
cdk --version
```

> To enable VS Code to open projects by using command “code .”
> Open the **Command Palette** via **(**⇧⌘**P)** and type shell command to find the Shell Command:
>shell command: Install 'code' command in PATH
>To Verify:  Type "code ."

###### 9. Install pip

 ```bash
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
```

###### 10. Infra As Code (IAC) using CDK

 ```bash
mkdir custom-vpc && cd custom-vpc
cdk init --language python
source  .env/bin/activate will take you to the virtual environment
echo "aws-cdk.aws_ec2" >> requirements.txt
```

###### 11. Add the following code to custom_vpc/custom_vpc_stack.py

```python
from aws_cdk import (aws_ec2  as  ec2, core  as  cdk)

# For consistency with other languages, `cdk` is the preferred import name for
# the CDK's core module. The following line also imports it as `core` for use
# with examples from the CDK Developer's Guide, which are in the process of
# being updated to use `cdk`. You may delete this import if you don't need it.

from aws_cdk import  core

class  CustomVpcStack(cdk.Stack):

def  __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:

	super().__init__(scope, construct_id, **kwargs)

	# The code that defines your stack goes here
	vpc = ec2.Vpc(
	self, "MyVpc",
	cidr="10.13.0.0/21",
	max_azs=2,
	nat_gateways=0,
	subnet_configuration=[
		ec2.SubnetConfiguration(name="public", cidr_mask=24, subnet_type=ec2.SubnetType.PUBLIC),
		ec2.SubnetConfiguration(name="private", cidr_mask=24, subnet_type=ec2.SubnetType.ISOLATED)
	]
)
```  

**CDK API Reference:** 
 https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html

###### 12. Check that there are no errors

```bash
cdk ls
CustomVpcStack
```
###### 13. Run `cdk synth` and checkout `cdk.out/CustomVpcStack.template.json` under `cdk.out`

> We could run this manually as well as a cloudformation template without using cdk
> Cdk synth generates a cloudformation template

###### 14. Run `cdk bootstrap`

> Uses the template to create the necessary resources such as S3 bucket to store the CloudFormation template and then it will use the template to deploy the resources

###### 15. Run `cdk deploy`

###### 16. **Clean-up**

```bash
cdk destroy
aws cloudformation delete-stack --stack-name CDKToolkit
aws s3 ls | grep cdktoolkit # copy the name
aws s3 rb s3://cdktoolkit-stagingbucket-abcdef # replace the name here
```