AWS CDK Learning
The AWS Cloud Development Kit (AWS CDK) lets you define your cloud infrastructure as code in one of its supported programming languages. It is intended for moderately to highly experienced AWS users.
AWS 提供给用的工具,能使得开发者使用特定的网络接口来进行资源的创建。例如定义VPC Subnet的网络资源,Security Group NAT 网关及安全组,可以新建EC2 RDS 等服务器及数据库的实例。基本上能够通过控制台创建和管理的资源都能通过CDK指令来实现。
这个文档是提供给海外客户的,按客户要求使用英文写的,英文比较简单,我就不再翻译成中文了,只在细节处作一定注释
An AWS CDK app is an application written in TypeScript, JavaScript, Python, Java, C# or Go that uses the AWS CDK to define AWS infrastructure. An app defines one or more stacks. Stacks (equivalent to AWS CloudFormation stacks) contain constructs, each of which defines one or more concrete AWS resources, such as Amazon S3 buckets, Lambda functions, Amazon DynamoDB tables, and so on.
AWS CDK 提供了五种语言的官方库支持,官方提供的组件,能使得我们更好的管理资源和调用资源。
我们用代码开发,无论写的多么复杂,本质上也是请求AWS的api接口实现功能。本文是JAVA 语言的CDK入门demo,后面将使用Python编写较为复杂的部署需求。
The AWS CDK includes the CDK Toolkit (also called the CLI), a command-line tool for working with your AWS CDK apps and stacks. Among other functions, the Toolkit provides the ability to convert one or more AWS CDK stacks to AWS CloudFormation templates and related assets (a process called synthesis) and to deploy your stacks to an AWS account.
Install the AWS CDK
npm install -g aws-cdk
Check aws-cdk installed result
cdk --version
下载cdk的方法很多,我这里刚好有npm,使用npm下载。python可以通过pip下载
Create AWS CDK app (JAVA)
Now initialize the app using the cdk init command, specifying the desired template (“app”) and programming language. That is:
类似与Springboot或者Vue的手脚架,cdk帮我按照指定版本init出来一个项目,有最基础的demo代码和项目结构
cdk init app --language java
The cdk init command creates a number of files and folders inside the hello-cdk
directory to help you organize the source code for your AWS CDK app. Take a moment to explore. The structure of a basic app is all there; you’ll fill in the details in this tutorial.
Import Project
Use IDEA import the project , Import Project > Select Directory > Import Project form Maven
Maven loading the project
Maven loading completed
Set AWS Access Key
上文我们说到,我们要访问aws cdk的api接口,那么百分百会涉及到请求的token或者秘钥的概念,下文是设置cdk请求秘钥的过程,后续代码的请求都会带上验证信息
Login https://signin.aws.amazon.com/ , get Account Secret .
Config Local Computer
You may also manually create or edit the ~/.aws/config
and ~/.aws/credentials
(macOS/Linux) or %USERPROFILE%\.aws\config
and %USERPROFILE%\.aws\credentials
(Windows) files to contain credentials and a default region, in the following format.
-
In
~/.aws/config
or%USERPROFILE%\.aws\config
Singapore -
[default] region=ap-southeast-1
-
In
~/.aws/credentials
or%USERPROFILE%\.aws\credentials
[default] aws_access_key_id=AKIAI44QH8DHBEXAMPLE aws_secret_access_key=**************************************
Demo AWS CDK APP
Add an AWS Queue Resource
package com.myorg;
import software.amazon.awscdk.Duration;
import software.amazon.awscdk.services.sqs.Queue;
import software.constructs.Construct;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
public class AwsLearningStack extends Stack {
public AwsLearningStack(final Construct scope, final String id) {
this(scope, id, null);
}
/**
* this stack will create a queue in AWS
*
* scope : Tells the bucket that the stack is its parent: it is defined within the scope of the stack. You can define * constructs inside of constructs, creating a hierarchy (tree).
* Here, and in most cases, the scope is this (self in Python), meaning the construct that contains the bucket: the * stack.
* id: The logical ID of the Bucket within your AWS CDK app. This (plus a hash based on the queue's location within the * stack) uniquely identifies the bucket across deployments so the AWS CDK can update it if you change how it's defined * in your app.Here it is "AwsLearningQueue." Buckets can also have a name, which is separate from this ID (it's the * queueName property).
* props: A bundle of values that define properties of the bucket. Here we've defined only one property: versioned, which * enables versioning for the files in the bucket.
* */
public AwsLearningStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
// The code that defines your stack goes here
// create a queue,Duration 300 seconds
final Queue queue = Queue.Builder.create(this, "AwsLearningQueue")
.visibilityTimeout(Duration.seconds(300))
.build();
}
}
Synthesize an AWS CloudFormation template for the app, as follows.
cdk synth
The cdk synth
command executes your app, which causes the resources defined in it to be translated into an AWS CloudFormation template. The displayed output of cdk synth
is a YAML-format template; the beginning of our app’s output is shown below. The template is also saved in the cdk.out
directory in JSON format.
Deploying the Demo APP Stack
cdk deploy
ERROR:AwsLearningStack: SSM parameter /cdk-bootstrap/hnb659fds/version not found. Has the environment been bootstrapped? Please run ‘cdk bootstrap’ (see https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)
run cdk bootstrap to fix it
cdk bootstrap
deploy again !!!
cdk deploy
aws-cdk deploy completed
Now that you’re done with the quick tour, destroy your app’s resources to avoid incurring any costs from the bucket you created, as follows.
cdk destroy
stroy your app’s resources** to avoid incurring any costs from the bucket you created, as follows.
cdk destroy
destroy 是非常重要的过程,aws有免费和收费的资源,我后续demo尽量使用免费的资源,但是免费资源也是有限额的,使用时间过度,就会产生费用,以ec2为例,如果一台免费ec2实例,月运行时间超过750小时,也会产生费用。学习的过程中一定要destroy 销毁掉资源。