AWS CloudFormation Concepts Intrinsic Functions: Difference between revisions
(→Ref) |
|||
Line 17: | Line 17: | ||
{{External|[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html Ref]}} | {{External|[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html Ref]}} | ||
The full form of the function is: | |||
The short form of the function is: | |||
!Ref ''reference'' | |||
Note that no ${} should be used around the reference, the parser will actually detect that as syntax error. | |||
The intrinsic function Ref returns the value of the object it refers to, such as a [[AWS_CloudFormation_Concepts#Input_Parameters|parameter]] or [[AWS_CloudFormation_Concepts#Resources|resource]]. When a parameter logical name is specified, it returns the value of the parameter. When a resource logical name is specified, it returns a value that can be typically used to refer to that resource, such as a physical ID. | The intrinsic function Ref returns the value of the object it refers to, such as a [[AWS_CloudFormation_Concepts#Input_Parameters|parameter]] or [[AWS_CloudFormation_Concepts#Resources|resource]]. When a parameter logical name is specified, it returns the value of the parameter. When a resource logical name is specified, it returns a value that can be typically used to refer to that resource, such as a physical ID. |
Revision as of 01:58, 18 April 2019
External
Internal
Overview
All intrinsic functions have full function name:
Fn::ImportValue: stack_output_export_name
and a short form:
!ImportValue stack_output_export_name
Fn::Ref, !Ref
The full form of the function is:
The short form of the function is:
!Ref reference
Note that no ${} should be used around the reference, the parser will actually detect that as syntax error.
The intrinsic function Ref returns the value of the object it refers to, such as a parameter or resource. When a parameter logical name is specified, it returns the value of the parameter. When a resource logical name is specified, it returns a value that can be typically used to refer to that resource, such as a physical ID.
!Ref AWS::StackName !Ref MyParameter !Ref MyResourceName
Fn::Sub, !Sub
Sub substitutes variables in an input string with specified values.
If only template parameters, resource logical IDs and resource attributes are substituted in the String parameter, no variable map is required:
!Sub '${TemplateParameterA} is a ${TemplateParameterB}' !Sub '${AWS::Region}-something'
Sub can be used as a replacement for GetAtt, it seems to extract the attributes of a resource and place them in a string just fine. This is an example of how to obtain the ARN of a resource created in the same template:
Using Sub to Configure the ARN of a Resource Created by the Template
Resources:
# this resource has an ARN
AccessLogGroup:
...
...
# this resource needs the ARN
Stage:
Type: AWS::ApiGateway::Stage
Properties:
...
AccessLogSetting:
DestinationArn: !Sub '${AccessLogGroup.Arn}'
...
GetAtt
An intrinsic function that returns arbitrary attributes of a resource (Ref returns just important value associated with the resource). The function takes two parameters: the logical name of the resource and the attribute to be retrieved, as an array.
Using GetAtt to Configure the ARN of a Resource Created by the Template
Resources:
# this resource has an ARN
AccessLogGroup:
...
...
# this resource needs the ARN
Stage:
Type: AWS::ApiGateway::Stage
Properties:
...
AccessLogSetting:
DestinationArn: !GetAtt AccessLogGroup.Arn
...
Join
The Fn::Join function takes two parameters, a delimiter that separates the values you want to concatenate and an array of values in the order that you want them to appear.
!Join [ delimiter, [ comma-delimited list of values ] ]
!Join ['-', [ a, b, c ]]
!Join ['-', !Split ['/', !Sub '${something}-something-else']]
returns "a:b:c"
Split
!Split ['.', "www.example.com"]
!Split ['/', !Sub '${something}-something-else']
returns ["www", "example", "com"].
Select
ImportValue
The Fn::ImportValue returns the value of an output exported by another stack. The function is used to create cross-stack references.
Can be used as a key in a YAML structure:
... ServiceRole: Fn::ImportValue: ...
or a value:
... EnvironmentVariables: - Name: TARGET_BUCKET Value: Fn::ImportValue: !Sub '${AWS::Region}-BuildBucket'
FindInMap
!FindInMap [ MapName, TopLevelKey, SecondLevelKey ]