Bỏ qua tới nội dung
CloudDojo
Phần 1/4 · 5 phút

CloudFormation template, stack và change set

0/2
Từ khóa của phần nàyChọn một từ để xem nghĩa trước khi đọc

Tạo hạ tầng bằng thao tác thủ công trên AWS Management Console khó lặp lại chính xác; mô tả hạ tầng trong template giúp tạo, cập nhật và tái tạo môi trường một cách tự động.

Template là một file YAML hoặc JSON mô tả các tài nguyên cần tạo. CloudFormation đọc template và tạo một stack: nhóm tài nguyên được tạo, cập nhật và xoá cùng nhau. Để thay đổi hạ tầng, cập nhật template và thực hiện update stack; change set cho phép xem trước các thay đổi CloudFormation sẽ áp dụng. Chạy ba tình huống của MFTECH:

Stackorders-db—
  Parameters:
    DbClass:
      Type: String
      AllowedValues: [db.t3.medium, db.r6g.large]
  Resources:
    DbSecurityGroup:  # chỉ cho phép 5432 từ sg-app
    DbSubnetGroup:    # hai private subnet, hai AZ
    Database:
      Type: AWS::RDS::DBInstance
      Properties:
        DBInstanceClass: !Ref DbClass
        VPCSecurityGroups: [!GetAtt DbSecurityGroup.GroupId]
        DBSubnetGroupName: !Ref DbSubnetGroup
        MultiAZ: true
        StorageEncrypted: true
        BackupRetentionPeriod: 7
        DeletionProtection: true
Stack orders-dbDbSecurityGroupAWS::EC2::SecurityGroupchưa tạoDbSubnetGroupAWS::RDS::DBSubnetGroupchưa tạoDatabaseAWS::RDS::DBInstancechưa tạo

Chọn Create stack để xem lần lượt các stack event.

Xem CloudFormation dựng từng resource của stack theo template, cập nhật qua change set và rollback khi gặp lỗi.

Khả năng đọc hiểu template quan trọng tương đương khả năng viết template. Dưới đây là security group của web tier trong template của MFTECH. Gửi từng kết nối thử nghiệm để xem tác dụng của mỗi rule:

WebSecurityGroup:
  Type: AWS::EC2::SecurityGroup
  Properties:
    GroupDescription: web của MFTECH
    VpcId: !Ref Vpc
    SecurityGroupIngress:
      - IpProtocol: tcp      # rule 1
        FromPort: 80
        ToPort: 80
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp      # rule 2
        FromPort: 22
        ToPort: 22
        CidrIp: 203.0.113.25/32
TCP 80 từ 14.232.8.9
ClientIP bất kỳSecurity group sg-webinbound: 2 rule trong templateEC2 webNginx :80, SSH :22

Chọn một kết nối, sau đó chọn Gửi request để xác định rule nào trong template cho phép kết nối.

Gửi từng kết nối tới security group khai báo trong template và xem rule nào cho phép.
Ghi nhớ:
  • Đảm bảo mọi tài nguyên được tạo theo cùng tiêu chuẩn: đưa tiêu chuẩn vào CloudFormation template, chỉ cho phép người dùng chọn tham số trong danh sách được phép.
  • Thay đổi hạ tầng bằng cách cập nhật template và update stack.
  • Khi quá trình tạo gặp lỗi giữa chừng, theo mặc định stack tự động rollback (có thể chọn giữ lại tài nguyên đã tạo thành công).
  • Dùng CloudFormation với tài nguyên thuộc namespace AWS::, Alexa:: và Custom::* không phát sinh phí bổ sung; chỉ tính phí tài nguyên AWS được tạo.
  • SecurityGroupIngress là inbound rule của security group và chỉ hỗ trợ rule cho phép.
Part 1/4 · 5 phút

CloudFormation templates, stacks and change sets

0/2
Từ khóa của phần nàyChọn một từ để xem nghĩa trước khi đọc

Building infrastructure manually in the AWS Management Console is hard to repeat exactly; describing it in a template lets environments be created, updated and rebuilt automatically.

A template is a YAML or JSON file that describes the resources to create. CloudFormation reads the template and creates a stack: a group of resources that are created, updated and deleted together. To change the infrastructure, update the template and perform an update stack; a change set lets you preview the changes CloudFormation will apply. Run three MFTECH scenarios:

Stackorders-db—
  Parameters:
    DbClass:
      Type: String
      AllowedValues: [db.t3.medium, db.r6g.large]
  Resources:
    DbSecurityGroup:  # chỉ cho phép 5432 từ sg-app
    DbSubnetGroup:    # hai private subnet, hai AZ
    Database:
      Type: AWS::RDS::DBInstance
      Properties:
        DBInstanceClass: !Ref DbClass
        VPCSecurityGroups: [!GetAtt DbSecurityGroup.GroupId]
        DBSubnetGroupName: !Ref DbSubnetGroup
        MultiAZ: true
        StorageEncrypted: true
        BackupRetentionPeriod: 7
        DeletionProtection: true
Stack orders-dbDbSecurityGroupAWS::EC2::SecurityGroupchưa tạoDbSubnetGroupAWS::RDS::DBSubnetGroupchưa tạoDatabaseAWS::RDS::DBInstancechưa tạo

Chọn Create stack để xem lần lượt các stack event.

Xem CloudFormation dựng từng resource của stack theo template, cập nhật qua change set và rollback khi gặp lỗi.

Being able to read a template matters as much as being able to write one. Below is the web tier’s security group in MFTECH’s template. Send each test connection to see the effect of each rule:

WebSecurityGroup:
  Type: AWS::EC2::SecurityGroup
  Properties:
    GroupDescription: web của MFTECH
    VpcId: !Ref Vpc
    SecurityGroupIngress:
      - IpProtocol: tcp      # rule 1
        FromPort: 80
        ToPort: 80
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp      # rule 2
        FromPort: 22
        ToPort: 22
        CidrIp: 203.0.113.25/32
TCP 80 từ 14.232.8.9
ClientIP bất kỳSecurity group sg-webinbound: 2 rule trong templateEC2 webNginx :80, SSH :22

Chọn một kết nối, sau đó chọn Gửi request để xác định rule nào trong template cho phép kết nối.

Gửi từng kết nối tới security group khai báo trong template và xem rule nào cho phép.
Ghi nhớ:
  • Make sure every resource is created to the same standard: put the standard into a CloudFormation template and let users choose parameters only from an allowed list.
  • Change infrastructure by updating the template and updating the stack.
  • When creation fails midway, by default the stack rolls back automatically (you can choose to keep the resources that were created successfully).
  • Using CloudFormation with resources in the AWS::, Alexa:: and Custom::* namespaces incurs no additional charge; you pay only for the AWS resources created.
  • SecurityGroupIngress is a security group inbound rule and supports allow rules only.

Kiểm tra nhanh

Trả lời để ghi nhận đã đọc phần này. Câu trả lời sai sẽ được nhắc ôn lại vào những ngày sau.

Một nhân sự mới tạo database bằng console nhưng không bật Multi-AZ và backup. MFTECH muốn mọi database được tạo theo cùng tiêu chuẩn. Làm gì?
Quá trình tạo stack gặp lỗi khi đã tạo xong một nửa tài nguyên. Theo mặc định CloudFormation làm gì?

Đã trả lời 0/2 câu