CloudFormation Advanced Topics

  CloudFormation

Course Main Menu

Section 10 Main Menu

Knowledge Review

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8149166?start=0

Ready to Use CloudFormation templates from AWS

Using the AWS CLI

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8149190?start=0
More information from AWS Solutions Architect training

  • We can use the AWS CLI to create, update or delete CloudFormation templates.
  • It is convenient when you start automating your deployments
  • It is also useful for managing parameters in a file.

Installing the AWS Command Line Interface

https://docs.aws.amazon.com/cli/latest/userguide/installing.html

 

Install AWS CLI (Easy way)

Ubuntu

sudo apt install -y awscli

Install AWS CLI via Python (Hard way)

Ubuntu:

sudo apt -y install python3
curl -O https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py --user

Add the executable path to your PATH variable:
Install the AWS CLI

pip install awscli --upgrade --user

Create a Profile

  • Console > Username > My Security Credentials > [Continue to Security Credentials]
  • Under “Your Security Credentials” > Access keys (access key ID and secret access key) > [Create New Access Key]
  • [Download Key File] (rootkey.csv)
  • Configure your profile
aws configure --profile profilename
  • Enter the Access Key, Secret Access Key Id and default region as prompted

Create your Parameters.json file

This file will allow you to pre-populate any parameters required

[
 {
  "ParameterKey": "InstanceType",
  "ParameterValue": "t2.micro"
 },
 {
  "ParameterKey": "KeyName",
  "ParameterValue": "mykey"
 },
 {
  "ParameterKey": "DBPassword",
  "ParameterValue": "!4U2n0"
 }
]

Run your template

aws cloudformation create-stack StackName --template-body file://path/to/template.yaml --parameters file://my-parameters.json --profile profilename --region us-east-2
The C.F. Template and Parameters files should be in the same folder as your command/execution script.

Troposphere (Python) to Generate CF Templates

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8149192?start=0

Best understood by reading the example: 1-troposphere-example.py

  • Troposphere allows you to leverage Python to write your CF templates: https://github.com/cloudtools/troposphere
  • Advantages
    • You can start using types in your templates
    • The generated CloudFormation will be valid
    • You can dynamically generate CloudFormation (loops, conditions, etc.)
    • You’re allowed to have complex conditions to generate exactly what you need.
  • Disadvantages
    • Two step process
      • Write Python to generate JSON
      • Upload JSON
    • It requires an understanding of Python vs. plain YAML

Install Troposphere

*Requires Pythod, duh!

pip install troposphere

Create the code

python filename.py

DeletionPolicy

Custom Resources with AWS Lambda

Best Practices for CloudFormation

Cost Estimate for Your Templates

Next Steps

 

LEAVE A COMMENT