Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudformation when to use getatt, ref, vs ${}

I'm trying to figure out the differences between GetAtt vs Ref vs ${}.

From what I understand you use GetAtt to refer to objects within the same template only and Ref can be used to refer to anything? Plus when would I use ${}?

like image 530
tmp dev Avatar asked Oct 30 '19 01:10

tmp dev


People also ask

What is the difference between GetAtt and ref?

GetAtt is essentially the same as the 2nd function of Ref above, it also returns an attribute of the resource that you created within your resource, but while ref returns only a default attribute, GetAtt allows you to choose from different attributes to return.

When using the ref function in CloudFormation what do we get back if we pass in the logical ID of an AWS :: EC2 :: instance object?

For some resources, an identifier is returned that has another significant meaning in the context of the resource. An AWS::EC2::EIP resource, for instance, returns the IP address, and an AWS::EC2::Instance returns the instance ID. You can also use Ref to add values to Output messages.

What is ref in YAML?

YAML. Ref: logicalName. Ref function is used to enter the logical name of another resource. Ref function returns the value that is predefined for each resource. For example, your EC2 instance has a logical name of EC2.


1 Answers

Ref can be used for two things:

  1. To return the value of a parameter that you passed in via the parameters section of the template.
  2. When you ref the logical ID of another resource in your template, Ref returns what you could could consider as a default attribute for that type of resource. So using ref for an EC2 instance will return the instance ID, Ref'ing an s3 bucket resource, it will return the bucket name. You can look at the bottom of each cloudformation resources page in the AWS docs to see what this value will be (See Return Values section: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html)

GetAtt is essentially the same as the 2nd function of Ref above, it also returns an attribute of the resource that you created within your resource, but while ref returns only a default attribute, GetAtt allows you to choose from different attributes to return.

Example, GetAtt for an EC2 instance gives you the option to return the AvailabilityZone, PrivateDnsName, PublicDNSName, etc of an instance - whereas Ref will only return the InstanceID. The different attributes you can return are different per resource type. You can also look at the bottom of each cloudformation resources page in the AWS docs to see what attributes you can all return (See Return Values section: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html)

${} is another way to reference parameters passed in through the parameters section of the template.

All of this is in the AWS documentation though.

like image 102
WarrenG Avatar answered Oct 13 '22 01:10

WarrenG