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?
CodePudding user response:
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).
CodePudding user response:
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?