如何為透過 CloudFormation 建立的執行個體根磁碟區加上標籤?
我想要為我在 AWS CloudFormation 中建立的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體根磁碟區加上標籤。
解決方法
若要為已連接的磁碟區加入 Amazon EC2 標籤,請在 CloudFormation 範本中加入 PropagateTagstoVolumeOnCreation,並將其值設為 True。
若要為根磁碟區加上標籤,請執行以下步驟:
-
開啟 CloudFormation console (CloudFormation 主控台)。
-
在儀表板上,選擇 Create stack - With new resources (standard) (建立堆疊 - 使用新資源 (標準))。
-
在 Prerequisite - Prepare (先決條件 - 準備) 範本中,選擇 Build from Infrastructure Composer (從 Infrastructure Composer 建置),然後選擇 Create in Infrastructure Composer (在 Infrastructure Composer 中建立)。
-
選擇 Template (範本),然後將 YAML 或 JSON 範本使用到您的程式碼編輯器中。
JSON 範本:{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "AWS CloudFormation Sample Template Tagging Root Volumes of EC2 Instances: This template shows you how to automatically tag the root volume of the EC2 instances that are created through the AWS CloudFormation template. This is done through the UserData property of the AWS::EC2::Instance resource. **WARNING** This template creates two Amazon EC2 instances and an IAM role. You will be billed for the AWS resources used if you create a stack from this template.", "Parameters": { "KeyName": { "Type": "AWS::EC2::KeyPair::KeyName", "Description": "Name of an existing EC2 KeyPair to enable SSH access to the ECS instances." }, "InstanceType": { "Description": "EC2 instance type", "Type": "String", "Default": "t2.micro", "AllowedValues": [ "t2.micro", "t2.small", "t2.medium", "t2.large", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge", "m4.large", "m4.xlarge", "m4.2xlarge", "m4.4xlarge", "m4.10xlarge", "c4.large", "c4.xlarge", "c4.2xlarge", "c4.4xlarge", "c4.8xlarge", "c3.large", "c3.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", "i2.xlarge", "i2.2xlarge", "i2.4xlarge", "i2.8xlarge" ], "ConstraintDescription": "Please choose a valid instance type." }, "InstanceAZ": { "Description": "EC2 AZ.", "Type": "AWS::EC2::AvailabilityZone::Name", "ConstraintDescription": "Must be the name of an Availability Zone." }, "WindowsAMIID": { "Description": "The Latest Windows 2016 AMI taken from the public Systems Manager Parameter Store", "Type": "AWS::SSM::Parameter::Value<String>", "Default": "/aws/service/ami-windows-latest/Windows_Server-2016-English-Full-Base" }, "LinuxAMIID": { "Description": "The Latest Amazon Linux 2 AMI taken from the public Systems Manager Parameter Store", "Type": "AWS::SSM::Parameter::Value<String>", "Default": "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" } }, "Resources": { "WindowsInstance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": { "Ref": "WindowsAMIID" }, "InstanceType": { "Ref": "InstanceType" }, "AvailabilityZone": { "Ref": "InstanceAZ" }, "IamInstanceProfile": { "Ref": "InstanceProfile" }, "KeyName": { "Ref": "KeyName" }, "PropagateTagsToVolumeOnCreation": "true", "Tags": [ { "Key": "Name", "Value": { "Ref": "AWS::StackName" } } ], "BlockDeviceMappings": [ { "DeviceName": "/dev/sdm", "Ebs": { "VolumeType": "io1", "Iops": "200", "DeleteOnTermination": "true", "VolumeSize": "10" } } ] } }, "LinuxInstance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": { "Ref": "LinuxAMIID" }, "InstanceType": { "Ref": "InstanceType" }, "AvailabilityZone": { "Ref": "InstanceAZ" }, "IamInstanceProfile": { "Ref": "InstanceProfile" }, "KeyName": { "Ref": "KeyName" }, "PropagateTagsToVolumeOnCreation": "true", "Tags": [ { "Key": "Name", "Value": { "Ref": "AWS::StackName" } } ], "BlockDeviceMappings": [ { "DeviceName": "/dev/sdm", "Ebs": { "VolumeType": "io1", "Iops": "200", "DeleteOnTermination": "true", "VolumeSize": "10" } } ] } }, "InstanceRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "ec2.amazonaws.com" ] }, "Action": [ "sts:AssumeRole" ] } ] }, "Path": "/", "Policies": [ { "PolicyName": "taginstancepolicy", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ec2:Describe*" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "ec2:CreateTags" ], "Resource": [ { "Fn::Sub": "arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:volume/*" }, { "Fn::Sub": "arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance/*" } ] } ] } } ] } }, "InstanceProfile": { "Type": "AWS::IAM::InstanceProfile", "Properties": { "Path": "/", "Roles": [ { "Ref": "InstanceRole" } ] } } } }YAML 範本:
AWSTemplateFormatVersion: 2010-09-09 Description: >- AWS CloudFormation Sample Template Tagging Root Volumes of EC2 Instances: This template shows you how to automatically tag the root volume of the EC2 instances that are created through the AWS CloudFormation template. This is done through the UserData property of the AWS::EC2::Instance resource. **WARNING** This template creates two Amazon EC2 instances and an IAM role. You will be billed for the AWS resources used if you create a stack from this template. Parameters: KeyName: Type: 'AWS::EC2::KeyPair::KeyName' Description: Name of an existing EC2 KeyPair to enable SSH access to the ECS instances. InstanceType: Description: EC2 instance type Type: String Default: t2.micro AllowedValues: - t2.micro - t2.small - t2.medium - t2.large - m3.medium - m3.large - m3.xlarge - m3.2xlarge - m4.large - m4.xlarge - m4.2xlarge - m4.4xlarge - m4.10xlarge - c4.large - c4.xlarge - c4.2xlarge - c4.4xlarge - c4.8xlarge - c3.large - c3.xlarge - c3.2xlarge - c3.4xlarge - c3.8xlarge - r3.large - r3.xlarge - r3.2xlarge - r3.4xlarge - r3.8xlarge - i2.xlarge - i2.2xlarge - i2.4xlarge - i2.8xlarge ConstraintDescription: Please choose a valid instance type. InstanceAZ: Description: EC2 AZ. Type: 'AWS::EC2::AvailabilityZone::Name' ConstraintDescription: Must be the name of an Availability Zone. WindowsAMIID: Description: >- The Latest Windows 2016 AMI taken from the public Systems Manager Parameter Store Type: 'AWS::SSM::Parameter::Value<String>' Default: /aws/service/ami-windows-latest/Windows_Server-2016-English-Full-Base LinuxAMIID: Description: >- The Latest Amazon Linux 2 AMI taken from the public Systems Manager Parameter Store Type: 'AWS::SSM::Parameter::Value<String>' Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 Resources: WindowsInstance: Type: 'AWS::EC2::Instance' Properties: ImageId: !Ref WindowsAMIID InstanceType: !Ref InstanceType AvailabilityZone: !Ref InstanceAZ IamInstanceProfile: !Ref InstanceProfile KeyName: !Ref KeyName PropagateTagsToVolumeOnCreation: 'true' Tags: - Key: Name Value: !Ref 'AWS::StackName' BlockDeviceMappings: - DeviceName: /dev/sdm Ebs: VolumeType: io1 Iops: '200' DeleteOnTermination: 'true' VolumeSize: '10' LinuxInstance: Type: 'AWS::EC2::Instance' Properties: ImageId: !Ref LinuxAMIID InstanceType: !Ref InstanceType AvailabilityZone: !Ref InstanceAZ IamInstanceProfile: !Ref InstanceProfile KeyName: !Ref KeyName PropagateTagsToVolumeOnCreation: 'true' Tags: - Key: Name Value: !Ref 'AWS::StackName' BlockDeviceMappings: - DeviceName: /dev/sdm Ebs: VolumeType: io1 Iops: '200' DeleteOnTermination: 'true' VolumeSize: '10' InstanceRole: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - ec2.amazonaws.com Action: - 'sts:AssumeRole' Path: / Policies: - PolicyName: taginstancepolicy PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - 'ec2:Describe*' Resource: '*' - Effect: Allow Action: - 'ec2:CreateTags' Resource: - !Sub 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:volume/*' - !Sub 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance/*' InstanceProfile: Type: 'AWS::IAM::InstanceProfile' Properties: Path: / Roles: - !Ref InstanceRole**重要:**在 AWS::EC2::Instance 標籤的 Tags 屬性中加入所需標籤。
-
選擇 Create template (建立範本)。接著確認,並繼續前往 CloudFormation。
-
選擇 Next (下一步)。
-
在 Stack name (堆疊名稱),輸入堆疊名稱。
-
在 Parameters (參數) 區段中,依照環境需求輸入資訊,包括執行個體類型、EC2 金鑰對和 Amazon Machine Image (AMI)。
-
選擇 Next (下一步)。
-
在 Options (選項) 區段中,輸入堆疊資訊。接著,選擇 Next (下一步)。
-
啟用 CloudFormation 堆疊,以建立 AWS Identity and Access Management (IAM) 資源。如果您同意條款,請選取 I acknowledge that AWS CloudFormation might create IAM resources (我了解 AWS CloudFormation 可能會建立 IAM 資源) 核取方塊。
-
選擇 Submit (提交)。
為執行個體的根磁碟區加上標籤
執行以下步驟:
- 開啟 Amazon EC2 console (Amazon EC2 主控台)。
- 在導覽窗格的 Elastic Block Store (彈性區塊存放區) 區段中,選擇 Volumes (磁碟區)。
- 在 Filter (篩選條件) 欄位中,輸入您在 CloudFormation 堆疊中設定的標籤,以確認磁碟區已加上標籤。
- 語言
- 中文 (繁體)

相關內容
已提問 4 年前
已提問 2 年前