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プロパティタイプはStringStagesプロパティタイプはStageDeclaration大きく以下のセクション
SyntaxPropertiesReturn valuesReturn 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の使い所