Skip to content

Notes + exercises from the Udemy course "AWS & Typescript Masterclass - CDK, Serverless, React"

Notifications You must be signed in to change notification settings

eviuxdotdev/aws-cdk-ts-masterclass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 

Repository files navigation

Requisites

  • node (install using nvm)
  • aws cli

AWS CloudFormation intro

AWS CDK Code -> synths into -> AWS CF Template -> which deploys to -> AWS CF

AWS CDK

  • ! 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 the init command, you can use npx 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 para cdk_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 file
  • cdk.json: Configuration for the CDK
    • app property sets the entry point of the application
  • lib: Stack definitions are stored here

Useful References

CDK CLI Commands

  • 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>
  • cdk synth: synthetizes the stack(s); generates a cloudformation template to cdk.out folder
    • generates a cf template for each stack specified in the bin file
  • cdk list: lists all stacks defined in the app locally
  • cdk diff: lists all differences between the local resources and remote/deployed ones
  • cdk doctor: checks if there are any problems in the app config or libraries
  • cdk destroy <stack-id>: deletes the specified stack in aws

Constructs

  • 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

Outputs

  • we can define outputs in cdk just like we can in cf.
  • to do it, we must use a L1 construct called CfnOutput

Deployment Parameters

  • 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

CDK IDs

  • 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:
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.

Cloudformation Intrinsic Functions

  • 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 from aws-cdk-lib
    • Example: Fn.split(), Fn.select()

Multiple stacks

  • 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

Cross stack references

  • Can be done using: new CfnOutput in one stack and referencing using Fn.importValue() in the other

Sharing resources with CDK

  • instead of using CfnOutputs and Fn.importValue(), we can make the stack have public readonly properties and pass them to another stack class by extending its constructor props object.
    • this way, cdk is actually handling the outputs + imports behind the scenes for us

CDK Aspects

  • 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

Deleting stacks

  • 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.

AWS Lambda CDK Constructs

  • 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

About

Notes + exercises from the Udemy course "AWS & Typescript Masterclass - CDK, Serverless, React"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published