A template is a JSON- or YAML-formatted text file that describes your AWS infrastructure. Example template for an EC2 instance in a security group is available here.
Template Sections
-
Format Version (optional)
-
Specifies the template version that the template conforms to. Not the same as the API or WSDL version. Can change independently of the API and WSDL versions.
-
-
Description (optional)
-
A text string that describes the template. Must always follow the template format version section.
-
-
Metadata (optional)
-
additional information about the template.
-
-
Parameters (optional)
-
Specifies values that you can pass in to your template at runtime (when you create or update a stack).
-
You can refer to parameters in the Resources and Outputs sections of the template.
-
-
Mappings (optional)
-
A mapping of keys and associated values that you can use to specify conditional parameter values, similar to a lookup table.
-
You can match a key to a corresponding value by using the Fn::FindInMap intrinsic function in the Resources and Outputs section.
-
-
Conditions (optional)
-
Defines conditions that control whether certain resources are created or whether certain resource properties are assigned a value during stack creation or update.
-
E.g. conditionally create a resource only for a production or test environment.
-
-
Transform (optional)
-
For serverless applications (Lambda-based applications), specifies the version of the AWS Serverless Application Model (AWS SAM) to use.
-
When you specify a transform, you can use AWS SAM syntax to declare resources in your template. The model defines the syntax that you can use and how it is processed.
-
You can also use the AWS::Include transform to work with template snippets that are stored separately from the main AWS CloudFormation template. You store your snippet files in an Amazon S3 bucket and then reuse the functions across multiple templates.
-
-
Resources (required)
-
Specifies the stack resources and their properties, such as an EC2 instance or an S3 bucket.
-
You can refer to resources in the Resources and Outputs sections of the template.
-
-
Outputs (optional)
-
Describes the values that are returned whenever you view your stack's properties.
-
For example, you can declare an output for an S3 bucket name and then call the aws cloudformation describe-stacks AWS CLI command to view the name.
-
Most Important Element Types
Parameters
With parameters, you can customize aspects of your template at run time, when the stack is built. For example, the Amazon RDS database size, Amazon EC2 instance types, database and web server port numbers can be passed to CloudFormation when a stack is created.
Each parameter can have a default value and description and may be marked as “NoEcho” in order to hide the actual value you enter on the screen and in the CloudFormation event logs. When you create a stack, the Console will automatically present a pop-up dialog form for you to edit parameter values.
Output Values
Output values present a stack’s key resources (such as the address of an ELB or RDS database) back to the user via the Console, or the command line tools. Can use simple functions to concatenate string literals and value of attributes associated with the actual AWS resources.
Resource Names
You can assign logical names to AWS resources in template. When a stack is created, CloudFormation binds the logical name to actual resource names, which is a combination of the stack and logical resource name. So there won’t be any name collisions between AWS resources in different stacks. Also, to prevent naming collisions, CloudFormation allows you to name only some resources, but not all.
Important Configurations
WaitCondition
CloudFormation provides a WaitCondition resource that acts as a barrier, blocking the creation of other resources until a completion signal is received from an external source
Rollback on error
By default, the “automatic rollback on error” feature is enabled. With this feature stacks are either fully created, or not at all. In case of error with any service during creation, everything created successfully is rolled back.
Bootstrapping
CloudFormation provides a set of application bootstrapping scripts that enable you to install packages, files, and services on your EC2 instances by describing them in your template.
CloudFormation can also be used to bootstrap both the Chef Server and Chef Client, or Puppet Master and Puppet Client, on EC2.
Tagging
EC2 resources that support tagging can be tagged in template. Tag values can refer to template parameters, other resource names, resource attribute values, or values computed (e.g., a concatenated string).
CloudFormation automatically tags EBS volumes and EC2 instances with the name of the stack.
VPC Support
CloudFormation supports creating VPCs, Subnets, Gateways, Route Tables and Network ACLs as well as creating resources such as Elastic IPs, EC2 Instances, EC2 Security Groups, Auto Scaling Groups, ELBs, RDS Instances and RDS Security Groups in a VPC.
Template Anatomy - Improtant Notes
-
Template, Parameter, Output, and Resource description fields are limited to 4096 characters.
-
You can include up to 60 parameters and 60 outputs in a template.
-
Resources section is the only required section.
-
Regular expressions (regexes) can be specified in a number of places within an AWS CloudFormation template, such as for the AllowedPattern property when creating a template parameter. Regex in CloudFormation conform to the Java rregex syntax.
-
Invalid JSON syntax in template will cause an error message during template validation.
-
Cloud formation can return data after provisioning and this can be configured. For example, to output the DNS name of your load balancer after provisioning a virtual data centre, you can use FN::GetAtt. The Fn::GetAtt intrinsic function returns the value of an attribute from a resource in the template. See references for more explanation on FN::GetAtt and other intrinsic functions.
References (Deprecated):
https://aws.amazon.com/cloudformation/
Sample templates are available @ https://aws.amazon.com/cloudformation/aws-cloudformation-templates/.
CloudFormation Designer @ https://aws.amazon.com/cloudformation/details/#designer
http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html
- heartin's blog
- Log in or register to post comments
Recent comments