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.