- Install
node
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
source .bashrc
nvm install 12.16.3
node --version
npm --version
- Install the CDK cli
npm install -g aws-cdk
- Create the application
export PREFIX=<your prefix>
mkdir $PREFIX && cd $PREFIX
# We will init everything manually this time
# cdk init app --language javascript
npm init
- Install dependencies
npm install --save \
@aws-cdk/core \
@aws-cdk/aws-ec2
- Create the stack script
cat << EOF > index.js
const cdk = require('@aws-cdk/core');
const ec2 = require('@aws-cdk/aws-ec2');
class SimpleDemoStack extends cdk.Stack {
constructor(app, id) {
super(app, id);
const vpc = new ec2.Vpc(this, 'VPC');
}
}
const app = new cdk.App();
new SimpleDemoStack(app, '${PREFIX}SimpleDemoStack');
EOF
- Configure the CDK
cat << EOF > cdk.json
{
"app" : "node index"
}
EOF
- Generate the cloudformation template
cdk synth
cat cdk.out/*.json
- Check the security of the template with a linter
sudo apt-get install ruby-full
sudo gem install cfn-nag
cfn_nag_scan --input-path cdk.out/*.template.json
- Set the deployment region
export AWS_DEFAULT_REGION=eu-west-1
- Init the CDK infrastructure
cdk bootstrap
- Check what resources are going to be created
cdk diff
- Deploy all things!
cdk deploy
- Include a security group, if you are inclined to do so:
cat << EOF > index.js
const cdk = require('@aws-cdk/core');
const ec2 = require('@aws-cdk/aws-ec2');
class SimpleDemoStack extends cdk.Stack {
constructor(app, id) {
super(app, id);
const vpc = new ec2.Vpc(this, 'VPC');
const webSecurityGroup = new ec2.SecurityGroup(this, 'webSecurityGroup', {
description: 'Allow access to webservers',
groupName: 'sgweb',
vpc: vpc
});
webSecurityGroup.addIngressRule(new ec2.AnyIPv4(), new ec2.TcpPort(80), 'allow http access from any ip');
webSecurityGroup.addIngressRule(new ec2.AnyIPv4(), new ec2.TcpPort(443), 'allow https access from any ip');
}
}
const app = new cdk.App();
new SimpleDemoStack(app, '${PREFIX}SimpleDemoStack');
EOF
- Clean the house
cdk destroy
- Get the project source code:
git clone https://github.com/ciberado/quick-cdk-workshop
cd quick-cdk-workshop/demoasg
npm install
- Replace the name of the stack:
PREFIX=<put_your_own_prefix_here>
sed -i "s/PokemonStack/${PREFIX}PokemonStack/g" index.js
- Deploy it:
export AWS_DEFAULT_REGION=eu-west-1
cdk bootstrap
cdk diff
cdk deploy
- Check for the outputs:
STACK_NAME=$(cdk ls)
aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--region eu-west-1 \
--query Stacks[0].Outputs \
--output table
- Clean up everything
cdk destroy
- Get the project source code:
git clone https://github.com/ciberado/quick-cdk-workshop
cd quick-cdk-workshop/demofargate
npm install
- Replace the name of the stack:
PREFIX=<put_your_own_prefix_here>
sed -i "s/FargatePokemonStack/${PREFIX}FargatePokemonStack/g" index.ts
- Compile typescript to js:
npm run build
- Deploy it:
export AWS_DEFAULT_REGION=eu-west-1
cdk bootstrap
cdk diff
cdk deploy
- Clean up everything
cdk destroy
For more information, visit the complete CDK workshop and the official examples.