Transcript
Page 1: [AWSマイスターシリーズ] AWS CloudFormation

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS マイスターシリーズ

AWS CloudFormation

20131127アマゾン データ サービス ジャパン株式会社ソリューションアーキテクトemsp 吉荒 祐一

reGenerate

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

2013年年CloudFormationのアップデート

  201327bull  Amazon S3 と Amazon RDSのタグ付けが可能に

  2013220bull  EBS-‐‑‒Optimized EC2インスタンスのプロビジョニングbull  Auto Scaling Groupのローリングデプロイbull  スタックアップデートのキャンセル

  2013812bull  並列列スタック処理理とネストされたスタックの更更新の追加

  2013917bull  追加のVPCサポートと新テンプレート

  20131014bull  フェデレーテッドユーザーおよび一時的なセキュリティ認証情報が利利用可能に

2

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Agenda

CloudFormationの概要  スタック  テンプレート  Cloud-‐‑‒init CloudFormation helpers CloudFormer  その他Tips  まとめ

3

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 CloudFormationの概要

4

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS CloudFormation

  EC2やELBなどを使ったAWSサービスのシステム構築を設定ファイル(テンプレート)を元に行行えるサービス

  テンプレートを自由に作成できるため自分好みのシステム構成が可能

  テンプレートはAWSのサービスを操るための新しい言語

5

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

AWS CloudFormationのイメージ

S3

CloudWatch

Elastic Load Balancing

EC2 EC2Auto Scaling

SNS

テンプレート

CloudFormation

テンプレートに基づき各サービスが起動

6

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用シーンと利利点

  一度度テンプレートを作成すれば同じ構成を再現できるbull  開発環境の構築bull  BlogシステムWebシステムゲームプラットフォームなど同じ仕組みでアプリやデータが異異なるようなもの

  ベストプラクティスが盛り込まれたテンプレートが使用可能bull  システムアーキテクチャの再利利用bull  WordPress Redmineなどが入った多くのサンプルテンプレートが提供済み

  起動時にパラメータを渡せるbull  例例えばDBのエンドポイントをEC2に渡せる

7

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用料料金金

CloudFormationの利利用自体は無料料

  テンプレートに従って構築された各AWSサービスに対して課金金

8

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 2: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

2013年年CloudFormationのアップデート

  201327bull  Amazon S3 と Amazon RDSのタグ付けが可能に

  2013220bull  EBS-‐‑‒Optimized EC2インスタンスのプロビジョニングbull  Auto Scaling Groupのローリングデプロイbull  スタックアップデートのキャンセル

  2013812bull  並列列スタック処理理とネストされたスタックの更更新の追加

  2013917bull  追加のVPCサポートと新テンプレート

  20131014bull  フェデレーテッドユーザーおよび一時的なセキュリティ認証情報が利利用可能に

2

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Agenda

CloudFormationの概要  スタック  テンプレート  Cloud-‐‑‒init CloudFormation helpers CloudFormer  その他Tips  まとめ

3

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 CloudFormationの概要

4

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS CloudFormation

  EC2やELBなどを使ったAWSサービスのシステム構築を設定ファイル(テンプレート)を元に行行えるサービス

  テンプレートを自由に作成できるため自分好みのシステム構成が可能

  テンプレートはAWSのサービスを操るための新しい言語

5

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

AWS CloudFormationのイメージ

S3

CloudWatch

Elastic Load Balancing

EC2 EC2Auto Scaling

SNS

テンプレート

CloudFormation

テンプレートに基づき各サービスが起動

6

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用シーンと利利点

  一度度テンプレートを作成すれば同じ構成を再現できるbull  開発環境の構築bull  BlogシステムWebシステムゲームプラットフォームなど同じ仕組みでアプリやデータが異異なるようなもの

  ベストプラクティスが盛り込まれたテンプレートが使用可能bull  システムアーキテクチャの再利利用bull  WordPress Redmineなどが入った多くのサンプルテンプレートが提供済み

  起動時にパラメータを渡せるbull  例例えばDBのエンドポイントをEC2に渡せる

7

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用料料金金

CloudFormationの利利用自体は無料料

  テンプレートに従って構築された各AWSサービスに対して課金金

8

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 3: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Agenda

CloudFormationの概要  スタック  テンプレート  Cloud-‐‑‒init CloudFormation helpers CloudFormer  その他Tips  まとめ

3

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 CloudFormationの概要

4

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS CloudFormation

  EC2やELBなどを使ったAWSサービスのシステム構築を設定ファイル(テンプレート)を元に行行えるサービス

  テンプレートを自由に作成できるため自分好みのシステム構成が可能

  テンプレートはAWSのサービスを操るための新しい言語

5

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

AWS CloudFormationのイメージ

S3

CloudWatch

Elastic Load Balancing

EC2 EC2Auto Scaling

SNS

テンプレート

CloudFormation

テンプレートに基づき各サービスが起動

