Automation - aws - CloudFormation

最後更新: 2023-05-25

介紹

AWS 官方的 Infrastructure as code  (IaC) 工具

它使用 JSON / YAML 去建立 Infrastructure

由於它們都不好寫, 建議用 Designer 去建立

使用: Describes all the AWS resources that you want (EC2, RDS) in YAML / JSON


YAML

 

ec2.yaml

Description: A sample template
Resources:
  EC2Instance:                         # An inline comment
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: "ami-05ec72576b2b4738f" # U22.04
      AvailabilityZone: ap-east-1a     # 非必要
      InstanceType: t3.micro
      KeyName: lab1_key                # existing EC2 KeyPair
    Tags:
      -
        Key: Name
        Value: MyTEST
Outputs:
  InstanceId:
    Description: InstanceId of the newly created EC2 instance
    Value:
      Ref: EC2Instance
  PublicDNS:
    Description: Public DNSName of the newly created EC2 instance
    Value:
      Fn::GetAtt:
      - EC2Instance
      - PublicDnsName
  PublicIP:
    Description: Public IP address of the newly created EC2 instance
    Value:
      Fn::GetAtt:
      - EC2Instance
      - PublicIp

 


Designer

 

https://console.aws.amazon.com/cloudformation/designer

If your YAML template has "#" comments, Designer doesn't preserve those comments when converting the template to JSON.

In addition, if you modify your template in Designer (for example, if you move a resource on the canvas), your comments are lost.

 


Usage

 

List stacks

aws cloudformation list-stacks

Deploy stack

# deploy: Deploys the specified AWS CloudFormation template by creating and then executing a change set.

# --stack-name
# If you specify an existing stack, the command updates the stack.
# If you specify a new stack, the command creates it.

aws cloudformation deploy --template-file ec2.yaml --stack-name test-stack

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - test-stack

More info. about stack

# resources

aws cloudformation list-stack-resources --stack-name test-stack

{
    "StackResourceSummaries": [
        {
            "LogicalResourceId": "EC2Instance",
            "PhysicalResourceId": "i-XXX",
            "ResourceType": "AWS::EC2::Instance",
            "LastUpdatedTimestamp": "2023-05-25T05:03:45.901000+00:00",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "NOT_CHECKED"
            }
        }
    ]
}

# Description for the stacks

aws cloudformation describe-stacks      # Returns ALL stacks

aws cloudformation describe-stacks --stack-name test-stack

{
    "Stacks": [
        {
            "StackId": "arn:aws:cloudformation:ap-east-1:...",
            "StackName": "test-stack",
            "ChangeSetId": "arn:aws:cloudformation:ap-east-1:...",
            "Description": "A sample template",
            "StackStatus": "CREATE_COMPLETE",
            "Outputs": [
                {
                    "OutputKey": "InstanceId",
                    "OutputValue": "i-XXX",
                    "Description": "InstanceId of the newly created EC2 instance"
                },
                {
                    "OutputKey": "PublicIP",
                    "OutputValue": "m.m.m.m",
                    "Description": "Public IP address of the newly created EC2 instance"
                },
                {
                    "OutputKey": "PublicDNS",
                    "OutputValue": "ec2-m-m-m-m.ap-east-1.compute.amazonaws.com",
                    "Description": "Public DNSName of the newly created EC2 instance"
                }
            ],
            ...
        }
    ]
}

Delete stack

aws cloudformation delete-stack --stack-name test-stack

aws cloudformation list-stacks

{
    "StackSummaries": [
        {
            "StackName": "test-stack",
            ...
            "DeletionTime": "2023-05-25T05:17:34.951000+00:00",
            "StackStatus": "DELETE_COMPLETE",
        }
    ]
}

aws ec2 describe-instances    # Double Check

 

 

Creative Commons license icon Creative Commons license icon