More Related Content
Similar to [AWSマイスターシリーズ] AWS CloudFormation
Similar to [AWSマイスターシリーズ] AWS CloudFormation (20)
More from Amazon Web Services Japan
More from Amazon Web Services Japan (20)
[AWSマイスターシリーズ] AWS CloudFormation
- 1. re:G
ene
rate
AWS マイスターシリーズ
AWS CloudFormation
2013.11.27
アマゾン データ サービス ジャパン株式会社
ソリューションアーキテクト 吉荒 祐⼀一
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 2. 2013年年
CloudFormationのアップデート
! 2013/2/7
• Amazon S3 と Amazon RDSのタグ付けが可能に
! 2013/2/20
• EBS-‐‑‒Optimized EC2インスタンスのプロビジョニング
• Auto Scaling Groupのローリングデプロイ
• スタックアップデートのキャンセル
! 2013/8/12
• 並列列スタック処理理とネストされたスタックの更更新の追加
! 2013/9/17
• 追加のVPCサポートと新テンプレート
! 2013/10/14
• フェデレーテッドユーザーおよび⼀一時的なセキュリティ認証情報が
利利⽤用可能に
2
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 3. Agenda
! CloudFormationの概要
! スタック
! テンプレート
! Cloud-‐‑‒init
! CloudFormation helpers
! CloudFormer
! その他Tips
! まとめ
3
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 4. ! CloudFormationの概要
4
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 5. AWS CloudFormation
! EC2やELBなどを使ったAWSサービスのシステム構築を、
設定ファイル(テンプレート)を元に⾏行行えるサービス
! テンプレートを⾃自由に作成できるため、⾃自分好みのシス
テム構成が可能
! テンプレートは、AWSのサービスを操るための新しい⾔言
語
5
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 6. AWS CloudFormationのイメージ
テンプレートに基づき
各サービスが起動
スタック
Elastic Load Balancing
テンプレート
S3
Cloud
Formation
SNS
EC2
EC2
Auto Scaling
6
CloudWatch
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 7. 利利⽤用シーンと利利点
! ⼀一度度テンプレートを作成すれば、同じ構成を再現できる
• 開発環境の構築
• Blogシステム、Webシステム、ゲームプラットフォームなど、
同じ仕組みでアプリやデータが異異なるようなもの
! ベストプラクティスが盛り込まれたテンプレートが使⽤用
可能
• システムアーキテクチャの再利利⽤用
• WordPress, Redmineなどが⼊入った、多くのサンプルテンプ
レートが提供済み
! 起動時にパラメータを渡せる
• 例例えばDBのエンドポイントをEC2に渡せる
7
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 8. 利利⽤用料料⾦金金
! CloudFormationの利利⽤用⾃自体は無料料
! テンプレートに従って構築された各AWSサービスに対し
て課⾦金金
8
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 10. CloudFormationに適した場⾯面
! 幅広いリソースを使い環境構築する場合
• エンタープライズアプリケーション
• レガシーアプリケーション
• 他の⾃自動化サービスではサポートされないリソースを使⽤用
! ElasticBeanstalkと組み合わせて構築する場合
• リソースのひとつとして、ElasticBeanstalkをサポート
10
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 11. ! スタック
11
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 12. スタック
! リソース(EC2インスタンスやS3バケット、RDSインスタンスな
ど)の集合のこと
! スタック単位でリソースの管理理が可能。スタック破棄を実⾏行行すると、
スタックにひもづくリソースを破棄することが可能
! 使⽤用するリソースおよびリソースの構築順は、テンプレートの依存
関係で決定
スタック
これらを⼀一度度に⽣生成・破棄可能
App
Serve
r
12
Web
Serve
r
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 13. スタック可能なリソースタイプ
! Amazon EC2 インスタンス
! Amazon EC2 セキュリティグループ
! Amazon Elastic Block Store(EBS)
ボリューム
! Elastic Load Balancers
! Amazon EC2 Elastic IP アドレス
! Auto Scaling グループ
! Auto Scaling ポリシー
! Amazon RDS
データベースインスタンス
! Amazon RDS パラメータグループ
! Amazon RDS セキュリティグループ
! Amazon DynamoDB
! Amazon SimpleDB ドメイン
! Amazon SQS キュー
! Amazon SNS トピック
! Amazon SNS サブスクリプション
13
!
!
!
!
!
!
!
!
!
!
AWS Elastic Beanstalk
Amazon CloudWatch アラーム
Amazon Route 53 DNS レコード
Amazon CloudFront
ディストリビューション
Amazon S3 バケット
AWS Identity and Access
Management ユーザー、グループ、
およびポリシー
Amazon ElastiCache
キャッシュクラスタ
Amazon ElastiCache
キャッシュセキュリティグループ
Amazon ElastiCache
キャッシュパラメータグループ
Amazon Virtual Private Cloud VPC、
サブネット、ゲートウェイ、ルートテー
ブル、およびネットワーク ACL
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 14. スタック構築⽅方法
! AWS Management Consoleから構築
! コマンドラインツール
• AWS Command Line Tool
• http://aws.amazon.com/cli/
! 各種SDK
Java : http://aws.amazon.com/jp/sdkforjava/
.NET : http://aws.amazon.com/jp/sdkfornet/
PHP : http://aws.amazon.com/jp/sdkforphp/
Ruby :
http://docs.aws.amazon.com/AWSSdkDocsRuby/latest/
DeveloperGuide/ruby-‐‑‒dg-‐‑‒setup.html
• JavaScript :
http://aws.amazon.com/documentation/sdkforjavascript/
• Python : http://docs.pythonboto.org/en/latest/
•
•
•
•
14
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 15. スタック構築⽅方法:もうひとつの視点
! 既存のテンプレートから構築
• ローカルファイル
• S3上のファイル
! サンプルテンプレートから構築
• http://aws.amazon.com/cloudformation/aws-‐‑‒
cloudformation-‐‑‒templates/
! ゼロから新しいものを作る
! テンプレートはテキストファイルとして作成、管理理、共
有可能
15
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 16. AWS Management Consoleから構築-‐‑‒1
16
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 20. AWS Management Consoleから構築-‐‑‒5
20
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 23. AWS Management Consoleから構築-‐‑‒8
23
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 27. ! テンプレート
27
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 28. {
"AWSTemplateFormatVersion" : "2010-09-09",
テンプレート
"Description" : “Sample",
"Parameters" : {
"KeyName" : {
"Description" : “Sample key,
"Type" : "String"
}
},
"Mappings" : {
},
! CloudFormationの⼼心臓部
! スタック構築の設計図
! JSONフォーマットで記述
"Resources" : {
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
"KeyName" : { "Ref" : "KeyName" },
"ImageId" : { "Fn::FindInMap" : [
"RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]}
}
},
"InstanceSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH access via port 22",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : "0.0.0.0/0"
}]
}
}
}
28
}
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 29. テンプレート解説
"AWSTemplateFormatVersion" : “2010-09-09",
テンプレートのバージョン
(最新は2010-‐‑‒09-‐‑‒09)
"Description" : "Valid JSON strings up to 4K",
テンプレートの詳細(説明⽂文)
{
"Parameters" : {
set of parameters
},
"Mappings" : {
set of mappings
},
“Conditions" : {
set of conditions
},
"Resources" : {
set of resources
},
"Outputs" : {
set of outputs
}
}
29
CloudFormation実⾏行行時に
後で変更更可能なパラメータを列列挙
(例例:DBユーザー名など)
Hashtableのようなもの
キーに応じて値を特定出来る
(例例:リージョンに応じたAMI番号など)
Parameters、他のCondition、Mappingから
条件判断し結果に応じてリソースを作成可能
EC2やRDSなど、スタックを構成する
リソースを定義
スタック構築後に取得したい値
(例例:アクセスURLなど)
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 30. Parameters
{
"AWSTemplateFormatVersion" : “2010-09-09",
"Description" : "Valid JSON strings up to 4K",
"Parameters" : {
set of parameters
},
"Mappings" : {
set of mappings
},
“Conditions" : {
set of conditions
},
"Resources" : {
set of resources
},
CloudFormation実⾏行行時に
後で変更更可能なパラメータを列列挙
(例例:DBユーザー名など)
"Outputs" : {
set of outputs
}
}
30
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 31. Parametersとは
! スタック構築時に値の⼊入⼒力力が可能
! データ型、デフォルト値、最⼩小最⼤大値など設定可能
"Parameters" : {
"Age":{
"Type":"Number",
“Default” : “30”,
“MinValue”: “20”,
“MaxValue”: “60”,
"Description":"input your age.“
},
"FirstName":{
"Type":"String",
"Description":"input your first name.“
},
}
31
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 32. Parametersのプロパティ
! 利利⽤用出来るプロパティ
プロパティ
Type
データ型 “String” “Number” “CommaDelimitedList”
Default
デフォルト値
NoEcho
⼊入⼒力力時に*****となる(パスワードなどに使⽤用)
AllowedValues
⼊入⼒力力可能値の⼀一覧指定 (例例:[“true”,”false”] )
AllowedPattern
正規表現で⼊入⼒力力可能パターンを指定(例例:[a-‐‑‒zA-‐‑‒Z]*)
MaxLength
最⼤大⽂文字数
MinLength
最⼩小⽂文字数
MaxValue
最⼤大値
MinValue
最⼩小値
Description
プロパティの詳細説明
ConstraintDescription
32
内容
⼊入⼒力力した値がAllowedPatternやMaxLengthなどの制約に
引っかかった時に表⽰示する説明
(どのような制約があるかの説明を記述)
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 33. Parametersの参照
! ⼊入⼒力力したパラメータ値は、テンプレート中で “Ref”を使
⽤用して参照可能
! ユーザー名、パスワード、ドメインなどの可変部分に便便
利利
ここにパラメータ⼊入⼒力力値が反映される
“Resources" : {
“MyServer":{
“Type":“AWS::EC2::Instance",
“Properties” : {
“KeyName”: { “Ref” : “Age”},
“Tags” : { “Ref” : “FirstName”}
}
}
}
33
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 34. !
!
!
!
!
!
!
!
Function
Ref
• パラメータを参照する
Fn::Base64
• ⽂文字列列をBase64エンコードする
Fn::FindInMap
• Mapから値を取り出す
Fn::GetAtt
• リソースから属性を取り出す
• 例例:"Fn::GetAtt" : [ "MyELB" , "DNSName"]
Fn::GetAZs
• 指定リージョンのアベイラビリティゾーンを返す
Fn::Join
• ⽂文字列列の連結
• “Fn::Join” : [ “:”, [ “a”, “b”, “c” ] ] は 「a:b:c」を返す
• “:”がセパレータ。不不要な場合は ”” を定義
Fn::Select
• Index値に従って、リストから値を返す
• 例例:{ “Fn::Select” : [ “1”, [ “apples”, “grapes”, “oranges”, “mangoes” ] ] }
“grapes”を返す
Condition Functions
• Fn::If, Fn::Equals, Fn::NotのようなFunctionにより、条件によるスタック作成を可能に
http://docs.amazonwebservices.com/AWSCloudFormation/latest/UserGuide/
index.html?intrinsic-‐‑‒function-‐‑‒reference.html
34
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 35. Pseudo Parameter
! 実⾏行行リージョンやスタック名といったパラメータを取得する予約語
• { “Ref” : “AWS::Region” } のように値を取得可能
! AWS::AccountId
! AWS::NotificationARNs
• notification Amazon Resource Names (ARNs)のリスト取得
! AWS::NoValue
“Resources" : {
• Fn::If Functionにて使⽤用
“MyServer":{
“Type":“AWS::EC2::Instance",
! AWS::Region
“Properties” : {
“KeyName”: { “Ref” : “AWS::StackName”},
! AWS::StackId
“Tags” : { “Ref” : “AWS::Region”}
! AWS::StackName
}
}
}
http://docs.amazonwebservices.com/AWSCloudFormation/latest/UserGuide/
index.html?pseudo-‐‑‒parameter-‐‑‒reference.html
35
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 36. Mappings
{
"AWSTemplateFormatVersion" : “2010-09-09",
"Description" : "Valid JSON strings up to 4K",
"Parameters" : {
set of parameters
},
"Mappings" : {
set of mappings
},
“Conditions" : {
set of conditions
},
"Resources" : {
set of resources
},
Hashtableのようなもの
キーに応じて値を特定出来る
(例例:リージョンに応じたAMI番号など)
"Outputs" : {
set of outputs
}
}
36
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 37. Mappingsとは
! キーとバリューのテーブル
! 例例えば、⼊入⼒力力値やリージョンによって値が変わるような
ものを決めるような使い⽅方
"Mappings" : {
"RegionTable" : {
"us-east-1" : {
"AMI" : "ami-8c1fece5“,
“Key” : “myKey-east”
},
"us-west-1" : {
"AMI" : "ami-3bc9997e“,
“Key” : “myKey-west”
},
"ap-northeast-1" : {
"AMI" : "ami-300ca731“,
“Key” : “myKey-japan”
}
}
},
37
マッピングを定義
(この例例の場合、”RegionTable”という
マッピングを定義)
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 38. Mappingsの使い⽅方
! “Fn:FindInMap”で値を取得
! "Fn::FindInMap" : [ "MapName", "Key", "Value"]
! MapName,key,Valueには”Ref”が利利⽤用可能
"Mappings" : {
"RegionTable" : {
"us-east-1" : {
"AMI" : "ami-8c1fece5“,
“Key” : “myKey-east”
},
"us-west-1" : {
"AMI" : "ami-3bc9997e“,
“Key” : “myKey-west”
},
"ap-northeast-1" : {
"AMI" : "ami-300ca731“,
“Key” : “myKey-japan”
}
}
},
38
“Resources" : {
“MyServer":{
“Type":“AWS::EC2::Instance",
“Properties” : {
“ImageId”: {
“Fn::FindInTemplate”
“Fn:FindInMap” : [
“RegionTable” ,
{ “Ref” : “region” },
“AMI”]
},
}
}
この例例だと、⼊入⼒力力された
}
regionパラメータによって
利利⽤用するAMIのIDを決定できる
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 39. Conditions
{
"AWSTemplateFormatVersion" : “2010-09-09",
"Description" : "Valid JSON strings up to 4K",
"Parameters" : {
set of parameters
},
Parameters、他のCondition、Mappingから
条件判断し結果に応じてリソースを作成可能
"Mappings" : {
set of mappings
},
“Conditions" : {
set of conditions
},
"Resources" : {
set of resources
},
"Outputs" : {
set of outputs
}
}
39
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 40. Conditionsの使い⽅方
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Mappings" : {
"RegionMap" : {
<一部省略>
}
},
"Parameters" : {
"EnvType" : {
EnvTypeパラメータが”prod”と等しければ
"Description" : "Environment type.",
“CreateProdResources”条件が成⽴立立
"Default" : "test",
"Type" : "String",
"AllowedValues" : ["prod", "test"],
"ConstraintDescription" : "must specify prod or test."
}
},
"Conditions" : {
"CreateProdResources" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "prod"]}
},
"Resources" : {
"EC2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]}
}
},
"MountPoint" : {
"Type" : "AWS::EC2::VolumeAttachment",
"Condition" : "CreateProdResources",
"Properties" : {
"InstanceId" : { "Ref" : "EC2Instance" },
© 2013 Amazon.com, Inc. and its affiliates. All rights
"VolumeId" : { "Ref" : "NewVolume" }, reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
40
- 41. "Parameters" : {
"EnvType" : {
"Description" : "Environment type.",
"Default" : "test",
"Type" : "String",
"AllowedValues" : ["prod", "test"],
"ConstraintDescription" : "must specify prod or test."
}
},
"Conditions" : {
"CreateProdResources" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "prod"]}
},
"Resources" : {
EnvTypeパラメータが”prod”と等しければ
"EC2Instance" : {
"Type" : "AWS::EC2::Instance",
“CreateProdResources”条件が成⽴立立
"Properties" : {
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]}
}
},
“CreateProdResources”条件が成⽴立立して
"MountPoint" : {
"Type" : "AWS::EC2::VolumeAttachment",
いる
"Condition" : "CreateProdResources",
場合にのみリソース⽣生成
"Properties" : {
"InstanceId" : { "Ref" : "EC2Instance" },
"VolumeId" : { "Ref" : "NewVolume" },
"Device" : "/dev/sdh"
}
},
“CreateProdResources”条件が成⽴立立して
"NewVolume" : {
いる
"Type" : "AWS::EC2::Volume",
"Condition" : "CreateProdResources",
場合にのみリソース⽣生成
"Properties" : {
"Size" : "100",
"AvailabilityZone" : { "Fn::GetAtt" : [ "EC2Instance", "AvailabilityZone" ]}
}
}
}
}
Conditionsの使い⽅方
41
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 42. Resources
{
"AWSTemplateFormatVersion" : “2010-09-09",
"Description" : "Valid JSON strings up to 4K",
"Parameters" : {
set of parameters
},
"Mappings" : {
set of mappings
},
“Conditions" : {
set of conditions
},
"Resources" : {
set of resources
},
EC2やRDSなど、スタックを構成する
リソースを定義
"Outputs" : {
set of outputs
}
}
42
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 43. Resourcesとは
! EC2やELB,RDSなど、起動するサービスを設定
! リソース毎に決められたパラメータを設定する
“Resources" : {
“MyWebServer" : {
“Type" : “AWS:EC2::Instance”
“Properties" : {
“KeyName" : "ami-8c1fece5“,
“ImageId” : “myKey-east”
},
"us-west-1" : {
"AMI" : "ami-3bc9997e“,
“Key” : “myKey-west”
},
"ap-northeast-1" : {
"AMI" : "ami-300ca731“,
“Key” : “myKey-japan”
}
}
},
43
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 44. EC2+SecurityGroupの例例
"Resources" : {
"Ec2Instance" : {
リソースタイプ
"Type" : "AWS::EC2::Instance",
"Properties" : {
"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
"KeyName" : { "Ref" : "KeyName" },
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]}
}
},
"InstanceSecurityGroup" : {
リソース毎のプロパティ
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH access via port 22",
"SecurityGroupIngress" : [ {
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : "0.0.0.0/0"
}]
}
}
}
44
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 46. Attributes
! リソースの振る舞いや関係を制御する
! DeletionPolicy
• スタックを削除する際のリソースの処理理
• Delete (Default) / Retain / Snapshot
! DependsOn
• 指定したリソースが⽣生成された後に⽣生成
• AWS::CloudFormation::WaitCondition、
AWS::CloudFormation::WaitConditionHandleと併⽤用
! Metadata
! UpdatePolicy
• AWS::AutoScaling::AutoScalingGroupリソースのみ
• Auto ScalingのRolling Updateでの挙動を制御
• MaxBatchSize
• MinInstancesInService
• PauseTime
46
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 47. Outputs
{
"AWSTemplateFormatVersion" : “2010-09-09",
"Description" : "Valid JSON strings up to 4K",
"Parameters" : {
set of parameters
},
"Mappings" : {
set of mappings
},
“Conditions" : {
set of conditions
},
"Resources" : {
set of resources
},
"Outputs" : {
set of outputs
}
}
47
スタック構築後に取得したい値
(例例:アクセスURLなど)
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 48. Outputsとは
! スタック構築後に取得・表⽰示したい情報の定義
! 例例えばアクセスURLや、DBの通信先情報、作ったIAM
ユーザー名など、あとで使⽤用するもの
! マネジメントコンソールから確認できるので、スタック
に関する情報を出⼒力力すると便便利利(キーペア名、AZ、イ
ンスタンスIDなど)
48
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 49. Outputの定義
出⼒力力したい名称を定義
"Outputs" : {
"InstanceId" : {
説明
"Description" : "InstanceId of the newly created EC2 instance",
"Value" : { "Ref" : "Ec2Instance" }
},
値
"AZ" : {
"Description" : "Availability Zone of the newly created EC2 instance",
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "AvailabilityZone" ] }
},
"ApplicationURL" : {
"Description" : "URL of running web application",
"Value" :
{ "Fn::Join":
["", [ "http://", { "Fn::GetAtt": [ "Ec2Instance" , "PublicIp"] } , "/index.html"] ] }
}
Functionを使って⽂文字列列を加⼯工
}
49
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 50. Outputの定義
"Outputs" : {
"InstanceId" : {
"Description" : "InstanceId of the newly created EC2 instance",
"Value" : { "Ref" : "Ec2Instance" }
},
"AZ" : {
"Description" : "Availability Zone of the newly created EC2 instance",
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "AvailabilityZone" ] }
},
"ApplicationURL" : {
"Description" : "URL of running web application",
"Value" :
{ "Fn::Join":
["", [ "http://", { "Fn::GetAtt": [ "Ec2Instance" , "PublicIp"] } , "/index.html"] ] }
}
}
50
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 51. テンプレートのまとめ
! Parameters,Mappings,Resources,Outputsの4つのブ
ロックに定義を記述
! 可変項⽬目はパラメーター化したり、Mappingで定義
! Functionsを使⽤用して、参照や⽂文字連結などの操作が可
能
! Resourceのパラメータはリソースタイプにより異異なる。
ドキュメントを利利⽤用して定義
• http://docs.amazonwebservices.com/
AWSCloudFormation/latest/UserGuide/index.html?aws-‐‑‒
template-‐‑‒resource-‐‑‒type-‐‑‒ref.html
51
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 52. テンプレートTips
! リソース定義にEC2インスタンスが必須ではない
• DB構築だけ、オートスケールだけ、アラーム設定だけでもOK
! 参照関係があると、依存関係があるとみなされる。相互
参照や循環参照はNG(エラーとなる)
! 参照関係がある部分は、参照の整合性が取れるよう起動
順をCloudFormationが決定
! スタックを破棄すると、スタックにひもづくリソースは
破棄される
• ただし”DeletionPolicy”を”Retain”にすると破棄されない
"Resources" : {
"myS3Bucket" : {
"Type" : "AWS::S3::Bucket",
"DeletionPolicy" : "Retain"
}
}
52
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 53. テンプレートTips
! リソース数が多いと、何のリソースか分からなくなるの
で、Tagsプロパティを使⽤用する
"MyInstance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"SecurityGroups" : [ { "Ref" : "MySecurityGroup" } ],
"ImageId" : "ami-20b65349",
"Tags" : [
{ "Key" : “Name", "Value" : “MyInstance" }
]
}
}
53
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 55. ⼊入れ⼦子テンプレート
! テンプレートからテンプレートの呼び出しが可能
! リソースタイプに “AWS::CloudFormation::Stack“を指定
! TemplateURLを指定すると、そのテンプレートを読み込み
"Resources" : {
"WebServer" : {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"TemplateURL" : “https://s3.amazonaws.com/hoge.template" ]]},
"Parameters" : {
"KeyName" :
{ "Ref" : "KeyName" },
"InstanceType" : { "Ref" : "InstanceType"},
}
},
55
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 56. AWS::CloudFormation::Stack
! AWS::CloudFormation::Stack を使⽤用してスタックを
上位のテンプレートのリソースとしてネストする
! トップレベルのスタックとネストされたスタックを⼀一緒
に更更新可能
! ネストされたスタックの更更新
• トップレベルのスタックでUpdateStackコマンドを実⾏行行
• Management Consoleで、トップレベルのスタックを選択
し、"Update Stack"をクリック
• CloudFormationはトップレベルのテンプレートだけでなく、ネ
ストされたテンプレートに対する更更新を検出し、テンプレート
が変更更されたスタックのみを更更新
56
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 57. ! Cloud-‐‑‒init
57
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 58. Cloud-‐‑‒init
! Amazon Linuxに⼊入っている、初期設定ツール
• 元々はUbuntuで作られたもの
! EC2インスタンスの起動時に、「userdata」として設定
したスクリプトを呼び出す。
• 「#!」 で始まる⽂文字列列の場合のみ、スクリプトを実⾏行行
! スクリプトを記述できるので、ロケール変更更、アプリイ
ンストール、ファイル書き換えなどが可能
! RHEL
• http://aws.amazon.com/articles/2519228458631605
! Windows
• EC2ConfigService
58
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 60. Cloud-‐‑‒initの特⻑⾧長と制限
! 特⻑⾧長
! インスタンス起動時に、インスタンスの初期化が可能
• AMIとユーザーデータは分離離できるので、AMIの作り込みが不不要
になる
• Apacheインストール済みのAMIを⽤用意すると、OSやApacheの
バージョンが上がったときに再作成になる
! 制限
! userdataは⼀一度度定義すると、書き換えは不不可能
• 書き換えて再実⾏行行が出来ない(後述のスタック更更新に使えない)
! 設定できるデータ量量は、16KByteまで
60
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 61. CloudFormationとの連携
! EC2リソースのプロパティ「UserData」に内容を記載
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId" : "ami-dcfa4edd",
"KeyName"
: { "Ref" : "KeyName" },
"UserData"
: { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bashn",
"log='/tmp/init.log'n",
"apt-get update &>> $logn",
"apt-get install -y ruby &>> $logn",
"cd /tmpn",
"wget http://rubygems.org/rubygems.tgz &>> $logn",
"tar zxf rubygems.tgz &>> $logn",
"cd rubygemsn",
"ruby setup.rb --no-format-executable &>> $logn",
]]
}
}
}
61
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 62. CloudFormationとの連携
! CloudFormationでUserDataを定義すると、定義中に
RefやFn::GetAttrを使⽤用することが可能
! 例例えばRDSのEndPointをUserDataに埋め込むといった
ことが出来る
62
"Resources" : {
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId" : "ami-dcfa4edd",
"UserData"
: { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bashn",
"cd /home/ec2-usern",
{ "Fn::Join" : ["", [
"echo ", {"Fn::GetAtt" : ["DBInstance", "Endpoint.Address"]} , "> db.confn"
]]}
]]}
} } },
"DBInstance" : {
"Type": "AWS::RDS::DBInstance",
}
}
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 63. ! CloudFormation helpers
63
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 64. CloudFormation helpers
! EC2インスタンス初期化時のヘルパースクリプト群
! 利利⽤用⽅方法
•
•
•
Amazon Linux AMI
Yumリポジトリ
その他下記より
• https://s3.amazonaws.com/cloudformation-‐‑‒examples/aws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒
latest.amzn1.noarch.rpm
• https://s3.amazonaws.com/cloudformation-‐‑‒examples/aws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒
latest.tar.gz
• https://s3.amazonaws.com/cloudformation-‐‑‒examples/aws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latest.zip
• https://s3.amazonaws.com/cloudformation-‐‑‒examples/aws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latest.msi
• https://s3.amazonaws.com/cloudformation-‐‑‒examples/aws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒
latest.src.rpm
! /opt/aws/bin に格納
•
•
•
•
cfn-‐‑‒init
cfn-‐‑‒get-‐‑‒metadata
cfn-‐‑‒signal
cfn-‐‑‒hup
! Python製
64
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 65. cfn-‐‑‒init
! パッケージやファイルのダウンロード、ファイルの⽣生成を⾏行行うスク
リプト
! 実⾏行行のための設定は、Metadata領領域に定義
• AWS::CloudFormation::Init
• Configsets、Commands、Files、Groups、Packages、Services、
Sources、Users
"Resources": {
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
source,package,files,services
"sources" : { : },
の順に実⾏行行される
"packages" : { : }
"files" : { : }
"services" : { : } }
}
},
"Properties": { : }
}
65 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 66. ! sources
cfn-‐‑‒initの設定
• 指定したURLからファイルをダウンロードして展開
• Tar,zip,tar+gzip,tar+bz2
"sources" : {
"/etc/myapp" : "https://s3.amazonaws.com/mybucket/myapp.tar.gz"
}
! packages
§ パッケージシステムを使⽤用してアプリをインストール
§ Apt,yum,rubygems,python,rpm
"packages" : {
最新版
"yum" : {
"httpd" : [],
"php" : [],
バージョン
},
指定
"rubygems" : {
"chef" : [ "0.10.2" ],
}
}
66 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 67. ! files
cfn-‐‑‒initの設定
• 指定パスへのファイルの⽣生成
"files" : {
ファイルパス
"/app/db.conf" : {
"content" : { "Fn::Join" : ["", [
"db.name=", {"Ref" : "DBName"}, "n",
ファイルの中⾝身
"db.user=", {"Ref" : "DBUser"}, "n",
を⽣生成
"db.pass=", {"Ref" : "DBPassword" },"n",
"db.host=", {"Fn::GetAtt" : ["DBInstance", "Endpoint.Address"]}, "n",
]] },
"mode" : "000644",
"owner" : "root",
"group" : "root"
}
S3などから取得
"/etc/myapp/myapp-init.pp" : {
"source" : "https://s3.amazonaws.com/myapp/myapp-init.pp",
"mode" : "100644",
"owner" : "root",
"group" : "wheel"
}
}
67 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 68. ! services
cfn-‐‑‒initの設定
• サービスの起動・再起動の指定
• 現在sysvinitのみ
OS起動時に起動さ
"services" : {
せるかどうか
"sysvinit" : {
"nginx" : {
"enabled" : "true",
cfn-‐‑‒init終了了後に起動するかどうか
"ensureRunning" : "true",
"files" : ["/etc/nginx/nginx.conf"],
"sources" : ["/var/www/html"]
設定ファイルもしくは指定ディ
},
レクトリのファイルが変わって
"php-fastcgi" : {
いた場合にサービス再起動する
"enabled" : "true",
"ensureRunning" : "true",
"packages" : { "yum" : ["php", "spawn-fcgi"] }
}
Cfg-‐‑‒initでパッケージを⼊入れて
}
いたらサービス再起動
68
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 69. cfn-‐‑‒initの起動
! cfn-‐‑‒initはデフォルトでは動作しない
! User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述して、
cloud-‐‑‒init経由で起動
! 引数にスタック名、リソース名、リージョン、アクセス
キー、シークレットキーを指定
"Properties": {
"UserData"
: { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bashn",
"/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackName" }, " -r WebServer ",
"
--access-key ", { "Ref" : "HostKeys" },
"
--secret-key ", {"Fn::GetAtt": ["HostKeys", "SecretAccessKey"]},
"
--region ", { "Ref" : "AWS::Region" }, "n",
]]}}
}
69
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 72. その他スクリプト
! cfn-‐‑‒get-‐‑‒metadata
• Metadataを簡単に取得出来るスクリプト
! cfn-‐‑‒signal
• リソース「WaitCondition」と共に使⽤用
• cfn-‐‑‒signalを使⽤用して、スタックに対して初期化状態の通知をす
ることが出来る
• アプリのインストールが失敗した場合にスタックを破棄したり
できる
! cfn-‐‑‒hup
• スタックを更更新(StackUpdateAPIを使⽤用)した場合に、それ
を感知して指定のファイルを実⾏行行するフックスクリプト
https://s3.amazonaws.com/cloudformation-‐‑‒examples/
BoostrappingApplicationsWithAWSCloudFormation.pdf
72
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 73. ! CloudFormer
73
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 74. CloudFormer
! 既に構築済みのシステム構成から、テンプレートを作成
するツール
! テンプレート作成のベースとなるテンプレートを構築す
るのに利利⽤用可能
テンプレート
CloudFormer
EC2
EC2
AutoScaling
74
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 75. CloudFormerの使い⽅方
! 既存の⼿手順やツールで必要なリソースを構成
! CloudFormerスタックを作成し起動
• CloudFormer⾃自⾝身がCloudFormationのスタックです。お客様
のAWS環境でスタックを起動することでCloudFormerが起動さ
れます。t1.maicro EC2インスタンス上で実⾏行行されます。
! CloudFormerを使⽤用して既存のAWSリソースからテン
プレートを⽣生成、結果はS3バケットに保存
! CloudFormerスタックをシャットダウン
! スタックを起動するために作成されたテンプレートを利利
⽤用ください
75
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 76. CloudFormerの特⻑⾧長
! CloudFormerは全てのCloudFormationのリソースをサ
ポート
! CloudFormerは⾃自動的に従属するリソースを選択
• 例例えば、EC2インスタンスを選択すると、EC2インスタンスが
必要とするセキュリティグループもリソースとして選択
! どのリソースをテンプレートに含めるか完全に制御可能
! 必要であれば⾃自動的に選択されたリソースを上書きした
り、他のリソースを追加したり可能
! テンプレートで⽤用いられる名前を指定可能
! 標準の名前は、既存のリソースの名前に基づく
! リソースの利利⽤用可能なアトリビュートからOutputパラ
メータを追加可能
76
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 77. CloudFormerスタックを作成し起動
! 以下のいずれかからスタックを作成
• AWS CloudFormation コンソール
• サンプルテンプレートから、” CloudFormer -‐‑‒ create a template
from your existing resources”を選択
• CloudFormerツールのページ
• http://aws.amazon.com/developertools/6460180344805680
• AWS CloudFormationテンプレートページ
• http://aws.amazon.com/cloudformation/aws-‐‑‒
cloudformation-‐‑‒templates/
! 作成したスタックを起動
• CloudFormerスタックのOutput、”URL”をクリック
77
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 78. CloudFormer起動
78
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 80. ! その他Tips
80
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 81. Baked AMI VS CF-‐‑‒Init
! AMIの作り込み VS Cloud-‐‑‒init+CloudFormation
helpers
! ⽤用途によって、AMI作り込み、cloud-‐‑‒init、helperを使
い分ける事が重要
利点
欠点
AMI作り込み
・構築が容易
・起動が速い
(オートスケールに有効)
・ベースAMI/ミドルウェア
更新時に再インストールが必要
・リージョン毎の構築が必要
ベースAMI+
Cloud-init+CFN
helpers
・ベースAMI/ミドルウェア更新時
に追従が可能
・DBのエンドポイントなどの
定義が渡せる
・テンプレート構築が手間
・OS初期化に時間がかかる
81
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 83. ! まとめ
83
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 84. CloudFormation利利⽤用のFAQ
! テンプレートまたはスタック数に制限はありますか?
• テンプレート数に制限はありません。アカウントに対して、ス
タックの最⼤大数は20に制限。
! 説明フィールドの⽂文字数に制限はありますか?
• テンプレート、パラメータ、出⼒力力、およびリソースの説明⽤用の
フィールドは、4,096⽂文字に制限。
! テンプレートのパラメータ、出⼒力力の数に制限はあります
か?
• 50のパラメータおよび10の出⼒力力。
84
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
- 87. 参考URL
! Cloud Formation Samples
• http://aws.amazon.com/jp/cloudformation/aws-‐‑‒
cloudformation-‐‑‒templates/
! AWS CloudFormation のアーティクルとチュートリア
ル
• http://aws.amazon.com/jp/cloudformation/aws-‐‑‒
cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials/
• BootstrappingApplicationsWithAWSCloudFormation
• https://s3.amazonaws.com/cloudformation-‐‑‒examples/
BoostrappingApplicationsWithAWSCloudFormation.pdf
• Integrating CloudFormation with Chef
• https://s3.amazonaws.com/cloudformation-‐‑‒examples/
IntegratingAWSCloudFormationWithOpscodeChef.pdf
! AWS CloudFormation よくある質問
• http://aws.amazon.com/jp/cloudformation/faqs/
87
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.