6

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用シーンと利利点

  一度度テンプレートを作成すれば同じ構成を再現できるbull  開発環境の構築bull  BlogシステムWebシステムゲームプラットフォームなど同じ仕組みでアプリやデータが異異なるようなもの

  ベストプラクティスが盛り込まれたテンプレートが使用可能bull  システムアーキテクチャの再利利用bull  WordPress Redmineなどが入った多くのサンプルテンプレートが提供済み

  起動時にパラメータを渡せるbull  例例えばDBのエンドポイントをEC2に渡せる

7

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用料料金金

CloudFormationの利利用自体は無料料

  テンプレートに従って構築された各AWSサービスに対して課金金

8

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 4: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 CloudFormationの概要

4

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS CloudFormation

  EC2やELBなどを使ったAWSサービスのシステム構築を設定ファイル(テンプレート)を元に行行えるサービス

  テンプレートを自由に作成できるため自分好みのシステム構成が可能

  テンプレートはAWSのサービスを操るための新しい言語

5

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

AWS CloudFormationのイメージ

S3

CloudWatch

Elastic Load Balancing

EC2 EC2Auto Scaling

SNS

テンプレート

CloudFormation

テンプレートに基づき各サービスが起動

6

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用シーンと利利点

  一度度テンプレートを作成すれば同じ構成を再現できるbull  開発環境の構築bull  BlogシステムWebシステムゲームプラットフォームなど同じ仕組みでアプリやデータが異異なるようなもの

  ベストプラクティスが盛り込まれたテンプレートが使用可能bull  システムアーキテクチャの再利利用bull  WordPress Redmineなどが入った多くのサンプルテンプレートが提供済み

  起動時にパラメータを渡せるbull  例例えばDBのエンドポイントをEC2に渡せる

7

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用料料金金

CloudFormationの利利用自体は無料料

  テンプレートに従って構築された各AWSサービスに対して課金金

8

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 5: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS CloudFormation

  EC2やELBなどを使ったAWSサービスのシステム構築を設定ファイル(テンプレート)を元に行行えるサービス

  テンプレートを自由に作成できるため自分好みのシステム構成が可能

  テンプレートはAWSのサービスを操るための新しい言語

5

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

AWS CloudFormationのイメージ

S3

CloudWatch

Elastic Load Balancing

EC2 EC2Auto Scaling

SNS

テンプレート

CloudFormation

テンプレートに基づき各サービスが起動

6

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用シーンと利利点

  一度度テンプレートを作成すれば同じ構成を再現できるbull  開発環境の構築bull  BlogシステムWebシステムゲームプラットフォームなど同じ仕組みでアプリやデータが異異なるようなもの

  ベストプラクティスが盛り込まれたテンプレートが使用可能bull  システムアーキテクチャの再利利用bull  WordPress Redmineなどが入った多くのサンプルテンプレートが提供済み

  起動時にパラメータを渡せるbull  例例えばDBのエンドポイントをEC2に渡せる

7

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用料料金金

CloudFormationの利利用自体は無料料

  テンプレートに従って構築された各AWSサービスに対して課金金

8

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 6: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

AWS CloudFormationのイメージ

S3

CloudWatch

Elastic Load Balancing

EC2 EC2Auto Scaling

SNS

テンプレート

CloudFormation

テンプレートに基づき各サービスが起動

6

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用シーンと利利点

  一度度テンプレートを作成すれば同じ構成を再現できるbull  開発環境の構築bull  BlogシステムWebシステムゲームプラットフォームなど同じ仕組みでアプリやデータが異異なるようなもの

  ベストプラクティスが盛り込まれたテンプレートが使用可能bull  システムアーキテクチャの再利利用bull  WordPress Redmineなどが入った多くのサンプルテンプレートが提供済み

  起動時にパラメータを渡せるbull  例例えばDBのエンドポイントをEC2に渡せる

7

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用料料金金

CloudFormationの利利用自体は無料料

  テンプレートに従って構築された各AWSサービスに対して課金金

8

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 7: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用シーンと利利点

  一度度テンプレートを作成すれば同じ構成を再現できるbull  開発環境の構築bull  BlogシステムWebシステムゲームプラットフォームなど同じ仕組みでアプリやデータが異異なるようなもの

  ベストプラクティスが盛り込まれたテンプレートが使用可能bull  システムアーキテクチャの再利利用bull  WordPress Redmineなどが入った多くのサンプルテンプレートが提供済み

  起動時にパラメータを渡せるbull  例例えばDBのエンドポイントをEC2に渡せる

7

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用料料金金

CloudFormationの利利用自体は無料料

  テンプレートに従って構築された各AWSサービスに対して課金金

8

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 8: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

利利用料料金金

CloudFormationの利利用自体は無料料

  テンプレートに従って構築された各AWSサービスに対して課金金

8

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 9: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSの構築デプロイ自動化サービスの中での位置づけ

