More Related Content
Similar to AWSのインフラはプログラミングコードで構築!AWS Cloud Development Kit 入門 (20)
More from Amazon Web Services Japan (20)
AWSのインフラはプログラミングコードで構築!AWS Cloud Development Kit 入門
- 1. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
2019.07.17
AWSのインフラはプログラミングコード
で構築!
AWS Cloud Development Kit 入門
Atsushi Fukui
Solutions Architect
DevOps Specialist
Amazon Web Services Japan K.K.
- 2. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
自己紹介
名前
福井 厚(ふくい あつし)fatsushi@
所属
アマゾン ウェブ サービス ジャパン株式会社
技術統括本部デベロッパーアドボカシー部
ソリューション アーキテクト
モダンアプリケーション開発スペシャリスト
前職
エンタープライズ アプリケーション開発コンサルタント
好きなAWSサービス
AWS Code シリーズ、AWS Cloud9、AWS CDK、AWS Lambda
- 3. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
このセッションの内容
• AWS Cloud Development Kitとは
• サンプルコーディング
• まとめ
- 4. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
インフラ構築の歴史
- 5. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
マニュアル作成(管理コンソール)
� 始めるのは簡単
� 繰り返し可能ではない
� エラーが起きやすい
� 時間がかかる
High
level
Low levelManual
- 6. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
スクリプト
� APIコールが失敗したら何が起こる?
� どうやってアップデートする?
� リソースが準備完了なのはどうやって知る?
� どうやってロールバックする?
Scripted
Manual
High
level
Low level
- 7. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
リソース プロビジョニング エンジン
AWS CloudFormation
template
(JSON/YAML)
HashiCorp
Configuration
Language (HCL)
望ましい状態の設定
Declarative
Scripted
Manual
High
level
Low
level
� 自動化が容易
� 再生成可能
� 設定の文法
� 抽象化なし、多数の詳細
- 8. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Document Object Models (DOMs)
Troposphere Python
SparkleFormation Ruby
GoFormation Go
…
if文、for ループ、IDE利用可能
例えば、TroposphereでVPCを作成するには128行必要
AWS
CloudFormati
on Template
� リアルコード ♥️
� 望ましい状態
� 抽象化は組み込まれていない
DOMs
Declarative
Scripted
Manual
High
level
Low
level
- 9. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Cloud Development Kit(AWS CDK) とは
- 10. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS CDK
- 11. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS CloudFormation
template
AWS CDK application
Stack(s)
Construct Construct
AWS CDK
Componentized
DOMs
Declarative
Scripted
Manual
High
level
Low
level
Resources
- 12. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS CDKは複数言語で利用可能
TypeScript、Python
Generally Available
Developer Preview
Java、C#
- 13. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
事前要件
• Node.js (>= 8.11.x)
• TypeScript >= 2.7
• Java 8 and Maven 3.5.4
• C# (.NET Core >= 2.0, .NET Framework >= 4.6.1
Mono >= 5.4)
• Python >= 3.6
- 14. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Demo
- 15. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
npm install -g aws-cdk
- 16. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
cdk init app --language
typescript
- 17. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
bin/cdk-worKshop.ts
#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import { CdkWorkshopStack } from '../lib/cdk-workshop-stack';
const app = new cdk.App();
new CdkWorkshopStack(app, 'CdkWorkshopStack');
- 18. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
lib/cdk-workshop-stack.ts
import sns = require('@aws-cdk/aws-sns');
import subs = require('@aws-cdk/aws-sns-subscriptions');
import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/core');
export class CdkWorkshopStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const queue = new sqs.Queue(this, 'CdkWorkshopQueue', {
visibilityTimeout: cdk.Duration.seconds(300)
});
- 19. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
lib/cdk-workshop-stack.ts
const topic = new sns.Topic(this, 'CdkWorkshopTopic');
topic.addSubscription(new subs.SqsSubscription(queue));
}
}
- 20. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
package.json
"name": "cdk-workshop",
"version": "0.1.0",
"bin": {
"cdk-workshop": "bin/cdk-
workshop.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"devDependencies": {
"@types/node": "8.10.45",
"typescript": "^3.3.3333",
"ts-node": "^8.1.0",
"aws-cdk": "^1.0.0"
},
"dependencies": {
"@aws-cdk/aws-sns": "^1.0.0",
"@aws-cdk/aws-sns-subscriptions":
"^1.0.0",
"@aws-cdk/aws-sqs": "^1.0.0",
"@aws-cdk/core": "^1.0.0"
}
}
- 21. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
cdk.json
{
"app": "npx ts-node bin/cdk-workshop.ts"
}
- 22. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
tsconfig.json
{
"compilerOptions": {
"target":"ES2018",
"module": "commonjs",
"lib": ["es2016", "es2017.object",
"es2017.string"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
”inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization":false
},
"exclude": ["cdk.out"]
}
- 23. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
cdk synth
- 24. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
出力されるCloudFormation Template
Resources:
CdkWorkshopQueue50D9D426:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 300
Metadata:
aws:cdk:path: CdkWorkshopStack/CdkWorkshopQueue/Resource
CdkWorkshopQueuePolicyAF2494A5:
Type: AWS::SQS::QueuePolicy
Properties:
PolicyDocument:
...
- 25. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS::CDK::Metadata
• AWS::CDK::MetadataリソースはToolkitによってすべてのス
タックに自動的に追加される
• これは生成されたスタックが利用しているCDKや利用してい
るライブラリのバージョンを分析するために、CDK チームに
よって利用される
• これによって、例えばセキュリティー上の問題などが発生し
た場合に重要な情報を利用者に通知することが可能になる
- 26. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
cdk bootstrap
cdk bootstrap
Bootstrapping environment aws://xxxxxxxxxxxx/ap-
northeast-1...
CDKToolkit: creating CloudFormation changeset...
Environment aws://xxxxxxxxxxxx/ap-northeast-1
bootstrapped (no changes).
• AWS CDKアプリを初めて環境(アカウント/リージョン)にデプロイする前
に、”bootstrap stack”をインストールする必要がある
- 27. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
cdk deploy
- 28. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
CloudFormationスタックを実行しリソースを作成
- 29. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
cdk diff
cdk diff
Stack CdkWorkshopStack
IAM Statement Changes
┌───┬─────────────────────────────────┬────────┬─────────────────┬─────────────────
────────────────┬──────────────────────────────────┐
│ │ Resource │ Effect │ Action │ Principal │
Condition │
├───┼─────────────────────────────────┼────────┼─────────────────┼─────────────────
────────────────┼──────────────────────────────────┤
│ - │ ${CdkWorkshopQueue50D9D426.Arn} │ Allow │ sqs:SendMessage │ Service:sns.amazonaws.com │ "ArnEquals":
{ │
│ │ │ │ │ │ "aws:SourceArn":
"${CdkWorksho │
│ │ │ │ │ │
pTopicD368A42F}" │
│ │ │ │ │ │ }
│
└───┴─────────────────────────────────┴────────┴─────────────────┴─────────────────
────────────────┴──────────────────────────────────┘
(NOTE: There may be security-related changes not in this list. See http://bit.ly/cdk-2EhF7Np)
Resources
[-] AWS::SQS::Queue CdkWorkshopQueue50D9D426 destroy
[-] AWS::SQS::QueuePolicy CdkWorkshopQueuePolicyAF2494A5 destroy
[-] AWS::SNS::Subscription CdkWorkshopQueueCdkWorkshopStackCdkWorkshopTopicD7BE96438B5AD106 destroy
[-] AWS::SNS::Topic CdkWorkshopTopicD368A42F destroy
- 30. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS CDK Concepts
- 31. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cloud Development Kit
App
• 実行可能プログラム
• CloudFormationテンプレートの
生成とデプロイに利用
Stack
• デプロイ可能な単位
• リージョンとアカウントを保持
Construct
• AWSリソースを表現
• 階層的な木構造を構成可能
- 32. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Construct
• AWS CDK アプリの基本ビルディングブロック
• Cloudコンポーネントを表し、CloudFormationが作成に必要
なすべてをカプセル化
• 単一のリソースを表現したり(Amazon S3 Bucketなど)、複
数のAWS CDKリソースをまたがるハイレベル コンポーネン
トを表現することも可能(モニタリングリソースとダッシュボー
ドを伴うクローンジョブなど)
- 33. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Construct Library
• low-level constructs – CFN リソース
• CloudFormationで利用可能なすべてのAWSリソース
• CfnXXX (例:s3.CfnBucketはAWS::S3::Bucketリソースを表す)
• CFNリソースを利用する場合は、すべてのリソースプロパティを明示的に
設定する必要がある
• High-level constructs – intent-based API
• AWSリソースを表すが、便利なデフォルト値や便利なメソッドを提供
• s3.Bucket.addLifeCycleRule()など
• Patterns
• 複数のリソースを含むAWSの一般的なタスクの実行
• aws-esc-patterns.LoadBalancedFargateServiceなど
- 34. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Composition
• Constructを通じて高度に抽象化されたパターンは
Compositionと呼ばれる
• CDK.AppをルートとしたConstructの階層構造を構築可能
• 再利用の単位として利用可能
- 35. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Constructに特定の機能がない場合の回避策
• CloudFormation Constructsを直接利用する
• すべてのCloudFormationリソース、プロパティと1:1で対応する自動
生成されたCFNリソース(S3.CfnBucketなど)を利用可能
• 対応するCFNリソースがまだ作成されていない場合
• cdk.CfnResourceクラスを利用する
• Constructsの背後にあるCFNリソースを変更する
• addOverride、addDeleteOverrideメソッドで直接上書きする
• CloudFormationカスタムリソースを実装する
- 36. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
参考資料
• AWS Cloud Development Kit (AWS CDK) Developer Guild
https://docs.aws.amazon.com/cdk/latest/guide/home.html
• CDK Tools
https://docs.aws.amazon.com/cdk/latest/guide/tools.html
• AWS CDK API Reference
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html
• aws-cdk-samples
https://github.com/aws-samples/aws-cdk-examples#TypeScript
• Workshop
https://cdkworkshop.com/