toge510 / aws

0 stars 0 forks source link

AWS CloudFormation Master Class v2 [2024] #1

Open toge510 opened 5 months ago

toge510 commented 5 months ago

11

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-s3-bucket.html

Create S3 bucket

Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties: {}

AWS CloudFormationとテンプレートによってプロビジョニングしたAWSリソースの集まりを「スタック」と言います。またAWS CloudFormationによって作成したスタックは、スタック単位で管理します。つまり、スタック内のある1つのAWSリソースを削除する、といったことはできません。AWSリソースを削除する場合は、スタックごとになります。

テンプレートそのものは、JSONもしくはYAML形式のテキストファイルです。テキストエディタがあれば誰でも作成できます。テンプレートでは、パラメータの定義、リソースの作成、実際の設定などを行います。またスタックの規模が大きくなると当然テンプレートも大きくなります。大きなテキストを1ファイルで管理すると、間違えて修正するなど問題も増えてくるので、できればテンプレートをスタックの機能ごとに分割することをおすすめします。

https://cloudnavi.nhn-techorus.com/archives/4244

12

behaviors

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html

sample

top level sections

A template is a JSON or YAML text file that contains the configuration information about the AWS resources you want to create in the stack. For this walkthrough, the sample template includes six top-level sections: AWSTemplateFormatVersion, Description, Parameters, Mappings, Resources, and Outputs; however, only the Resources section is required.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/GettingStarted.Walkthrough.html

12

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-s3-bucket.html

PropertiesのUpdate requiresにおいて、Updateの際にリソース削除する必要があるかわかる。

toge510 commented 4 months ago

Section5

Parameters

SSM parameter, Systems Manager Parameter Store

Parameters:
  InstanceType:
    Description: WebServer EC2 instance type
    Type: AWS::SSM::Parameter::Value<String>
    Default: /dev/ec2/instanceType

  ImageId:
    Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
    Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2

Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      ImageId: !Ref ImageId
toge510 commented 4 months ago

Section6

Optional attributes for Resources

DependsOn

Docker composeのdependsonと同意

以下の場合、MyInstanceは、MyS3zBucketの後に作成される。削除されるときは、MyInstaceが削除されて、その後にByS3Bucketが削除されるという順番。

Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket

  # the EC2 instance will be created after the S3 bucket
  MyInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref ImageId
      InstanceType: t2.micro
    DependsOn: MyS3Bucket

DeletionPolicy

stackを削除(デフォルト)したときに、リソースを保持するか、削除するか、snapshotを保存するかを指定できる

UpdateReplacePolicy

Replacement behaviorを要するupdate properties時に、リソースをどうするかを指定

toge510 commented 4 months ago

Section7

mapping, Fn::FindlnMap

Pseudo Parameters

Section8

あるスタックでOutputをExportすると、他のスタックから、そのOutputをreference可能である。