Search code examples
amazon-web-servicesaws-cloudformationtaggingblock-device

AWS Cloudformation: Tagging block device from auto-scaling launch configuration


I have created an autoscaling group and its launch configuration via Cloudformation template.

The Autoscaling group has only one tag called 'name'. There is a block device mapping for the ec2 instances in the launch configuration as below:

ASGLaunchConfiguration:
    Type: AWS::AutoScaling::LaunchConfiguration
    DependsOn: CloudwatchConfigSsm
    Properties:
      ImageId: !Ref PresentationAMIId
      InstanceType: !Ref PresentationInstanceType
      AssociatePublicIpAddress: false
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeSize: 20
            VolumeType: gp2
            Encrypted: true

But the problem is the tag called 'name' that is defined in the associated autoscaling group is not propagated to the created EBS volume. The tag definition in the autoscaling group is below:

Tags:
        - Key: 'Name'
          Value: !Sub 'presentation-server-${Env}'
          PropagateAtLaunch: true

Further, I need to add some other important tags. How I can achieve this?

Update:

I created the below launch template:

ASGLaunchTemplate:
    Type: AWS::EC2::LaunchTemplate
    DependsOn: CloudwatchConfigSsm
    Properties:
      LaunchTemplateName: !Sub 'ec2-launch-template-${Region}-${Env}'
      TagSpecifications:
        - ResourceType: instance
          Tags:
          - Key: System
            Value: 'xxx'
          - Key: Env
            Value: !Sub '${Env}'
          - Key: EnvNumber
            Value: '01'
          - Key: Country
            Value: !Sub '${Region}'
          - Key: Company
            Value: 'xxx'
          - Key: Global
            Value: 'all'
          - Key: CostCenter
            Value: 'xxxx'
          - Key: 'Name'
            Value: !Sub 'presentation-server-${Env}'
        - ResourceType: volume
          Tags:
          - Key: 'Name'
            Value: !Sub 'presentation-server-ebs-volume-${Env}'
          - Key: System
            Value: 'xxx'
          - Key: Env
            Value: !Sub '${Env}'
          - Key: EnvNumber
            Value: '01'
          - Key: Country
            Value: !Sub '${Region}'
          - Key: Company
            Value: 'xxx'
          - Key: Global
            Value: 'all'
          - Key: CostCenter
            Value: 'xxxx'
      LaunchTemplateData:
        ImageId: !Ref PresentationAMIId
        InstanceType: !Ref PresentationInstanceType
        NetworkInterfaces:
          - AssociatePublicIpAddress: false
        BlockDeviceMappings:
          - DeviceName: /dev/xvda
            Ebs:
              VolumeSize: 20
              VolumeType: gp2
              Encrypted: true

Got below error while updating the stack:

You can specify only one TagSpecification parameter in the request. Ensure that the request includes only one TagSpecification parameter and try again.


Solution

  • It should be:

    ASGLaunchTemplate:
        Type: AWS::EC2::LaunchTemplate
        DependsOn: CloudwatchConfigSsm
        Properties:
          LaunchTemplateName: !Sub 'ec2-launch-template-${Region}-${Env}'
          LaunchTemplateData:
            ImageId: !Ref PresentationAMIId
            TagSpecifications:
              - ResourceType: instance
                Tags:
                - Key: System
                  Value: 'xxx'
                - Key: Env
                  Value: !Sub '${Env}'
                - Key: EnvNumber
                  Value: '01'
                - Key: Country
                  Value: !Sub '${Region}'
                - Key: Company
                  Value: 'xxx'
                - Key: Global
                  Value: 'all'
                - Key: CostCenter
                  Value: 'xxxx'
                - Key: 'Name'
                  Value: !Sub 'presentation-server-${Env}'
              - ResourceType: volume
                Tags:
                - Key: 'Name'
                  Value: !Sub 'presentation-server-ebs-volume-${Env}'
                - Key: System
                  Value: 'xxx'
                - Key: Env
                  Value: !Sub '${Env}'
                - Key: EnvNumber
                  Value: '01'
                - Key: Country
                  Value: !Sub '${Region}'
                - Key: Company
                  Value: 'xxx'
                - Key: Global
                  Value: 'all'
                - Key: CostCenter
                  Value: 'xxxx'        
            InstanceType: !Ref PresentationInstanceType
            NetworkInterfaces:
              - AssociatePublicIpAddress: false
            BlockDeviceMappings:
              - DeviceName: /dev/xvda
                Ebs:
                  VolumeSize: 20
                  VolumeType: gp2
                  Encrypted: true