Elastic Beanstalk OpsWorks CloudFormation

フレキシビリティ

導入の容易易さ

9

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 10: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationに適した場面

  幅広いリソースを使い環境構築する場合bull  エンタープライズアプリケーションbull  レガシーアプリケーションbull  他の自動化サービスではサポートされないリソースを使用

ElasticBeanstalkと組み合わせて構築する場合bull  リソースのひとつとしてElasticBeanstalkをサポート

10

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 11: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 スタック

11

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 12: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック

  リソース(EC2インスタンスやS3バケットRDSインスタンスなど)の集合のこと

  スタック単位でリソースの管理理が可能スタック破棄を実行行するとスタックにひもづくリソースを破棄することが可能

  使用するリソースおよびリソースの構築順はテンプレートの依存関係で決定

WebServer

AppServer

スタック これらを一度度に生成破棄可能

12

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 13: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック可能なリソースタイプ

  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 サブスクリプション

  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

13

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 14: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法   AWS Management Consoleから構築   コマンドラインツール

bull  AWS Command Line Toolbull  httpawsamazoncomcli

  各種SDKbull  Java httpawsamazoncomjpsdkforjavabull  NET httpawsamazoncomjpsdkfornetbull  PHP httpawsamazoncomjpsdkforphpbull  Ruby httpdocsawsamazoncomAWSSdkDocsRubylatestDeveloperGuideruby-‐‑‒dg-‐‑‒setuphtml

bull  JavaScript httpawsamazoncomdocumentationsdkforjavascript

bull  Python httpdocspythonbotoorgenlatest

14

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 15: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

スタック構築方法もうひとつの視点

  既存のテンプレートから構築bull  ローカルファイルbull  S3上のファイル

  サンプルテンプレートから構築bull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  ゼロから新しいものを作る

  テンプレートはテキストファイルとして作成管理理共有可能

15

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 16: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒1

16

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 17: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒2

スタックの名称

サンプルテンプレート

ローカルファイルのemsp テンプレート

テンプレートファイルURL(同一リージョンのS3上)

いずれか

17

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 18: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

各種パラメータの入力力(後で説明)

AWS Management Consoleから構築-‐‑‒3

18

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 19: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒4

リソースにタグ付け

19

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 20: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒5

20

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 21: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒6

スタックのコスト見見積もり

21

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 22: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒7

スタック構築開始

22

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 23: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒8

23

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 24: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒9

スタック構築実行行中

24

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 25: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒10

スタック構築完了了この時点ですべてのサービスが起動している

25

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 26: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWS Management Consoleから構築-‐‑‒11

スタック構築失敗ロールバック中

26

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 27: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 テンプレート

27

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 28: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート

CloudFormationの心臓部   スタック構築の設計図   JSONフォーマットで記述

AWSTemplateFormatVersion 2010-09-09 Description ldquoSample Parameters KeyName Description ldquoSample key Type String Mappings Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

28

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 29: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレート解説 AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

テンプレートのバージョン(最新は2010-‐‑‒09-‐‑‒09)

テンプレートの詳細(説明文)

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

EC2やRDSなどスタックを構成するリソースを定義

スタック構築後に取得したい値(例例アクセスURLなど)

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

29

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 30: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Parameters

CloudFormation実行行時に後で変更更可能なパラメータを列列挙(例例DBユーザー名など)

30

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 31: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersとは   スタック構築時に値の入力力が可能   データ型デフォルト値最小最大値など設定可能

Parameters Age TypeNumber ldquoDefaultrdquo ldquo30rdquo ldquoMinValuerdquo ldquo20rdquo ldquoMaxValuerdquo ldquo60rdquo Descriptioninput your ageldquo FirstName TypeString Descriptioninput your first nameldquo

31

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 32: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Parametersのプロパティ   利利用出来るプロパティプロパティ 内容Type データ型 ldquoStringrdquo ldquoNumberrdquo ldquoCommaDelimitedListrdquoDefault デフォルト値NoEcho 入力力時にとなる(パスワードなどに使用)AllowedValues 入力力可能値の一覧指定 (例例[ldquotruerdquordquofalserdquo] )AllowedPattern 正規表現で入力力可能パターンを指定(例例[a-‐‑‒zA-‐‑‒Z])MaxLength 最大文字数MinLength 最小文字数MaxValue 最大値MinValue 最小値Description プロパティの詳細説明ConstraintDescription 入力力した値がAllowedPatternやMaxLengthなどの制約に

引っかかった時に表示する説明(どのような制約があるかの説明を記述)

32

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 33: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAgerdquo ldquoTagsrdquo ldquoRefrdquo ldquoFirstNamerdquo

Parametersの参照

  入力力したパラメータ値はテンプレート中で ldquoRefrdquoを使用して参照可能

  ユーザー名パスワードドメインなどの可変部分に便便利利

