- node (install using nvm)
- aws cli
AWS CDK Code -> synths into -> AWS CF Template -> which deploys to -> AWS CF
- ! tip: Don't install
aws-cdk
globally.- Instead, for every project install the package locally and set its version in package.json file
- If you need to use the
aws-cdk
package commands, such as theinit
command, you can usenpx
for that. Like so:npx cdk <command>
- Afterwards, everytime you want to use cdk use
npm run cdk
to use the local installation of the package - Reference
- TODO: Agregar un alias a
$ZSH_CUSTOM/aliases.zsh
paracdk_new='npx cdk init --language=typescript'
Inside the init folder, important files are:
bin/cdk-starter.ts
: entry point for the project, as set by the 'app' property on the cdk.json filecdk.json
: Configuration for the CDKapp
property sets the entry point of the application
lib
: Stack definitions are stored here
cdk init app --language=typescript
cdk bootstrap
: prepares the aws environment for usage with cdk. before a cdk app is deployed, the env must first be bootstrapped,- it provisions a s3 bucket (used to store cdk project files), a ecr repository (used to store docker images) and iam roles (configured to grant permissions needed by cdk to perform deployments)
cdk deploy
: synthetizes and deploys the app to aws- creates/updates a
cdk.out
folder, which contains the cloudformation template that is synthetized by cdk and deployed to aws - to deploy a specific stack, use
cdk deploy <stack-id>
- creates/updates a
cdk synth
: synthetizes the stack(s); generates a cloudformation template tocdk.out
folder- generates a cf template for each stack specified in the bin file
cdk list
: lists all stacks defined in the app locallycdk diff
: lists all differences between the local resources and remote/deployed onescdk doctor
: checks if there are any problems in the app config or librariescdk destroy <stack-id>
: deletes the specified stack in aws
- basic building blocks of a cdk app
- levels/types of constructs:
- L1 - low level constructs: represents raw aws resources (no default options -- must configure all properties)
- L2 - higher level constructs: represents aws resources with a higher level of abstraction (default options)
- L3 - patterns: combines multiple resources, to represent a commonly used pattern of resources used in aws
- we can define outputs in cdk just like we can in cf.
- to do it, we must use a L1 construct called
CfnOutput
- we can define parameters in cdk just like we can in cf
- to do so, we can use a L1 construct called
CfnParameter
- then, to pass the parameters to a cdk command, one can use:
cdk <command> --parameters <parameter-name>=<parameter-value>
- TODO: Check external parameters
- any resource declared in a cdk app requires an ID. this is the second parameter given to a construct's constructor method.
- this cdk id is used in the logical id (and physical id, if none is provided in the resource's options) of the resource in cloudformation.
- the logical id in cf consists of the cdk id + a random string (this is generated by cdk)
- to override this behavior we can use the following:
- the logical id in cf consists of the cdk id + a random string (this is generated by cdk)
const bucket = new Bucket(this, 'PhotoBucket')
(bucket.node.defaultChild as CfnBucket).overrideLogicalId('PhotoBucket_OverridenLogicalId')
- the physical id of a resource can be generated by cdk or can be established in the resource's construct options
- be careful of manually naming resources' logical or physical id, since it could result in some weird behavior such as resources being unable to be replaced.
- build-in functions to help manage our stacks
- complete reference of cf intrinsic functions here
- these functions can be used in the app's code, importing
Fn
fromaws-cdk-lib
- Example:
Fn.split()
,Fn.select()
- Example:
- why?
- bc of stacks with sensitive info
- some stacks may take a lot of deploy/deletion time
- organization of resources
- to deploy multiple stacks, we must use
cdk deploy --all
- Can be done using:
new CfnOutput
in one stack and referencing usingFn.importValue()
in the other
- instead of using
CfnOutputs
andFn.importValue()
, we can make the stack have public readonly properties and pass them to another stack class by extending its constructorprops
object.- this way, cdk is actually handling the outputs + imports behind the scenes for us
- feature of cdk that lets us check or modify resources after they were created
- implements the visitor pattern
- simple use case: add tag to a resource
- popular usecase: enforce security or best practices (like a code linter)
- it's useful to apply an operation to all constructs in a given scope (reference)
- cdk-nag: popular library to enforce rules via cdk aspects
- if a stack has resources that are referenced in other stacks, this stack cannot be deleted
- to delete them, the stack the reference has to be first removed or the whole stack should be deleted
- ! note: this is only recommended in test/learning environments. never do this in prod.
- CDK offers a construct that provides many utilities to work with AWS Lambda.
- This construct is named
NodejsFunction
, and provides:- Code bundling with tree shaking
- Compiles TS to JS
- Leaves out AWS-SDK dependencies
- Completely editable
- Library used for bunding is esbuild