#
ドキュメント

Document

自分のための備忘録です。

CloudFormation

  • Infrastructure as Codeを実現
  • プロビジョニングツール

Coudformation テンプレート

ルート定義済みプロパティ

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を定義
  • 論理IDArtifactStoreBucketは任意の文字列
  • AWS::S3::Bucketリソースタイプは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
  • ResourcesAWS::CodePipeline::Pipelineリソースタイプとして、論理IDDeployPipelineを定義
  • 論理IDDeployPipelineは任意の文字列
  • AWS::CodePipeline::PipelineリースタイプはPropertiesとしてRoleArnStagesなどを持つ
    • RoleArnプロパティタイプはString
    • StagesプロパティタイプはStageDeclaration
リソースタイプリファレンスの見方

大きく以下のセクション

  • Syntax
  • Properties
  • Return values

Return valuesは、Fn::GetAttrで参照するしたときに返される値。

Fn::GetAtt: [ logicalNameOfResource, attributeName ]
# or
!GetAtt logicalNameOfResource.attributeName

リソース属性リファレンス

  • リースタイプ(AWS::S3::BucketAWS::CodePipeline::Pipelineなど)は、Propertiesを持つ
  • 各リソースで定義されているPropertiesに加えて、共通してリ使用できるリソース属性(例:DependsOn属性)を追加できる

ref. リソース属性リファレンス

DependsOnリソース属性

依存関係はDependsOnで明示的に設定する他に、!Refおよび!GetAttで暗黙的に設定される場合がある。

組み込み関数リファレンス

AWS CloudFormation には、スタックの管理に役立ついくつかの組み込み関数が用意されています。実行するまでわからない値をプロパティに代入するには、テンプレートで組み込み関数を使用します。

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html

intrinsic:内在的

!Ref

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セクションに記載されている。

Fn::Sub

  • https://chariosan.com/2019/08/11/cfn_fnsub/
  • 実行時に、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

Fn::GetAtt: [ logicalNameOfResource, attributeName ]
# or
!GetAtt logicalNameOfResource.attributeName

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html

別スタックのリソースを参照

インポート(参照) Fn::ImportValue
  • 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

CodePipelineをCloudformationで実行

Cloudformation ECS

AWSのECSのExecutionRoleArnとTaskRoleArnの違いがわからない

  • ExecutionRoleArnECSが持つべきロール
  • TaskRoleArn:タスクが持つべきロール
    • タスクでCloudWatchへのログを出力していればCloudWatchのポリシーが必要

CLI

スタックを作成

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 状態のままになっている場合に、このスタックを更新するにはどうすればよいですか?

公式ドキュメント

  • AWS リソースおよびプロパティタイプのリファレンス
  • https://docs.aws.amazon.com/en_us/AWSCloudFormation/latest/UserGuide/template-reference.html (テンプレートリファレンス 英語)
  • https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/Welcome.html (英語)
  • Intrinsic function reference
  • 組み込み関数リファレンス
  • CodePipelineをCloudFormation(CodepipelineをCloudFormationで実行するCloudFormationのドキュメント)
    • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/continuous-delivery-codepipeline-basic-walkthrough.html
  • CodePipelineをCloudFormation(CodepipelineをCloudFormationで実行するCodePipelineのドキュメント)
    • https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/action-reference-CloudFormation.html
    • https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CloudFormation.html
    • https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/tutorials-cloudformation-action.html
    • https://docs.aws.amazon.com/codepipeline/latest/userguide/tutorials-cloudformation-action.html
  • https://docs.aws.amazon.com/code-samples/latest/catalog/code-catalog-cloudformation-codepipeline.html サンプル

Intrinsic 固有の、本質的な

参考記事