cd cdk-starter
- cdk init
- cdk init --language=typescript (For simple project)
- lib folder - This folder will contains the actual cloudformation
- bin folder - Contains the code to initialize the application stacks
- The dependencies are stored in package.json file.
- There are primary two types of dependencies
- devdependencies
- dependencies
- aws-cdk-lib - Construct lib
- constructs
- These will generate types.
- There are primary two types of dependencies
This is a blank project for CDK development with TypeScript.
The cdk.json
file tells the CDK Toolkit how to execute your app.
- "app": "npx ts-node --prefer-ts-exts bin/cdk-starter-new.ts" - This node execute.
- This is the entry point into our project. This is what is called when we call different cdk command
- inside our project, and makes it a CDK project.
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation template
- Create folder
- npm init -y
- install dependencies
- create launcher.ts file - This is like the bin folder for the app
- create empty stack
- create cdk.json file
cd space-finder-new
npm init -y
npm i -D aws-cdk aws-cdk lib constructs
npm i -D typescript ts-node
npm i -D @types/node
- Visitor pattern
- simple usecase: add tags
- popular usecase: enforce security or best practice (like a code linter)%
- Use case will be tagging for existing resources.
-
Runnable application code is made up of two set of code i.e APP Code and Dependencies code:
- app code
- Dependencies (Node Modules)
- other
- AWS SDK
Q: How can we make sure that when we are deploying our code we are not deploying our dependencies and not developer dependencies i.e. dependencies via AWS SDK
- Runnable Application
- TS APP CODE --> JS
- DEPENDENCIES --> USED
- DEPENDENCIES --> NOT USED
Example if we have a library that has a lot of feature, and out of those features we only use a few. It doesn't make sense to deploy the complete library if we only use a part of it - Like a function or two from a big library. This is called tree shaking.
The problem related to dependency management and bundling can be resolved using NodeJsFunction CDK Construct.
- Bundles all code with three shaking
- Complies TS to JS
- Leaves out AWS-SDK dependencies
- Completely editable
- Library: esbuild --> Takes your code and creates a unity bundle.
- Past solution: webpack - slow and hard to configure.
For getting the type dependencies for lambda
npm i -D @types/aws-lambda
- Deploy
- invoke
- read cloudwatch logs
- One approach will be to create multiple lambdas each corresponding to a specific method for each resource.
- Second approach will be group all our lambda togather per resources, which will handle all methods.
- Monolithic Lambda - A single lambda for all the resources example: /spaces and /reservations able to handle all the methods - NOT RECOMMENDED
- A good approach will be
- Apigateway handles routing
- Lambda handles HTTP method
- Dynamodb db - NoSQL DB
- Ideal for Documents and key-value models
- Handle Simple Queries
- Cheap, fast, scalable and limited indexing
- Handles simple Queries
- Section deals with putting all queries inside a lamdba and api gateway.
- Web Application can be handled via the internet
cannot read properties of undefined
- We get this if don't define the type correctly when defining the values in the ddb- Solutions:
- marshall & unmarshalling from @aws-cdk/util-dynamodb
- DynamoDBDocumentClient from @aws-sdk/lib-dynamodb
cdk code ---[synth]---> CF template ----[deploy]---->AWS CF where synth and deploy are two commands
- bin folder will have the initializtion code.
- lib folder will have the actual cloudformation template i.e. the code that will translate to cloudformation.
- package.json will have the dependencies that cdk will install.
- The dependencies will be of two types i.e Dev dependencies and dependencies.
- using the ts-node dependencies we don't have to build and could directly run the typescript code.
- What makes this project a cdk project is the dependencies i.e the aws-cdk dependencies, and the two construct lib i.e. constructs, and aws-cdk-lib.
- Additionally it also has a cdk.json file which configures the cdk inside this project.
- app command "npx ts-node --prefer-ts-exts bin/cdk-starter-new.ts" will be called when we call different cdk commands.
- npx is node execute, which will execute the ts-node library which allows direct execution of the typescript.
- then we have a path to our bin folder with cdk starter file, this is the entry point into our project. This is what is being called when we execute different cdk commands. Inside this project.
- npx is node execute, which will execute the ts-node library which allows direct execution of the typescript.
- app command "npx ts-node --prefer-ts-exts bin/cdk-starter-new.ts" will be called when we call different cdk commands.
- The dependencies will be of two types i.e Dev dependencies and dependencies.
- They are the basic building blocks for a CDK application.
- cfn (cloud formation) resources. When used, we must configure all properties.
- No pre configured options, requires all options to be build from scratch. ()
- CDK provides additional functionality like defaults, boiler plate and type safety for many parameters.
- One level of abstractions above the low level.
- Combine multiple types of resources and help with common tasks in AWS.
- Example LambdaRestAPI - Which is combination of lambda and api gateway combined together.
- L1 - Most AWS resources are migrated to L2. Use of new services that are still not migrated.
- L2 - Most of the time
- L3 - Matter of preference of company policy. What degree of abstraction do I want?
-
In cloudformation:
- All the resources are organized into stacks, and these stack are formed use L1 resources/constructs.
-
In CDK:
- CDK has a layer application just as an internal tool i.e. CDK app, it requries this application,
- but let's talk about these constructs for AWS.
- Where as the stack formed from the CDK app can contain a combination of L1, L2 and L3 constructs.
- But in the end the CDK generates i.e. cdk sync L1 constructs.
cdk init app --language typescript - We need to provide the app for which we need to initialize the project. cdk bootstrap - This is create a starter environment for all our applications. cdk synth - Only generates a cloudformation template for each stack. cdk deploy - Used to deploy the stack. If we have only one stack we can just call the cdk deploy. cdk list - list the number of stacks - This is done locally. cdk diff - Gives the delta between what we have locally and what is there remotely.
- By default the CDK resources that are created, example s3 bucket has a default removal policy.
- Due to which the s3 bucket will be orpaned when the stack is deleted.
- Additionally the assets bucket has the IAM polices etc., if that is deleted, you will not be able to
- delete the stack.
- Cloudformation will create a new-resource
- Delete the old one
- Construct ID, Logical ID, Physical ID when a stack is created
- Logical ID is requried by cloudformaton to use this ID inside Cloudformation.
- The physical ID is required by AWS. "ARN" - This is how a resource is referenced outside the stack.
- The corresponding logical ID will also change of that resource.
- If the logical ID of a resource changes, AWS will create and replace it.
- This means
- AWS will create a new resources
- AWS will delete the old once.
- Short definition: build-in function to help manager our stacks.
CDK code --[synth]--> cf template ---[deploy]---> AWS CF
At the Deployment step:
- stack is created. Available information:
- stack name, stack id
- deployment parameters, external parameters
- Resource1 is created. Ex: s3 bucket. Available info: bucket name, id
- Resource2 is created. Ex: s3 bucket. Available info: bucket name, id ... ...
Deployment finished.
The point is that CDK and cloudformation let us do this in the order we choose, if we choose one resource list to be created before another resource. This is important because maybe one resource needs information from another resouce. And here is where cloudformation instrinsic function come into place.
exmaple: Ref function, conditional function example: Fn:If
- Some stacks may contain sensetivie info (IAM roles, secrets)
- Some stacks may take a lot of time for deployment or deletion
- Allows us to deploy incremental changes corresponding to individual stacks.
- Resources get big and the need organizaton.
- There are no documented rules, not even best practices
- Separate stacks for resources with state(databases, buckets)
- Separate stacks for IAM roles, polices, secrets
- Separate stacks for resources with complex initialization (VPCs, DNS)
- CDK consider alphabetical order for deployment.
- If there is dependency between the stack, if the stacks are not deployed in sequence,
- the deployment will fail.
- Delete the stack that don't have any references first
- Once that is done, than delete the remaining stacks
- Using this approach you will not run into "rollback state"