Cloudformation

From Ever changing code
Revision as of 08:35, 20 August 2018 by Pio2pio (talk | contribs) (→‎Cloudformation Vim lint)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Basic structure of Json and Yaml templates

Cloudformation template basic structure
YAML JSON
AWSTemplateFormatVersion: 2010-09-09
Description: Yaml string, version 1.0
Metadata: {}
Parameters: {}
Mappings: {}
Conditions: {}
Transform: {}
Resources: {}
Outputs: {}
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "JSON string, version 1.0",
    "Metadata": {},
    "Parameters": {},
    "Mappings": {},
    "Conditions": {},
    "Transform": {},
    "Resources": {},
    "Outputs": {}
}

Structured template

Cloudformation template basic structure
YAML JSON
AWSTemplateFormatVersion: 2010-09-09
Description: JSON string
Metadata: {}
Parameters:
  InstanceTypeParameter:
    Type: String
    Default: t2.micro
    AllowedValues:
      - t2.micro
      - m1.smal
    Description: 'Enter t2.micro, m1.large'
Mappings: {}
# Mapping01:
#   Key01:
#     Name: Value01
#   Key02:
#     Name: Value02
Conditions: {}
Transform: {}
# AWS::serverless
# AWS::Include (reusing template snippets)
Resources:
  MyBucket:     #logicalID must be unique, can be reused within the template
  Type: 'AWS:S3::Bucket'  #type of resource to create
  Properties:  #additional options for a resource
    'Fn::Transform;:
      - Name: 'AWS::Include'
         Parameters:
         Location: s3://bucket/MyBucketName.yaml
Outputs: {}
# logicalID:
#   Descryption:
#   Value:
#   Export:
#     Name: Value to export
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "JSON string",
    "Metadata": {},
    "Parameters": {
        "InstanceTypeParameter": {
            "Type": "String",
            "Default": "t2.micro",
            "AllowedValues": [
                "t2.micro",
                "m1.small"
            ],
            "Description": "Enter t2.micro, m1.small"
        }
    },
    "Mappings": {},
    "Conditions": {},
    "Transform": {},
    "Resources": {},
    "Outputs": {}
}

Intrinsic Functions (buildin)

Use intrinsic functions in your templates to assign values to properties that are not available until runtime.

   Fn::Base64
   Fn::Cidr
   Condition Functions
   Fn::FindInMap
   Fn::GetAtt
   Fn::GetAZs
   Fn::ImportValue
   Fn::Join
   Fn::Select
   Fn::Split
   Fn::Sub
   Ref

Syntax

Within AWS designer following are available:

  • ctrl + space - opens auto-completion and help context in a resource properties
  • pink dot - indicates a resource depends on other resource

Function short form

The exclamation point indicates the YAML short form of a command. It does not indicate a Boolean not.

"Fn::And": [{condition}, {...}] #Json
Fn::And: [Condition] #yaml full syntax
!And [condition]     #short form

Cloudformation Vim lint

Validate CloudFormation yaml/json templates against the CloudFormation spec and additional checks. Includes checking valid values for resource properties and best practices. The Serverless Application Model (SAM) is supported by the linter. The template is transformed using AWS SAM (https://github.com/awslabs/serverless-application-model) before the linter processes the template.


Prereq for VIM, install cfn-lint command line tool

pip install cfn-lint
cfn-lint <path to yaml template>


Install Vim 8.0+ Vundle plugin

#add plugin to plugin list
$ vim ~/.vimrc
Plugin 'speshak/vim-cfn'
#install plugin from a terminal
$ vim +PluginInstall +qall
#configure
$ vim ~/.vimrc
" Docs say to add to ~/.vim/after/plugin/syntastic.vim for vundle but I did add to ~/.vimrc
let g:syntastic_cloudformation_checkers = ['cfn_lint']


Vim cnf-lint real example

Cfn-lint cloudFormation


Set Clouformation syntax in case was not autodetected

:set syntax=yaml.cloudformation

References

References