Intrinsic Functions
Fn::Ref
- The
Fn::Ref
function can be leveraged to reference- Parameters => returns the value of the parameter
- Resources => returns the physical ID of the underlying resource (ex: EC2 ID)
- The shorthand for this in YA\/1_ is
!Ref
1 2 3 4 |
DbSubnetl: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC |
Fn::GetAtt
- Attributes are attached to any resources you create
- To know the attributes of your resources, the best place to look at is the documentation.
- For example: the AZ of an EC2 machine !
1 2 3 4 5 6 |
Resources: EC2lnstance: Type: "AWS::EC2::Instance" Properties: Imageld: ami-1234567 InstanceType: t2.micro |
1 2 3 4 5 6 7 |
NewVolume: Type: "AWS::EC2::Volume" Condition: CreateProdResources Properties: Size: 100 AvailabilityZone: !GetAtt EC2lnstance.AvailabilityZone |
Fn::FindInMap
Accessing Mapping Values
- We use
Fn::FindInMap
to return a named value from a specific key !FindInMap [ MapName, TopLevelKey, SecondLevelKey ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
AWSTemplateFormatVersion: "2010-09-09" Mappings: RegionMap: us-east-1: "32": "ami-6411e20d" "64": "ami-7a11e213" us-west-1: "32": "ami-c9c7978c" "64": "ami-cfc7978a" eu-west-1: "32": "ami-37c2f643" "64": "ami-31c2f645" ap-southeast-1: "32": "ami-66f28c34" " 64": "ami-60f28c32" ap-northeast-1: "32": "ami-9c03a89d" "64": "ami-a003a8a1" Resources: myEC2Instance: Type: "AWS::EC2::Instance" Properties: Imageld: !FindInMap [RegionMap, !Ref "AWS::Region", 32] InstanceType: m1.small |
Fn:ImportValue
- Import values that are exported in other templates
- For this, we use the
Fn::ImportValue
function
1 2 3 4 5 6 7 8 9 |
Resources: MySecureInstance: Type: AWS::EC2::Instance Properties: AvailabilityZone: us-east-la Imageld: ami-a4c7edb2 InstanceType: t2.micro SecurityGroups: - !ImportValue SSHSecurityGroup |
Fn::Join
- Join values with a delimiter
1 |
!Join [ delimiter, [ comma-delimited list of values ] ] |
- This creates “a:b:c” :
1 |
!Join [ ":" , [ a, b, c ] ] |
Function Fn::Sub
Fn::Sub
, or!Sub
as a shorthand, is used to substitute variables from a text. It’s a very handy function that will allow you to fully customize your templates.- For example, you can combine
Fn::Sub
with References or AWS Pseudo variables ! - String must contain
${VariableName}
and will substitute them
1 2 3 |
!Sub - String - { VarlName: VarlValue, Var2Name: Var2Value } |
1 |
!Sub String |
Conditions Functions
1 2 |
Conditions: CreateProdResources: !Equals [ !Ref EnvType, prod ] |
- The logical ID if for you to choose. It’s how you name condition
- The intrinsic function (logical) can be any of the following:
Fn::And
Fn::Equals
Fn::If
Fn::Not
Fn::Or