Direkt zum Inhalt

Wie tagge ich ein Root-Volume von einer von CloudFormation erstellten Instance?

Lesedauer: 6 Minute
0

Ich möchte das Root-Volume meiner Amazon Elastic Compute Cloud (Amazon EC2)-Instances taggen, die ich in AWS CloudFormation erstellt habe.

Lösung

Um Amazon-EC2-Tags zu deinen angefügten Volumes hinzuzufügen, füge PropagateTagstoVolumeOnCreation in der CloudFormation-Vorlage hinzu und setze den Wert auf Wahr.

Gehe wie folgt vor, um ein Root-Volume zu taggen:

  1. Öffne die CloudFormation-Konsole.

  2. Wähle im Dashboard Create stack - With new resources (standard) (Stack erstellen – Mit neuen Ressourcen (Standard) aus).

  3. Wähle in Voraussetzung – Vorlage vorbereiten die Option Aus Infrastructure Composer heraus entwickeln und dann In Infrastructure Composer erstellen aus.

  4. Wähle Vorlage und verwende dann entweder die YAML- oder JSON-Vorlage in deinem Code Editor.
    JSON-Vorlage:

    {
        "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-Vorlage:

    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

    Wichtig: Füge die gewünschten Tags in der Tags-Eigenschaft der AWS::EC2::Instance-Tags hinzu.

  5. Wähle Vorlage erstellen aus. Bestätige das und fahre mit CloudFormation fort.

  6. Wähle Weiter aus.

  7. Gib unter Stack-Name einen Namen für deinen Stack ein.

  8. Gib im Abschnitt Parameter die Informationen ein, die auf den Anforderungen deiner Umgebung basieren, einschließlich des Instance-Typs, EC2-Schlüsselpaars und Amazon Machine Image (AMI).

  9. Wähle Weiter aus.

  10. Gib im Abschnitt Optionen die Informationen für deinen Stack ein. Wähle dann Weiter.

  11. Aktiviere den CloudFormation-Stack, um eine AWS Identity and Access Management (IAM)-Ressource zu erstellen. Wenn du mit den Bedingungen einverstanden bist, aktivieren das Kontrollkästchen I acknowledge that AWS CloudFormation might create IAM resources (Ich nehme zur Kenntnis, dass AWS CloudFormation möglicherweise IAM-Ressourcen erstellt).

  12. Wähle Absenden aus.

Root-Volume der Instance taggen

Gehe wie folgt vor:

  1. Öffne die Amazon-EC2-Konsole.
  2. Wähle im Navigationsbereich im Abschnitt Elastic Block Store die Option Volumes aus.
  3. Gib im Feld Filter das Tag ein, das du im CloudFormation-Stack festgelegt hast, um zu bestätigen, dass das Volume getaggt wurde.
AWS OFFICIALAktualisiert vor einem Jahr