ここにパラメータ入力力値が反映される

33

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 34: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Function   Refbull  パラメータを参照する

FnBase64bull  文字列列をBase64エンコードする

FnFindInMapbull  Mapから値を取り出す

FnGetAttbull  リソースから属性を取り出すbull  例例FnGetAtt [ MyELB DNSName]

FnGetAZsbull  指定リージョンのアベイラビリティゾーンを返す

FnJoinbull  文字列列の連結bull  ldquoFnJoinrdquo [ ldquordquo [ ldquoardquo ldquobrdquo ldquocrdquo ] ] は 「abc」を返すbull  ldquordquoがセパレータ不不要な場合は rdquordquo を定義

FnSelectbull  Index値に従ってリストから値を返すbull  例例 ldquoFnSelectrdquo [ ldquo1rdquo [ ldquoapplesrdquo ldquograpesrdquo ldquoorangesrdquo ldquomangoesrdquo ] ] ldquograpesrdquoを返す

  Condition Functionsbull  FnIf FnEquals FnNotのようなFunctionにより条件によるスタック作成を可能に

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlintrinsic-‐‑‒function-‐‑‒referencehtml34

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 35: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Pseudo Parameter   実行行リージョンやスタック名といったパラメータを取得する予約語

bull  ldquoRefrdquo ldquoAWSRegionrdquo のように値を取得可能   AWSAccountId   AWSNotificationARNs

bull  notification Amazon Resource Names (ARNs)のリスト取得   AWSNoValue

bull  FnIf Functionにて使用   AWSRegion   AWSStackId   AWSStackName

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlpseudo-‐‑‒parameter-‐‑‒referencehtml

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoKeyNamerdquo ldquoRefrdquo ldquoAWSStackNamerdquo ldquoTagsrdquo ldquoRefrdquo ldquoAWSRegionrdquo

35

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 36: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Mappings

Hashtableのようなものキーに応じて値を特定出来る

(例例リージョンに応じたAMI番号など)

36

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 37: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsとは   キーとバリューのテーブル   例例えば入力力値やリージョンによって値が変わるようなものを決めるような使い方

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

マッピングを定義(この例例の場合rdquoRegionTablerdquoという

マッピングを定義)

37

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 38: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Mappingsの使い方

  ldquoFnFindInMaprdquoで値を取得   FnFindInMap [ MapName Key Value] MapNamekeyValueにはrdquoRefrdquoが利利用可能

Mappings RegionTable us-east-1 AMI ami-8c1fece5ldquo ldquoKeyrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

ldquoFnFindInTemplaterdquo

ldquoResources ldquoMyServer ldquoTypeldquoAWSEC2Instance ldquoPropertiesrdquo ldquoImageIdrdquo ldquoFnFindInMaprdquo [ ldquoRegionTablerdquo ldquoRefrdquo ldquoregionrdquo ldquoAMIrdquo] この例例だと入力力された

regionパラメータによって利利用するAMIのIDを決定できる

38

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 39: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Conditions

Parameters他のConditionMappingから条件判断し結果に応じてリソースを作成可能

39

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 40: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Conditionsの使い方 AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

40

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 41: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion 2010-09-09 Mappings RegionMap

<一部省略>13 Parameters EnvType Description Environment type Default test Type String AllowedValues [prod test] ConstraintDescription must specify prod or test Conditions CreateProdResources FnEquals [Ref EnvType prod] Resources EC2Instance Type AWSEC2Instance Properties ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] MountPoint Type AWSEC2VolumeAttachment Condition CreateProdResources Properties InstanceId Ref EC2Instance VolumeId Ref NewVolume Device devsdh NewVolume Type AWSEC2Volume Condition CreateProdResources Properties Size 100 AvailabilityZone FnGetAtt [ EC2Instance AvailabilityZone ]

EnvTypeパラメータがrdquoprodrdquoと等しければldquoCreateProdResourcesrdquo条件が成立立

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

ldquoCreateProdResourcesrdquo条件が成立立している

場合にのみリソース生成

Conditionsの使い方

41

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 42: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Resources

EC2やRDSなどスタックを構成するリソースを定義

42

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 43: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Resourcesとは   EC2やELBRDSなど起動するサービスを設定   リソース毎に決められたパラメータを設定する

ldquoResources ldquoMyWebServer ldquoType ldquoAWSEC2Instancerdquo ldquoProperties ldquoKeyName ami-8c1fece5ldquo ldquoImageIdrdquo ldquomyKey-eastrdquo us-west-1 AMI ami-3bc9997eldquo ldquoKeyrdquo ldquomyKey-westrdquo ap-northeast-1 AMI ami-300ca731ldquo ldquoKeyrdquo ldquomyKey-japanrdquo

43

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 44: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

EC2+SecurityGroupの例例 Resources Ec2Instance Type AWSEC2Instance Properties SecurityGroups [ Ref InstanceSecurityGroup ] KeyName Ref KeyName ImageId FnFindInMap [ RegionMap Ref AWSRegion AMI ] InstanceSecurityGroup Type AWSEC2SecurityGroup Properties GroupDescription Enable SSH access via port 22 SecurityGroupIngress [ IpProtocol tcp FromPort 22 ToPort 22 CidrIp 00000 ]

リソース毎のプロパティ

リソースタイプ

44

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 45: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

リソースタイプ  AWSAutoScalingAutoScalingGroup  AWSAutoScalingLaunchConfiguration  AWSAutoScalingScalingPolicy  AWSAutoScalingTrigger  AWSCloudFormationAuthentication  AWSCloudFormationCustomResource  AWSCloudFormationInit  AWSCloudFormationStack  AWSCloudFormationWaitCondition  AWSCloudFormationWaitConditionHandle  AWSCloudFrontDistribution  AWSCloudWatchAlarm  AWSDynamoDBTable  AWSEC2CustomerGateway  AWSEC2DHCPOptions  AWSEC2EIP  AWSEC2EIPAssociation  AWSEC2Instance  AWSEC2InternetGateway  AWSEC2NetworkAcl  AWSEC2NetworkAclEntry  AWSEC2NetworkInterface  AWSEC2NetworkInterfaceAttachment  AWSEC2Route  AWSEC2RouteTable  AWSEC2SecurityGroup  AWSEC2SecurityGroupIngress  AWSEC2SecurityGroupEgress  AWSEC2Subnet  AWSEC2SubnetNetworkAclAssociation  AWSEC2SubnetRouteTableAssociation  AWSEC2Volume  AWSEC2VolumeAttachment  AWSEC2VPC  AWSEC2VPCDHCPOptionsAssociation  AWSEC2VPCGatewayAttachment

  AWSEC2VPNConnection  AWSEC2VPNConnectionRoute  AWSEC2VPNGateway  AWSEC2VPNGatewayRoutePropagation  AWSElastiCacheCacheCluster  AWSElastiCacheParameterGroup  AWSElastiCacheSecurityGroup  AWSElastiCacheSecurityGroupIngress  AWSElastiCacheSubnetGroup  AWSElasticBeanstalkApplication  AWSElasticBeanstalkEnvironment  AWSElasticLoadBalancingLoadBalancer  AWSIAMAccessKey  AWSIAMGroup  AWSIAMInstanceProfile  AWSIAMPolicy  AWSIAMRole  AWSIAMUser  AWSIAMUserToGroupAddition  AWSRDSDBInstance  AWSRDSDBParameterGroup  AWSRDSDBSubnetGroup  AWSRDSDBSecurityGroup  AWSRDSDBSecurityGroupIngress  AWSRoute53RecordSet  AWSRoute53RecordSetGroup  AWSS3Bucket  AWSS3BucketPolicy  AWSSDBDomain  AWSSNSTopicPolicy  AWSSNSTopic  AWSSQSQueue  AWSSQSQueuePolicy

httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

AWSaws-‐‑‒product-‐‑‒namedata-‐‑‒type-‐‑‒name

45

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 46: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Attributes

  リソースの振る舞いや関係を制御する DeletionPolicy

bull  スタックを削除する際のリソースの処理理bull  Delete (Default) Retain Snapshot

DependsOnbull  指定したリソースが生成された後に生成bull  AWSCloudFormationWaitCondition AWSCloudFormationWaitConditionHandleと併用

  Metadata UpdatePolicy

bull  AWSAutoScalingAutoScalingGroupリソースのみbull  Auto ScalingのRolling Updateでの挙動を制御

bull  MaxBatchSizebull  MinInstancesInServicebull  PauseTime

46

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 47: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSTemplateFormatVersion ldquo2010-09-09 Description Valid JSON strings up to 4K Parameters set of parameters Mappings set of mappings ldquoConditions set of conditions Resources set of resources Outputs set of outputs

Outputs

スタック構築後に取得したい値(例例アクセスURLなど)

47

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 48: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Outputsとは

  スタック構築後に取得表示したい情報の定義   例例えばアクセスURLやDBの通信先情報作ったIAMユーザー名などあとで使用するもの

  マネジメントコンソールから確認できるのでスタックに関する情報を出力力すると便便利利(キーペア名AZインスタンスIDなど)

48

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 49: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

出力力したい名称を定義

説明

Functionを使って文字列列を加工

49

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 50: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

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 FnGetAtt [ Ec2Instance AvailabilityZone ] ApplicationURL Description URL of running web application Value     FnJoin      [ [ http FnGetAtt [ Ec2Instance PublicIp] indexhtml] ]

50

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 51: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートのまとめ

