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

问题描述 投票:0回答:1

我们希望使用特定标记标记云信息堆栈的每个资源(出于计费和财务原因)。这包括在(/ 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等,这里的任何帮助都将不胜感激。我们当前创建实例,然后在创建堆栈后进行标记。但这似乎有点脆弱,应该有一个更容易的方式来做这个....

amazon-web-services configuration tags config amazon-cloudformation
1个回答
1
投票

Not possible with cloud formation

事实证明,这与aws cloudformation无法实现。这个问题暗示了我的答案:https://serverfault.com/questions/876942/create-new-ec2-instance-with-existing-ebs-volume-as-root-device-using-cloudforma

Solution with terraform

“terraform”非常简单:


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

Our solution (Jenkins and shell script)

我们使用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 ::卷和标签传播的所有提示的Thx。

© www.soinside.com 2019 - 2024. All rights reserved.