CloudFormation template, stack và change set
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:
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
Chọn Create stack để xem lần lượt các stack event.
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/32Chọ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.
- Đả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.
CloudFormation templates, stacks and change sets
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:
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
Chọn Create stack để xem lần lượt các stack event.
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/32Chọ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.
- 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.