ParametersMappingsResourcesOutputsの4つのブロックに定義を記述

  可変項目はパラメーター化したりMappingで定義   Functionsを使用して参照や文字連結などの操作が可能

  Resourceのパラメータはリソースタイプにより異異なるドキュメントを利利用して定義bull  httpdocsamazonwebservicescomAWSCloudFormationlatestUserGuideindexhtmlaws-‐‑‒template-‐‑‒resource-‐‑‒type-‐‑‒refhtml

51

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 52: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips   リソース定義にEC2インスタンスが必須ではない

bull  DB構築だけオートスケールだけアラーム設定だけでもOK   参照関係があると依存関係があるとみなされる相互参照や循環参照はNG(エラーとなる)

  参照関係がある部分は参照の整合性が取れるよう起動順をCloudFormationが決定

  スタックを破棄するとスタックにひもづくリソースは破棄されるbull  ただしrdquoDeletionPolicyrdquoをrdquoRetainrdquoにすると破棄されない

Resources myS3Bucket Type AWSS3Bucket DeletionPolicy Retain

52

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 53: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

  リソース数が多いと何のリソースか分からなくなるのでTagsプロパティを使用するMyInstance Type AWSEC2Instance Properties SecurityGroups [ Ref MySecurityGroup ] ImageId ami-20b65349 Tags [ Key ldquoName Value ldquoMyInstance ]

53

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 54: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

テンプレートTips

CloudFormationで作られたリソースにはスタック名などのタグが付与される

54

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 55: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

入れ子テンプレート

  テンプレートからテンプレートの呼び出しが可能   リソースタイプに ldquoAWSCloudFormationStackldquoを指定 TemplateURLを指定するとそのテンプレートを読み込み

Resources  WebServer Type AWSCloudFormationStack Properties TemplateURL ldquohttpss3amazonawscomhogetemplate ]] Parameters KeyName Ref KeyName InstanceType Ref InstanceType

55

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 56: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

AWSCloudFormationStack   AWSCloudFormationStack を使用してスタックを上位のテンプレートのリソースとしてネストする

  トップレベルのスタックとネストされたスタックを一緒に更更新可能

  ネストされたスタックの更更新bull  トップレベルのスタックでUpdateStackコマンドを実行行bull  Management Consoleでトップレベルのスタックを選択しUpdate Stackをクリック

bull  CloudFormationはトップレベルのテンプレートだけでなくネストされたテンプレートに対する更更新を検出しテンプレートが変更更されたスタックのみを更更新

56

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 57: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 Cloud-‐‑‒init

57

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 58: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

  Amazon Linuxに入っている初期設定ツールbull  元々はUbuntuで作られたもの

  EC2インスタンスの起動時に「userdata」として設定したスクリプトを呼び出すbull  「」 で始まる文字列列の場合のみスクリプトを実行行

  スクリプトを記述できるのでロケール変更更アプリインストールファイル書き換えなどが可能

  RHELbull  httpawsamazoncomarticles2519228458631605

  Windowsbull  EC2ConfigService

58

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 59: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒init

User Dataに定義Cloud-‐‑‒initがuserdata

を読み込む

読み込んだスクリプトを実行行

EC2インスタンス起動

59

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 60: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Cloud-‐‑‒initの特長と制限   特長   インスタンス起動時にインスタンスの初期化が可能

bull  AMIとユーザーデータは分離離できるのでAMIの作り込みが不不要になる

bull  Apacheインストール済みのAMIを用意するとOSやApacheのバージョンが上がったときに再作成になる

  制限 userdataは一度度定義すると書き換えは不不可能

bull  書き換えて再実行行が出来ない(後述のスタック更更新に使えない)   設定できるデータ量量は16KByteまで

60

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 61: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携   EC2リソースのプロパティ「UserData」に内容を記載

MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd KeyName Ref KeyName UserData FnBase64 FnJoin [ [ binbashn log=tmpinitlogn apt-get update ampgtgt $logn apt-get install -y ruby ampgtgt $logn cd tmpn wget httprubygemsorgrubygemstgz ampgtgt $logn tar zxf rubygemstgz ampgtgt $logn cd rubygemsn ruby setuprb --no-format-executable ampgtgt $logn ] ]

61

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 62: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationとの連携 CloudFormationでUserDataを定義すると定義中にRefやFnGetAttrを使用することが可能

  例例えばRDSのEndPointをUserDataに埋め込むといったことが出来る

Resources MyInstance Type AWSEC2Instance Properties ImageId ami-dcfa4edd UserData FnBase64 FnJoin [ [ binbashn cd homeec2-usern FnJoin [ [

echo FnGetAtt [DBInstance EndpointAddress] gt dbconfn ]] ]] DBInstance Type AWSRDSDBInstance 62

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 63: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

63

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 64: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormation helpers

  EC2インスタンス初期化時のヘルパースクリプト群   利利用方法

bull  Amazon Linux AMIbull  Yumリポジトリbull  その他下記より

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestamzn1noarchrpm

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latesttargz

bull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestzipbull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestmsibull  httpss3amazonawscomcloudformation-‐‑‒examplesaws-‐‑‒cfn-‐‑‒bootstrap-‐‑‒latestsrcrpm

  optawsbin に格納bull  cfn-‐‑‒initbull  cfn-‐‑‒get-‐‑‒metadatabull  cfn-‐‑‒signalbull  cfn-‐‑‒hup

  Python製

64

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 65: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒init   パッケージやファイルのダウンロードファイルの生成を行行うスクリプト

  実行行のための設定はMetadata領領域に定義bull  AWSCloudFormationInitbull  ConfigsetsCommandsFilesGroupsPackagesServicesSourcesUsers

Resources MyInstance Type AWSEC2Instance Metadata AWSCloudFormationInit config sources packages files services Properties

sourcepackagefilesservicesの順に実行行される

65

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 66: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   sources

bull  指定したURLからファイルをダウンロードして展開bull  Tarziptar+gziptar+bz2

sources etcmyapp httpss3amazonawscommybucketmyapptargz

  packagessect  パッケージシステムを使用してアプリをインストールsect  Aptyumrubygemspythonrpm

packages yum httpd [] php [] rubygems chef [ 0102 ]

最新版

バージョン指定

66

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 67: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   files

bull  指定パスへのファイルの生成

files appdbconf content FnJoin [ [ dbname= Ref DBName n dbuser= Ref DBUser n dbpass= Ref DBPassword n dbhost= FnGetAtt [DBInstance EndpointAddress] n ]] mode 000644 owner root group root etcmyappmyapp-initpp source httpss3amazonawscommyappmyapp-initpp mode 100644 owner root group wheel

ファイルの中身を生成

ファイルパス

S3などから取得

67

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 68: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの設定   services

bull  サービスの起動再起動の指定bull  現在sysvinitのみservices sysvinit nginx enabled true ensureRunning true files [etcnginxnginxconf] sources [varwwwhtml] php-fastcgi enabled true ensureRunning true packages yum [php spawn-fcgi]

cfn-‐‑‒init終了了後に起動するかどうか

OS起動時に起動させるかどうか

設定ファイルもしくは指定ディレクトリのファイルが変わっていた場合にサービス再起動する

Cfg-‐‑‒initでパッケージを入れていたらサービス再起動

68

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 69: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

cfn-‐‑‒initの起動

cfn-‐‑‒initはデフォルトでは動作しない   User-‐‑‒dataにcfn-‐‑‒initを起動するスクリプトを記述してcloud-‐‑‒init経由で起動

  引数にスタック名リソース名リージョンアクセスキーシークレットキーを指定

Properties UserData FnBase64 FnJoin [ [ binbashn optawsbincfn-init -s Ref AWSStackName -r WebServer --access-key Ref HostKeys --secret-key FnGetAtt [HostKeys SecretAccessKey] --region Ref AWSRegion n ]]

69

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 70: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

初期化シーケンスEC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(ファイルダウンロード)Packages(インストール)

Files(設定作成)Services(サービス)

を実行行

起動完了了70

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 71: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Chef(solo)との連携例例EC2インスタンス起動

Initdに登録してあるcloud-‐‑‒initが実行行される

User-‐‑‒data領領域からスクリプトを読み込み

スクリプト内でcfn-‐‑‒initを呼び出し

cfn-‐‑‒init内でmeta-‐‑‒dataの読み込み

Meta-‐‑‒dataの設定に従いSources(レシピファイルダウンロード)Packages(Omnibusでchefインストール)

Files(レシピの一部書き換え)を実行行

起動完了了

スクリプト内でChefを実行行

httpss3amazonawscomcloudformation-‐‑‒templates-‐‑‒us-‐‑‒east-‐‑‒1wordpress-‐‑‒via-‐‑‒chef-‐‑‒solotemplate が分かりやすい例例(ただしrubygem使用)

ダウンロードしたレシピを元にchefがサーバ構築

71

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 72: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

その他スクリプト cfn-‐‑‒get-‐‑‒metadata

bull  Metadataを簡単に取得出来るスクリプト cfn-‐‑‒signal

bull  リソース「WaitCondition」と共に使用bull  cfn-‐‑‒signalを使用してスタックに対して初期化状態の通知をすることが出来る

bull  アプリのインストールが失敗した場合にスタックを破棄したりできる

cfn-‐‑‒hupbull  スタックを更更新(StackUpdateAPIを使用)した場合にそれを感知して指定のファイルを実行行するフックスクリプト

httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

72

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 73: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer

73

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 74: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer   既に構築済みのシステム構成からテンプレートを作成するツール

  テンプレート作成のベースとなるテンプレートを構築するのに利利用可能

EC2 EC2

AutoScaling

テンプレート

CloudFormer

74

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 75: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの使い方

  既存の手順やツールで必要なリソースを構成   CloudFormerスタックを作成し起動

