项目作者: joesustaric

项目描述 :
:cloud: Playing with AWS CDK
高级语言: JavaScript
项目地址: git://github.com/joesustaric/cdk-playground.git
创建时间: 2020-08-12T23:54:33Z
项目社区:https://github.com/joesustaric/cdk-playground

开源协议:

下载


CDK Playground

AWS have recently released CDK. I want to play with it.

Use the AWS CDK to define your cloud resources in a familiar programming language. The AWS CDK supports TypeScript, JavaScript, Python, Java, and C#/.Net.

More info here.

Useful commands

  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template

Install CDK

  1. # requires Node.js ≥ 10.13.0
  2. # !! versions 13.0.0 to 13.6.0 are not supported !!
  3. $ npm i -g aws-cdk
  4. # To create a new js project in a new blank dir run..
  5. $ cdk init app --language javascript

Managing AWS Construct Library Modules

AWS Construct Library modules are named like @aws-cdk/SERVICE-NAME

  1. # Install
  2. npm install @aws-cdk/aws-s3 @aws-cdk/aws-lambda
  3. # Update
  4. npm update

CDK Key Concepts

Constructs

Basic building blocks. Represent a single Resource (eg S3) or higher-level component of multiple AWS CDK Resources.
CDK includes a Construct library which includes ‘levels’ of Constructs. Deails here.

  • Level 1 (L1), Direct mappings to AWS Resources available in CF. e.g. s3.CfnBucket Here you will need to explicity configure all resource properties.

  • Level 2 (L2), AWS Resources but higher level, e.g. s3.Bucket but with additional properties and methods. e.g. bucket.addLifeCycleRule()

  • Patterns, even higher level AWS Resources! Things that often involve more AWS Resources. e.g aws-apigateway.LambdaRestApi represents API Gateway backed by a Lambda function.

App

Application written in CDK supported languages to define AWS infrastructure. One or more stacks. More info here.

e.g. Define a Stack..

  1. class MyFirstStack extends Stack {
  2. constructor(scope, id, props) {
  3. super(scope, id, props);
  4. new s3.Bucket(this, 'MyFirstBucket');
  5. }
  6. }

Add the Stack construct to the App construct. App is the root of the Construct tree.

  1. const app = new App();
  2. new MyFirstStack(app, 'hello-cdk');
  3. app.synth();

App Lifecycle

Summarised from here.

  1. Construction

Instantiate all the defined constructs + link them together. Most of the app code is executed.

  1. Preparation

Constructs that have implimented the prepare() go through another round to set up their final state (tranparent / no user feedback) Not recommended to use the prepare hook. It could impact behaviour.

  1. Validation

Constructs that have implimented the validate() can self validate to ensure they’re in a state to deploy. Recommended to perform validation as soon as possible. Better error feedback.

  1. Synthesis

app,synth() traverses the construct tree and invokes this method on all constructs. The output from constructs that impliment synthesize method can emit deployment artifacts.. eg CFN templates, docker image assets etc.. Most cases you might not need to use this.

  1. Deployment

The CDK CLI Takes deploy artifacts and deploys it to AWS. Uploads assets and begins CFN deployment to create resources.

  1. +-------------------------------------------------------------------------------------+
  2. | |
  3. | CDK CLI |
  4. | |
  5. +---------------------------+---------------------------------------------+-----------+
  6. | |
  7. | | sends output
  8. | | to CloudFormation
  9. |Calls your v
  10. | App +----CloudFormation----+
  11. | | |
  12. v | +-----------------+ |
  13. +---------------------------+------------------------------+ | | | |
  14. | CDK APP | | | Deploy | |
  15. | | | | | |
  16. +-----------+ | +---------+ +-------+ +--------+ +----------+ | | +-----------------+ |
  17. | | | | | | | | | | | | +----------------------+
  18. | CDK App +-----> |Construct+--->+Prepare+---->+Validate+---->+Synthesize| | ^
  19. |Source Code| | | | | | | | | | | |
  20. +-----------+ | +---------+ +-------+ +--------+ +----------+ | +----------+------+
  21. | +-->+ |
  22. +----------------------------------------------------------+ | Template |
  23. ++ other artifacts|
  24. | |
  25. +-----------------+

Testing Concepts

Copy Pastaed from here.

Snapshot tests

Test the synthesized AWS CloudFormation template against a previously-stored “golden master” template. This way, when you’re refactoring your app, you can be sure that the refactored code works exactly the same way as the original. If the changes were intentional, you can accept a new master for future tests.

Fine-grained assertions

test specific aspects of the generated AWS CloudFormation template, such as “this resource has this property with this value.” These tests help when you’re developing new features, since any code you add will cause your snapshot test to fail even if existing features still work. When this happens, your fine-grained tests will reassure you that the existing functionality is unaffected.

Validation

tests help you “fail fast” by making sure your AWS CDK constructs raise errors when you pass them invalid data. The ability to do this type of testing is a big advantage of developing your infrastructure in a general-purpose programming language.

  • Examples of all these above

TODOs

  • figure out good stack dir structure
  • figure out how good is the testing lib? read this
  • example of L1 vs L2 components.
  • secrets managment read this
  • ci / cd read this
  • stack drifts
  • IAM roles

TO DO later…

Things I like things I don’t like..