Skip to content

Instantly share code, notes, and snippets.

@deependhamecha
Last active June 21, 2023 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deependhamecha/795b40a879212b89f94d69d545d33a7e to your computer and use it in GitHub Desktop.
Save deependhamecha/795b40a879212b89f94d69d545d33a7e to your computer and use it in GitHub Desktop.
AWS & Typescript MasterClass - CDK V2, Serverless, React

Create an IAM User with AdministratorAccess policy.

Install AWS Cli, Go to IAM User and Security credentials, Access Key, Create Access Key.

aws configure

Enter Values and Output format as json.

Cloud Formation

Stacks are way to organize resources.

Upload a simple JSON.

{
  "Resources": {
    "HelloBucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "AccessControl": "PublicRead"
      }
    }
  }
}

AWS CDK

Install CDK

npm i -g aws-cdk

Initialize CDK

cdk init --language=typescript

cdk.json cdk configuration file.

Inside Project root folder run cdk bootstrap.

Check CloudFormation View in AWS console.

Now run cdk deploy

Check CloudFormation, it will add CDKStarterStack and inside resources it will have CDKMetadata.

Check cdk.out` folder in root folder of the project, which is deployed in the AWS Stacks.

cdk synth will only generate template files but does not deploy, so do cdk synth && cdk deploy

There are 3 level of CDK constructs

  1. L1: Low level constructs - (Cloud formation) resources. When used, we must configure all properties. (Most AWS resources are migrated to L2)
  2. L2: AWS resources with higher-level - CDK provides additional functionality like defaults, boiler plate and type safety for many parameters. (Most of the time)
  3. L3: Patterns: Combine multiple types of resouces and help with common tasks in AWS. Examples: LambdaRestApi (Matter of preference of company policy. What degree of abstraction do I want?)
cdk list

Shows you difference between your local and deployed on AWS.

cdk diff
cdk doctor

Destroy Stack from AWS

cdk destroy CdkStarterStack
const myL2Bucket = new Bucket(this, 'MyL2Bucket', {
  lifecycleRules: [{
    expiration: Duration.days(2)
  }]
});

new CfnOutput(this, 'MyL2BucketName', { // This outputs in AWS Outputs Section to debug once deployed.
  value: myL2Bucket.bucketName
});
new CfnParameter(this, 'duration', {
  default: 6,
  minValue: 1,
  maxValue: 10,
  type: 'Number'
});

Now, try firing this command:

cdk deploy --parameters duration=11

You will get an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment