DEV Community

Cover image for AWS Cloud Development Kit (CDK) - Create a VPC
Sri
Sri

Posted on • Edited on

AWS Cloud Development Kit (CDK) - Create a VPC

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
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install git
Enter fullscreen mode Exit fullscreen mode
3. Install AWS toolkit extension
Click on Extensions
Search for AWS Toolkit
Install and you will see AWS icon on the side bar
Enter fullscreen mode Exit fullscreen mode
4. Install AWS CLI
Cmd + Shift + P
Search for AWS: Create Credentials Profile
Create an AWS user with programmatic access
Enter fullscreen mode Exit fullscreen mode
5. Create a user in AWS Console with Programmatic access

Creating IAM user (console)

6. Configure AWS Cli
Configure the following credentials
/Users/[username]/.aws/credentials
[default]
aws_access_key_id = <key>
aws_secret_access_key = <secretkey>
Enter fullscreen mode Exit fullscreen mode
/Users/[username]/.aws/config
[default]
region = ap-southeast-2
output=json 
Enter fullscreen mode Exit fullscreen mode
To Verify
aws —version
or
Click on AWS icon on the side bar and you should see the region and the AWS services
Enter fullscreen mode Exit fullscreen mode
7. Install Node.js
Open the Terminal app and type brew update. This updates Homebrew with a list of the latest version of Node.
Type “brew install node”
Enter fullscreen mode Exit fullscreen mode

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
npm install -g aws-cdk
Enter fullscreen mode Exit fullscreen mode

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

To Verify
cdk --version
Enter fullscreen mode Exit fullscreen mode

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
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
Enter fullscreen mode Exit fullscreen mode
10. Infra As Code (IAC) using CDK
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
Enter fullscreen mode Exit fullscreen mode
11. Add the following code to custom_vpc/custom_vpc_stack.py
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)
    ]
)
Enter fullscreen mode Exit fullscreen mode

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

12. Check that there are no errors
cdk ls
CustomVpcStack
Enter fullscreen mode Exit fullscreen mode
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
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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)