Search code examples
amazon-web-servicesaws-cloudformationaws-cloudformation-custom-resource

AWS Cloudformation: How to use username as a tag


I need to create a Cloudformation Script for an ec2 instance which should have tags specifying among other things, the aws username of the person who ran the script to create the instance. What is the variable name to get the IAM Username? The tagging part of my script is shown below. Whereas the AccountId can be retrieved, I'm unable to get the username.

    Tags:
    - Key: Name
      Value: !Ref InstanceName
    - Key: team
      Value: !Ref Team
    - Key: awsAccount
      Value: !Sub ${AWS::AccountId}
    - Key: userName
      Value: !Sub ${AWS::IAM::User}
    - Key: purpose
      Value: !Ref Purpose

Solution

  • Unfortunately there is no such pseudo parameter in CloudFormation. The list of all available parameters, such as AWS::AccountId is here.

    One way around this is to create a parameter for your CloudFormation template (as you do with Purpose or team), e.g.

    Parameters:
        UserName:
          Type: String
    

    and reference it in your Tags:

    - Key: userName
      Value: !Ref UserName