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/
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install git
Click on Extensions Search for AWS Toolkit Install and you will see AWS icon on the side bar
Cmd + Shift + P Search for AWS: Create Credentials Profile Create an AWS user with programmatic access
Configure the following credentials /Users/[username]/.aws/credentials [default] aws_access_key_id = <key> aws_secret_access_key = <secretkey>
/Users/[username]/.aws/config [default] region = ap-southeast-2 output=json
To Verify aws —version or Click on AWS icon on the side bar and you should see the region and the AWS services
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
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
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 ."
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 get-pip.py
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
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:
cdk ls CustomVpcStack
We could run this manually as well as a cloudformation template without using cdk
Cdk synth generates a cloudformation template
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
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