Infrastructure as Code
を実現CloudFormation
はテンプレートをアップロードしてスタックを作成CloudFormation
テンプレートhttps://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/template-anatomy.html
AWSTemplateFormatVersion: "version date"
Description:
String
Metadata:
template metadata
Parameters:
set of parameters
Rules:
set of rules
Mappings:
set of mappings
Conditions:
set of conditions
Transform:
set of transforms
Resources:
set of resources
Outputs:
set of outputs
このセクションには、AWS CloudFormation でサポートされているすべての AWS リソースタイプおよびプロパティタイプのリファレンス情報が含まれています。 リソースタイプ識別子は、常に次の形式です。
service-provider::service-name::data-type-name
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html
Parameters:
# ...
Foo:
Description: Foo description
Type: String
Default: foo
Parameter
プロパティにString
プロパティタイプとしてFoo
を定義String
プロパティタイプはAWSで定義済みFoo
は任意の文字列例1
Resources:
# ...
ArtifactStoreBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
Resources
プロパティにAWS::S3::Bucketリソースタイプとして、論理IDArtifactStoreBucket
を定義ArtifactStoreBucket
は任意の文字列Properties
としてVersioningConfiguration
などを持つ例2
Resources:
# ...
DeployPipeline:
Type: "AWS::CodePipeline::Pipeline"
Properties:
RoleArn: arn:aws:iam:{{アカウントID}}:role/{{CodePipelineServiceRole}}
# ------------------------------------------------------------#
# CodePipeline Stages
# ------------------------------------------------------------#
Stages:
# ------------------------------------------------------------#
# Category: Deploy
# ------------------------------------------------------------#
- Name: CFNSource
Actions:
-
Name: CFNTemplateSourc
Resources
にAWS::CodePipeline::Pipelineリソースタイプとして、論理IDDeployPipeline
を定義DeployPipeline
は任意の文字列AWS::CodePipeline::Pipeline
リースタイプはProperties
としてRoleArn
やStages
などを持つRoleArn
プロパティタイプはString
Stages
プロパティタイプはStageDeclaration大きく以下のセクション
Syntax
Properties
Return values
Return values
は、Fn::GetAttr
で参照するしたときに返される値。
Fn::GetAtt: [ logicalNameOfResource, attributeName ]
# or
!GetAtt logicalNameOfResource.attributeName
AWS::S3::Bucket
やAWS::CodePipeline::Pipeline
など)は、Properties
を持つProperties
に加えて、共通してリ使用できるリソース属性(例:DependsOn
属性)を追加できるref. リソース属性リファレンス
依存関係はDependsOn
で明示的に設定する他に、!Ref
および!GetAtt
で暗黙的に設定される場合がある。
AWS CloudFormation には、スタックの管理に役立ついくつかの組み込み関数が用意されています。実行するまでわからない値をプロパティに代入するには、テンプレートで組み込み関数を使用します。
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html
intrinsic
:内在的
The intrinsic function Ref returns the value of the specified parameter or resource.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html
参照 | 戻り値 |
---|---|
parameter's logical name | 値(value) |
resource's logical name | 物理ID(physical ID) |
In addition to the logical ID, certain resources also have a physical ID, which is the actual assigned name for that resource, such as an EC2 instance ID or an S3 bucket name. Use the physical IDs to identify resources outside of AWS CloudFormation templates, but only after the resources have been created.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
追記:!Ref
にリソースの論理ID
を指定したときに返される値はドキュメントのReturn Values
セクションに記載されている。
実行時に、AWS CloudFormation が関連する値と置き換わる変数のある文字列。変数を ${MyVarName} として書き込みます。変数は、テンプレートパラメーター名、リソースの論理 ID、リソース属性、またはキー/値マップの変数です。テンプレートパラメーター名、リソースの論理 ID、およびリソース属性のみを指定する場合、キー/値マップを指定しないでください。
テンプレートパラメータ名、または ${InstanceTypeParameter} などのリソースの論理 ID を指定すると、AWS CloudFormation は Ref 組み込み関数を使用した場合と同じ値を返します。
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html
マップ使用例。
Parameters:
RootDomainName:
type: String
value: example.com
# ...
# ...
Name: !Sub
- www.${Domain}
- { Domain: !Ref RootDomainName }
この場合、Name
にはwww.example.com
が入る。
Fn::GetAtt: [ logicalNameOfResource, attributeName ]
# or
!GetAtt logicalNameOfResource.attributeName
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html
Fn::ImportValue: {{エクスポート名}}
{{エクスポート名}}
はエクスポートのOutpus.Export.Name
の値。
ref. https://www.youtube.com/watch?v=cILN6ycz0TE
Outpus.Export.Name
は、リージョン内で一意でなければいけない。
The following restrictions apply to cross-stack references:
- For each AWS account, Export names must be unique within a region.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html
AWSのECSのExecutionRoleArnとTaskRoleArnの違いがわからない
ExecutionRoleArn
:ECS
が持つべきロールTaskRoleArn
:タスクが持つべきロールCloudWatch
へのログを出力していればCloudWatch
のポリシーが必要aws cloudformation create-stack \
--stack-name foo \
--template-body file///path/to/template.ym \
--capabilities CAPABILITY_NAMED_IAM \
ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-creating-stack.html
update-stack
は直接スタックを更新(実装)するaws cloudformation create-change-set \
--stack-name foo \
--template-body file///path/to/template.yml \
--change-set-name FooChangeSet \
--capabilities CAPABILITY_NAMED_IAM \
ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets-create.html
マネジメントコンソールでは既存スタックの変更セットを作成
を実行する。
create-change-set
を使用するaws cloudformation update-stack \
--stack-name foo \
--template-body file///path/to/template.yml \
--change-set-name FooChangeSet \
--capabilities CAPABILITY_NAMED_IAM \
ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-direct.html
マネジメントコンソールからは↑の画像の更新
を実行する。
$ aws cloudformation validate-template --template-body file:///path/to/template.yml --profile foo
ROLLBACK_COMPLETE state and can not be updated
スタックの情報とリストの取得
CloudFormation スタックが UPDATE_ROLLBACK_FAILED 状態のままになっている場合に、このスタックを更新するにはどうすればよいですか?
CloudFormation
のドキュメント)CodePipeline
のドキュメント)Intrinsic 固有の、本質的な
!Ref
の使い所