AWS CDK (Cloud Development Kit)
source link: https://wilsonmar.github.io/aws-cdk/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Write dynamic Python, Java, .NET, NodeJs, or TypeScript code which are synth’d into Cloud Formation or Terraform static yaml
This article is a concise, yet deep technical description without generalizations that waste your time.
BTW content here are my personal opinions, and not intended to represent any employer (past or present).
AWS defines their CDK (Cloud Development Kit) as:
“A multi-language software development framework for modeling cloud infrastructure as reusable components”
Programmatic coding enable reusableclass libraries (templates) which define entire sets of services using a single line:*
#!/usr/bin/env node import 'source-map-support/register'; import * as cdk from '@aws-cdk/core'; import { TheBigFan Stack} from '../lib/the-big-fan-stack'; const app = new cdk.App(); new TheBigFanStack(app, 'TheBigFanStack');
The above code references “Constructs” which bundles infrastructure into reusable components deployed by a stack which “synth” out templates in CloudFormation or Terraform.
AWS and open-source contributors develop construct libraries for each of its many services (RDS, CloudFront, Kinesis, etc.). Reinvent 2019: How to make contributions ? https://www.youtube.com/watch?v=9As_ZIjUGmY
Template code contains:
- if/else and for loops (control logic)
- autocomplete
- in-line documentation
- compile-time warnings
- modeling of cross-account and cross-region pipeline configurations
https://cdkworkshop.com provides instructions for a “Hello, world” Lambda function fronted by an API Gateway endpoint for users to call via an HTTP request.
https://aws.amazon.com/solutions/implementations/centralized-logging/
VIDEO: Apps withine a CDK Pipeline:
vs Proton
How is CDK different versus the AWS Proton app delivery service which monitors and update deployments?
Install AWS CDK CLI
-
Have Homebrew installed for brew commands?
-
The AWS CDK is not “cdk” in Homebrew:
brew info aws-cdk
aws-cdk: stable 1.111.0 (bottled) AWS Cloud Development Kit - framework for defining AWS infra as code https://github.com/aws/aws-cdk Not installed From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/aws-cdk.rb License: Apache-2.0 ==> Dependencies Required: node ✘ ==> Analytics install: 3,718 (30 days), 11,115 (90 days), 35,166 (365 days) install-on-request: 3,719 (30 days), 11,119 (90 days), 35,167 (365 days) build-error: 0 (30 days)
-
No need to first install NodeJs
brew install aws-cdk
==> Installing dependencies for aws-cdk: libuv, nghttp2 and node ==> Installing aws-cdk dependency: libuv ==> Pouring libuv--1.41.1.mojave.bottle.tar.gz 🍺 /usr/local/Cellar/libuv/1.41.1: 49 files, 3.3MB ==> Installing aws-cdk dependency: nghttp2 ==> Pouring nghttp2--1.44.0.mojave.bottle.tar.gz 🍺 /usr/local/Cellar/nghttp2/1.44.0: 23 files, 2.6MB ==> Installing aws-cdk dependency: node ==> Pouring node--16.5.0.mojave.bottle.tar.gz 🍺 /usr/local/Cellar/node/16.5.0: 2,467 files, 48.3MB ==> Installing aws-cdk ==> Pouring aws-cdk--1.114.0.mojave.bottle.tar.gz 🍺 /usr/local/Cellar/aws-cdk/1.114.0: 8,201 files, 146.5MB
-
How much disk space?
df -h $(pwd)
-
See if it works:
cdk version
(node:8758) ExperimentalWarning: The fs.promises API is experimental 1.114.0 (build 7e41b6b)
Alternately, get a menu:
cdk
(node:8732) ExperimentalWarning: The fs.promises API is experimental Usage: cdk -a <cdk-app> COMMAND Commands: cdk list [STACKS..] Lists all stacks in the app [aliases: ls] cdk synthesize [STACKS..] Synthesizes and prints the CloudFormation template for this stack [aliases: synth] cdk bootstrap [ENVIRONMENTS..] Deploys the CDK toolkit stack into an AWS environment cdk deploy [STACKS..] Deploys the stack(s) named STACKS into your AWS account cdk destroy [STACKS..] Destroy the stack(s) named STACKS cdk diff [STACKS..] Compares the specified stack with the deployed stack or a local template file, and returns with status 1 if any difference is found cdk metadata [STACK] Returns all metadata associated with this stack cdk init [TEMPLATE] Create a new, empty CDK project from a template. cdk context Manage cached context values cdk docs Opens the reference documentation in a browser [aliases: doc] cdk doctor Check your set-up for potential problems Options: -a, --app REQUIRED: command-line for executing your app or a cloud assembly directory (e.g. "node bin/my-app.js") [string] -c, --context Add contextual string parameter (KEY=VALUE) [array] -p, --plugin Name or path of a node package that extend the CDK features. Can be specified multiple times [array] --trace Print trace for stack warnings [boolean] --strict Do not construct stacks with warnings [boolean] --lookups Perform context lookups (synthesis fails if this is disabled and context lookups need to be performed) [boolean] [default: true] --ignore-errors Ignores synthesis errors, which will likely produce an invalid output [boolean] [default: false] -j, --json Use JSON output instead of YAML when templates are printed to STDOUT [boolean] [default: false] -v, --verbose Show debug logs (specify multiple times to increase verbosity) [count] [default: false] --debug Enable emission of additional debugging information, such as creation stack traces of tokens [boolean] [default: false] --profile Use the indicated AWS profile as the default environment [string] --proxy Use the indicated proxy. Will read from HTTPS_PROXY environment variable if not specified [string] --ca-bundle-path Path to CA certificate to use when validating HTTPS requests. Will read from AWS_CA_BUNDLE environment variable if not specified [string] -i, --ec2creds Force trying to fetch EC2 instance credentials. Default: guess EC2 instance status [boolean] --version-reporting Include the "AWS::CDK::Metadata" resource in synthesized templates (enabled by default) [boolean] --path-metadata Include "aws:cdk:path" CloudFormation metadata for each resource (enabled by default) [boolean] [default: true] --asset-metadata Include "aws:asset:*" CloudFormation metadata for resources that uses assets (enabled by default) [boolean] [default: true] -r, --role-arn ARN of Role to use when invoking CloudFormation [string] --toolkit-stack-name The name of the CDK toolkit stack [string] --staging Copy assets to the output directory (use --no-staging to disable, needed for local debugging the source files with SAM CLI) [boolean] [default: true] -o, --output Emits the synthesized cloud assembly into a directory (default: cdk.out) [string] --no-color Removes colors and other style from console output [boolean] [default: false] --fail Fail with exit code 1 in case of diff [boolean] [default: false] --version Show version number [boolean] -h, --help Show help [boolean]
If your app has a single stack, there is no need to specify the stack name If one of cdk.json or ~/.cdk.json exists, options specified there will be used as defaults. Settings in cdk.json take precedence. </pre>
### Node version alignment
-
If you get an error such as this when you run:
/usr/local/Cellar/aws-cdk/1.114.0/libexec/lib/node_modules/aws-cdk/node_modules/yargs-parser/build/index.cjs:1007 throw Error(`yargs parser supports a minimum Node.js version of ${minNodeVersion}. Read our version support policy: https://github.com/yargs/yargs-parser#supported-nodejs-versions`); ^ Error: yargs parser supports a minimum Node.js version of 10. Read our version support policy: https://github.com/yargs/yargs-parser#supported-nodejs-versions
-
What version of NodeJs do you have installed?
node -v
If you have a version less that (10), such as “v9.11.1”, upgrade Node to the version specified (v10).
-
To enable multiple versions of Node to be installed simultaneously, install NVM:
brew install nvm
-
Identify an LTS version to install:
nvm ls
v8.11.1 v8.11.4 -> v9.11.1 system default -> node (-> v9.11.1) node -> stable (-> v9.11.1) (default) stable -> 9.11 (-> v9.11.1) (default) iojs -> N/A (default) lts/* -> lts/dubnium (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.16.0 (-> N/A) lts/dubnium -> v10.16.0 (-> N/A)
-
Based on the above:
nvm install 10.16.0
Now using node v10.16.0 (npm v6.9.0)
https://wilsonmar.github.io/node-osx-install/
NPM Code
-
In the https://docs.aws.amazon.com/cdk/latest/guide/cli.html install a fall-back global version:
npm install -g aws-cdk # install latest version
PROTIP: Be prepared to work with multiple versions of the AWS CDK installed. Install a matching version of the AWS CDK Toolkit in individual CDK projects (omitting -g from the npm install command).
-
use npx cdk to invoke it; this will run the local version if one exists, falling back to a global version if not.
However, there is also:
Initialize language folder
cdk init --language python
cdk deploy
cdk synth stack1
CDKGoat
https://github.com/bridgecrewio/cdkgoat is a “Vulnerable by Design” AWS CDK repository. CdkGoat is a learning and training project that demonstrates how common configuration errors can find their way into production cloud environments.
It also shows how Bridgecrew can be used with the AWS CDK to provide CloudFormation template vulnerability scanning at build time, even though no CloudFormation templates exist in the source repository.
Rockstars
Elad Ben-Israel (@emeshbi)
Jason Fulghum (CDK team Sr Mgr Dev Tools)
Jun Fisk at CloudAcademy.com:
VIDEO: “Multi-Stack Deployments with CDK for Terraform” discusses https://github.com/hashicorp/cdktf-multistack-serverless-example used by the end to end example in TypeScript for a serverless web application hosted on AWS and deployed with the CDK for Terraform.
-
Frontend: React, Create React App, statically hosted via AWS S3 + CloudFront
-
Backend API: AWS Lambda + API Gateway + DynamoDB
Resources
-
https://www.youtube.com/watch?v=1ps0Wh19MHQ
-
https://www.youtube.com/watch?v=Q1FcifrDocE
-
https://www.youtube.com/watch?v=Cf3yJv3klsg
-
https://www.youtube.com/watch?v=ZWCvNFUN-sU
https://www.hashicorp.com/blog/cdk-for-terraform-enabling-python-and-typescript-support
Others must know: please click to share:
AWS CDK (Cloud Development Kit) was published on May 04, 2022.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK