通过 CloudFormation 在根卷的 AWS::EC2::Instances BlockStorage 上设置标签

Set Tag on AWS::EC2::Instances BlockStorage of root Volume via CloudFormation

我们想用特定标签标记云形成堆栈的每个资源(出于计费和财务原因)。这包括安装在 (/dev/sda1) 下的主分区上使用的 主存储。

这就是我们拥有的:

---
AWSTemplateFormatVersion: '2010-09-09'
Description: The Name
Parameters:
  InstanceType:
    Description: EC2 instance type
    Type: String
    Default: t3.small
    AllowedValues:
    - t3.nano
    - ...
  InstanceName:
    Description: Name Tag
    Type: String
Resources:
  TheECCInstance:
    Type: AWS::EC2::Instance
    Properties:
      KeyName: jenkins
      ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
      InstanceType:
        Ref: InstanceType
      SubnetId: subnet-0e9c7d7c2711aaf9e
      BlockDeviceMappings:
        - DeviceName: "/dev/sda1"
          Ebs:
            VolumeSize:
              Ref: EBSBlockSize
            VolumeType: gp2
      Tags:
      - Key: Name
        Value:
          Ref: InstanceName
      - Key: Type
        Value: TheType

Mappings:
  RegionMap:
    'ap-northeast-3':
      NAME: ap-northeast-3b
      AMI: 'ami-05e896b95030bd37c'
    'sa-east-1':
      NAME: sa-east-1b
      AMI: 'ami-03c6239555bb12112'
    'eu-west-1':
      NAME: eu-west-1b
      AMI: 'ami-00035f41c82244dab'
    ...

Outputs:
  ...

不介意使用类似的东西:

  RootVolume:
    Type: AWS::EC2::Volume      // Or Something in that direction (EFS / EBS / whatever)
    Properties:
      Size:
        Ref: EBSBlockSize
      VolumeType: gp2
      AvailabilityZone: !FindInMap [RegionMap, !Ref 'AWS::Region', NAME]
      Tags:
        - Key: Type
          Value: TheType

并安装它。将其作为主分区附加似乎是不可能的。作为 AWS::EC2::Volume、AWS::EFS 等。如有任何帮助,我们将不胜感激。我们目前创建实例,然后在堆栈创建后标记。但这似乎有些脆弱,应该有更简单的方法来做到这一点....

不可能形成云

事实证明,这对于 aws cloudformation 是不可能的。 这个问题提示我这个答案:https://serverfault.com/questions/876942/create-new-ec2-instance-with-existing-ebs-volume-as-root-device-using-cloudforma

terraform 解决方案

使用 "terraform" 非常简单:


resource "aws_instance" "someName" {
  ami           = "ami-***********"
  instance_type = "t2.*******"
  tags          = {
    Type = "InstanceTag"
  }
  volume_tags   = {
    Type = "VolumeTag"
  }
}

我们的解决方案(Jenkins 和 shell 脚本)

我们使用 cloudformation 通过 jenkins 生成我们的 CI-实例。迁移到 Terraform 会导致比我们愿意承担更多的工作。我们需要用于成本计算的标签。因此,当意识到无法使用 cloudformation 时,我们使用 cli 在实例创建后对其进行标记。

显然,awscli 必须安装。凭据来自环境变量。

这是我们使用的脚本:

sleep 1m
EC2_VOLUMES=$(aws ec2 describe-instances --region "${INSTANCE_REGION}" --filter "Name=tag-key,Values=Type" "Name=tag-value,Values=Jenkins" --filter "Name=tag-key,Values=Name" "Name=tag-value,Values=BackendJenkins${BUILD_NUMBER}" --query "Reservations[*].Instances[*].BlockDeviceMappings[*].Ebs.[VolumeId]" --output text)

echo $AWS_SECRET_ACCESS_KEY | base64 -i > foo.txt

while read -r RESOURCE; do
    # We set the name and the type tag
    aws ec2 create-tags --resources "$RESOURCE" --region "${INSTANCE_REGION}" --tags "Key=Type,Value=TheTagValue"
    aws ec2 create-tags --resources "$RESOURCE" --region "${INSTANCE_REGION}" --tags "Key=Name,Value=TheInstanceName${BUILD_NUMBER}"
done <<< "$EC2_VOLUMES"

谢谢关于生成 EC2::Volumes 和标签传播的所有提示。