aws cloudformation intrinsic functions and mappings
TRANSCRIPT
AWS CloudFormationIntrinsic Functions and Mappings
Managing Windows instances in the Cloud
Sponsors
Presented by Adam Book from
Find me on LinkedIn
CloudFormation Deep Dive
CloudFormation Review
AWS CloudFormation Allows you to build Infrastructure as code using templates which are constructed from json.
CloudFormation Template
There are 8 sections of a Cloud formation template, most of which are optional
Format Version (optional)
Description (optional)
Metadata (optional)
Mappings (optional)
Parameters(optional)
Conditions(optional)
Resources (required)
Outputs(optional)
CloudFormation Best Practice
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/best-practices.html
As you use Cloud Formation make sure you follow the best practices for success
• Do Not Embed Credentials in You Templates• Use AWS-Specific Parameter Types• Use Parameter Constraints • Validate Templates Before Using them • Manage All Stack Resources Through AWS Cloud Formation
CloudFormationIntrinsic FunctionsFunction OverviewFn::Base64 returns the Base64 representation of the input string (user data)
Fn::FindInMap returns the value corresponding to keys in a two-level map that is declared in the Mappings section
Fn::GetAtt returns the value of an attribute from a resource in the template.
Fn::GetAZs returns an array that lists Availability Zones for a specified region.
Fn::Join appends a set of values into a single value, separated by the specified delimiter.
Fn::Select returns a single object from a list of objects by index.
Ref returns the value of the specified parameter or resource.
CloudFormationMappings
The Mappings section is optional but is matches a key to a corresponding set of named values. If you want to set values based on region, you can create a mapping that uses the key as the name and then contains the values you want to specify for each region.
You cannot include parameters, pseudo parameters, or intrinsic functions in the Mappings section.
CloudFormationMappings - cont. "Mappings" : { "RegionMap" : { "us-east-1" : { "32" : "ami-6411e20d"}, "us-west-1" : { "32" : "ami-c9c7978c"}, "eu-west-1" : { "32" : "ami-37c2f643"}, "ap-southeast-1" : { "32" : "ami-66f28c34"}, "ap-northeast-1" : { "32" : "ami-9c03a89d"} } }
CloudFormationMappings - cont. "asgApp": { "MinSize" : { "value": "2" }, "MaxSize" : { "value": "2" }, "DesiredCapacity" : { "value": "2" }, "HealthCheckType" : { "value": "EC2" }, "TerminationPolicies" : { "value": "OldestInstance" } }
CloudFormationMappings - cont.
"asgAppA": { "Type": "AWS::AutoScaling::AutoScalingGroup", "Properties": { "AvailabilityZones" : { "Ref": "AZs" }, "VPCZoneIdentifier" : { "Ref": "PrivateAPPSubnets" }, "LaunchConfigurationName" : { "Ref": "LaunchConfig" }, "MinSize" : { "Fn::FindInMap": [ "asgApp", "MinSize", "value" ] }, "MaxSize" : { "Fn::FindInMap": [ "asgApp", "MaxSize", "value" ] }, "DesiredCapacity" : { "Fn::FindInMap": [ "asgApp", "DesiredCapacity", "value" ] }, "HealthCheckType" : { "Fn::FindInMap": [ "asgApp", "HealthCheckType", "value" ] }, "TerminationPolicies" : [{ "Fn::FindInMap": [ "asgApp", "TerminationPolicies", "value" ] }],
Fn::FindInMap
"Resources" : { "myEC2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "32"]}, "InstanceType" : "m1.small" } } } }
This function performs lookups, it accepts a ‘mappings’ object on of one or two keys and then returns a value
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html
Fn::Base64
{ "Fn::Base64" : ”apt-get update –y " }
This function accepts plain text and converts it to Base 64
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-base64.html
Fn::Join
"Outputs" : { "URL" : { "Description" : "The URL of your demo website", "Value" : { "Fn::Join" : [ "", [ "http://", { "Fn::GetAtt" : [ "ElasticLoadBalancer", "DNSName" ]}]]} } }
This can be used to concatenate various components to produce things such as a URL.
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-join.html
Fn::GetAtt
Some examples of attributes that can be called are:• EC2 -> PrivateIp• EC2-> PublicIp• ElasticLoadBalancing -> DNSName• IAM::Group -> ARN • S3 Bucket -> DomainName • Simple AD -> Alias
As you dynamically create items in your Cloud Formation templates , you may need to use some of the Attributes after they are created.
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html
Fn::GetAtt
"MyEIP" : { "Type" : "AWS::EC2::EIP", "Properties" : { "InstanceId" : { "Ref" : "MyEC2Instance" } } }
“Fn:GetAtt” :[ “MyEIP”, “AllocationId” ]
As you dynamically create items in your Cloud Formation templates
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html
Fn::GetAZs
{ "Fn::GetAZs" : "us-east-1" }
{ "Fn::GetAZs" : { "Ref" : "AWS::Region" } }
The intrinsic function Ref returns to value of the specified parameter or resource.
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-select.html
NOTE: You can use the Ref function in the Fn::GetAZz function.
Fn::Select
{ “Fn::Select” : [ “0”, {”Fn::GetAZs” : “”} ] }
Selects a single object from a list of object and can be paired with other functions such as Fn::GetAZs
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-select.html
The output is the first Availablity zone in the region where the template is applied.
Replacing the 0 with a 1 would select the second Availability Zone
Fn::Ref
"MyEIP" : { "Type" : "AWS::EC2::EIP", "Properties" : { "InstanceId" : { "Ref" : "MyEC2Instance" } } }
The intrinsic function Ref returns to value of the specified parameter or resource.
For more infohttp://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html
Cloud Formation Templates Real World Examples
Photo curtesyof Stephen Radford via http://snap.io
Questions?
Image by http://www.gratisography.com/