bull  CloudFormer自身がCloudFormationのスタックですお客様のAWS環境でスタックを起動することでCloudFormerが起動されますt1maicro EC2インスタンス上で実行行されます

  CloudFormerを使用して既存のAWSリソースからテンプレートを生成結果はS3バケットに保存

  CloudFormerスタックをシャットダウン   スタックを起動するために作成されたテンプレートを利利用ください

75

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 76: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerの特長

  CloudFormerは全てのCloudFormationのリソースをサポート

  CloudFormerは自動的に従属するリソースを選択bull  例例えばEC2インスタンスを選択するとEC2インスタンスが必要とするセキュリティグループもリソースとして選択

  どのリソースをテンプレートに含めるか完全に制御可能   必要であれば自動的に選択されたリソースを上書きしたり他のリソースを追加したり可能

  テンプレートで用いられる名前を指定可能   標準の名前は既存のリソースの名前に基づく   リソースの利利用可能なアトリビュートからOutputパラメータを追加可能

76

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 77: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormerスタックを作成し起動

  以下のいずれかからスタックを作成bull  AWS CloudFormation コンソール

bull  サンプルテンプレートからrdquo CloudFormer -‐‑‒ create a template from your existing resourcesrdquoを選択

bull  CloudFormerツールのページbull  httpawsamazoncomdevelopertools6460180344805680

bull  AWS CloudFormationテンプレートページbull  httpawsamazoncomcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  作成したスタックを起動bull  CloudFormerスタックのOutputrdquoURLrdquoをクリック

77

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 78: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer起動

78

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 79: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormer実行行方法

  テンプレート化したいリージョンを選ぶとウィザード形式でリソースの選択が可能

  チェックボックスを入れたリソースを元にテンプレートが完成

79

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 80: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 その他Tips

80

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 81: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

Baked AMI VS CF-‐‑‒Init   AMIの作り込み VS Cloud-‐‑‒init+CloudFormation helpers

  用途によってAMI作り込みcloud-‐‑‒inithelperを使い分ける事が重要

利点 欠点

AMI作り込み 構築が容易 起動が速い  (オートスケールに有効)

ベースAMIミドルウェア  更新時に再インストールが必要 リージョン毎の構築が必要

ベースAMI+ Cloud-init+CFN helpers

ベースAMIミドルウェア更新時    に追従が可能 DBのエンドポイントなどの  定義が渡せる

テンプレート構築が手間 OS初期化に時間がかかる

81

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 82: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

IAM Capability CloudFormationでIAMユーザーを作成する場合ウィザード途中でチェックボックスをonにする必要があるbull  cfn-‐‑‒create-‐‑‒stackおよびcfn-‐‑‒update-‐‑‒stackの場合は emsp emsp 「-‐‑‒-‐‑‒capalilities CAPABILITY_IAM」をコマンドに付与

82

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 83: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

 まとめ

83

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 84: [AWSマイスターシリーズ] AWS CloudFormation

copy 2012 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

84

CloudFormation利利用のFAQ   テンプレートまたはスタック数に制限はありますか

bull  テンプレート数に制限はありませんアカウントに対してスタックの最大数は20に制限

  説明フィールドの文字数に制限はありますかbull  テンプレートパラメータ出力力およびリソースの説明用のフィールドは4096文字に制限

  テンプレートのパラメータ出力力の数に制限はありますかbull  50のパラメータおよび10の出力力

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 85: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

まとめ CloudFormationを使用するとシステム構成をテンプレート化できる

AmazonLinuxにはOS初期化のための仕組みが用意されている

  cloud-‐‑‒initとcfnヘルパーまたchefなどをCloudFormationと組み合わせるとアプリケーションのセットアップも柔軟に可能

  システム構築の発想を変えてみるbull  OSは素材bull  テンプレート化でよりクラウドの特性を生かしたシステム構築が可能に

85

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 86: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

CloudFormationで真のクラウドマイスターを目指せ

WebServer

AppServer

86

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87

Page 87: [AWSマイスターシリーズ] AWS CloudFormation

copy 2013 Amazoncom Inc and its affiliates All rights reserved May not be copied modified or distributed in whole or in part without the express consent of Amazoncom Inc

参考URL   Cloud Formation Samples

bull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒templates

  AWS CloudFormation のアーティクルとチュートリアルbull  httpawsamazoncomjpcloudformationaws-‐‑‒cloudformation-‐‑‒articles-‐‑‒and-‐‑‒tutorials

bull  BootstrappingApplicationsWithAWSCloudFormationbull  httpss3amazonawscomcloudformation-‐‑‒examplesBoostrappingApplicationsWithAWSCloudFormationpdf

bull  Integrating CloudFormation with Chefbull  httpss3amazonawscomcloudformation-‐‑‒examplesIntegratingAWSCloudFormationWithOpscodeChefpdf

  AWS CloudFormation よくある質問bull  httpawsamazoncomjpcloudformationfaqs

87


Top Related