AWS offers a JSON format called CloudFormation for describing what resources your applications need. We can send this JSON up to AWS which will then provision databases, lambda functions, and more for us according to our specifications.
Writing this JSON format is cumbersome though. Full applications can result in thousands of lines of CloudFormation JSON.
Many products have sprung up to address this and other problems with CloudFormation. The one we’ll be working with is AWS CDK, or Cloud Development Kit.
CDK comes in a few different languages including TypeScript, JavaScript, Python, Java, and C#. All of these allow us to use native language constructs to generate CloudFormation. Today we’ll be using the JavaScript version because unfortunately we don’t have Rust as an option here.
Make a new directory that will hold all of our cdk related code.
mkdir infra
cd infra
Node comes with a tool called npx
which will allow us to install and run the AWS CDK cli without installing it globally.
We’ll take advantage of npx to initialize a new javascript-based CDK application.
❯ npx aws-cdk init app --language=javascript
Need to install the following packages:
aws-cdk
Ok to proceed? (y) y
Applying project template app for javascript
# Welcome to your CDK JavaScript project
This is a blank project for JavaScript development with CDK.
The `cdk.json` file tells the CDK Toolkit how to execute your app. The build step is not required when using JavaScript.
## Useful commands
* `npm run test` perform the jest unit tests
* `cdk deploy` deploy this stack to your default AWS account/region
* `cdk diff` compare deployed stack with current state
* `cdk synth` emits the synthesized CloudFormation template
Executing npm install...
✅ All done!
This results in a number of files.
.
├── README.md
├── bin
│ └── infra.js
├── cdk.json
├── jest.config.js
├── lib
│ └── infra-stack.js
├── package-lock.json
├── package.json
└── test
└── infra.test.js
The jest.config.js
and test/
directory are for tests, which we won’t be covering today.
bin/infra.js
is the entrypoint to our CDK application. It imports the infrastructure we define in lib/infra-stack.js
and instantiates it. We don’t need to worry about this file much, except for the name in the string 'InfraStack'
, which has to be unique.
#!/usr/bin/env node
const cdk = require("aws-cdk-lib");
const { InfraStack } = require("../lib/infra-stack");
const app = new cdk.App();
new InfraStack(app, "InfraStack", {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */
/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
The package.json
contains which dependencies we use and some build scripts and the cdk.json
file contains some generated configuration that we could choose to modify.
We need to bootstrap the cdk environment now, which sets up a few resources CDK will need to use as it deploys our application. The most relevant of these is the Amazon S3 bucket, which is where our lambda code will be uploaded when we deploy functions.
CDK also bootstraps additional resources like IAM roles.
These resources can cost money but we’re talking way less than the cost of a single coffee per month, not “netflix subscription” amounts.
You can either bootstrap using your account number and an AWS region directly
cdk bootstrap aws://ACCOUNT-NUMBER-1/REGION-1
or you can use the profile we set up in “Creating an AWS Account”.
cdk bootstrap --profile rust-adventure-playground
The output will look something like this.
⏳ Bootstrapping environment aws://215832444507/us-east-2...
CDKToolkit: creating CloudFormation changeset...
✅ Environment aws://215832444507/us-east-2 bootstrapped.