简体   繁体   中英

Referencing an EC2 instance's name tag in AWS CloudFormation without parameterization

I have a CloudFormation stack that creates an EC2 instance and gives it a name tag.

I want to create a CloudWatch alarm, and reference the EC2 instance's name in the alarm's name - something like AlarmName: !Sub "Status Check Alarm - ${EC2InstanceName}" .

!Ref will allow me to reference the CloudFormation script's parameters, but I don't want to parameterize the EC2 instance name - I don't want or need that to be customizable, and I don't want users to have the ability to choose a custom name for the server.

I tried outputting the EC2 instance name so I could !Ref that, but I got an Invalid template resource property 'Outputs' error, so I don't know if my approach even works:

EC2Instance:
  Properties: ...
  Type: AWS::EC2::Instance
  Outputs:
    EC2InstanceName:
      Description: The server's name.
      Value: !GetAtt EC2Instance.Tags.Name
      Export:
        Name: "EC2InstanceName"

How do I reference the EC2 instance's name without parameterizing the name at the top-level of the script?

EDIT:

I ended up using parameters anyway so I could !Ref them. I guess you could also set up an "allowed values" list containing only a single value that matches the default. It's lame but it works, I guess.

Parameters:
  EC2InstanceName:
    Type: String
    Default: "web-server-blah"
    Description: The name of the EC2 instance.

You can use !GetAtt only for attributes which are specifically named in the documentation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html Tags are not among them.

But if you provide a different tag for your instance, then you can refer it without even exporting it (providing that it is a constant value).

I see what you are trying to do, but AWS does not support everything you would like to work out of the box. One way how I imagine it can be done - and you may not like it - is either via a macro or a custom resource (lambda function).

Can't use just use !Ref EC2Instance ? I realize it won't be the friendly "Name" tag value, but it could be more useful, especially if you have duplicates of the same "Name". It would make your alarm be something like "Status Check Alarm - i-123456789".

Whereas if you use the name it might be something more like 10 alarms that read "Status Check Alarm - WWWServer", but now which WWWServer?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM