These are my notes on setting up a new development environment to use the AWS CDK.
Most of this is very well documented already but I’m planning on using this setup for a few upcoming posts, so thought I’d start at the very beginning.
I’m using a Mac but the steps are much the same regardless of OS.
AWS CLI install from here:
https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-mac.html
Install node.js from: https://nodejs.org/ if you don’t have it already
Then use node to install the aws-cdk
npm install -g aws-cdk
Check the cdk is working with:
cdk --version
Optionally, you may also want to install python or java
An AWS Cloud is needed, so you must have or create an AWS account.
Once that’s done, we need to setup an AWS User & give it sufficient access:
https://cdkworkshop.com/15-prerequisites/200-account.html
Go to the IAM console in your AWS account and select Users > Create User
I called mine “cdk-user“. Select to enable Programattic Access and add an Admin policy. At the end of the process, select to download the new users credentials.
In your local command line, run:
aws configure
this will prompt you to supply the newly created credentials.
Once that is done, you can test connectivity from your local shell to your AWS account with some simple aws commands like:
aws s3 ls
which should simply list the S3 buckets in your account to prove connectivity is working; it may return nothing if there are no buckerts, or an error if it can’t connect.
Now is a good time to decide on an IDE like vscode or atom.
They both have extensions and plugins that make CDK development easier, no matter which language you choose to develop your CDK apps in.
To create a new simple project, run
cdk init sample-app --language typescript
as described here:
https://cdkworkshop.com/20-typescript/20-create-project.html
this is a very simple project but ideal for testing the setup and end to end process.
Before you can deploy you need to initialise CDK in the account:
https://cdkworkshop.com/20-typescript/20-create-project/100-cdk-init.html
cdk bootstrap
This creates an S3 bucket in our AWS account with the supporting files needed by CDK. This only needs done once.
take a look around the example app’s code, there are a few files of interest:
lib/cdk-workshop-stack.ts contains the tiny amount of code used to create an SQS queue and an SNS topic
package.json details the project dependencies and node shortcuts for building, watching, testing etc
README.md details some useful commands and describes the overall project
After boot-strapping, if you run another
aws s3 ls
you should now see the bootstrap S3 bucket.
and when ready, you can deploy the simple example app with
cdk deploy
CDK will let you know in advance what is is planning on doing – which resources will be created, deleted or altered, giving you a chance to backout
After confirming you want to go ahead with these changes, you should soon see the new stack within your CloudFormation console, along with the CDK toolkit stack we bootstrapped
to delete the example stack and clean up, simply do
cdk destroy
That’s it – the local environment is set up and can connect to the AWS account, a very simple app has been built, tested, deployed and deleted, and the one-off CDK bootstrapping has been done.
Next: on to something more interesting!