aws london loft: cloudformation workshop

45
AWS London Loft: CloudFormation Workshop Templated AWS Resources Tom Maddox Solutions Architect [email protected]

Upload: duongnhu

Post on 13-Feb-2017

227 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: AWS London Loft: CloudFormation Workshop

AWS London Loft: CloudFormation WorkshopTemplated AWS Resources

Tom Maddox

Solutions Architect

[email protected]

Page 2: AWS London Loft: CloudFormation Workshop

Who am I?

• Gardener (Capacity Planning)

• Motorcyclist (Agility)

• Mobile App Writer

• Problem Solver

• Technology Geek

• Solutions Architect

Page 3: AWS London Loft: CloudFormation Workshop

What will we cover?

• Introduction to CloudFormation

• Template anatomy

• Bootstrapping instances

• Managing stacks

• Best Practices

• Demo

Page 4: AWS London Loft: CloudFormation Workshop

Managing complex cloud

architectures

Page 5: AWS London Loft: CloudFormation Workshop

Modern cloud architectures have lots of “bits”

Page 6: AWS London Loft: CloudFormation Workshop

Modern cloud architectures have lots of “bits”

S3 buckets

Auto Scaling groups

EC2 instances

CloudFront distributions

Elastic Load Balancing

RDS databases

Elasticache clusters

Security groups

SNS topics & subscriptions

EBS volumes

CloudWatch alarms

Elastic IPs

VPCs

Route tables

IAM users, roles, and policies

Route 53 hosted zones

Page 7: AWS London Loft: CloudFormation Workshop

Use the console?

Time consuming

Error prone

Not repeatableNot (easily) auditable

S3 buckets

Auto Scaling groups

EC2 instances

CloudFront distributions

Elastic Load Balancing

RDS databases

Elasticache clusters

Security groups

SNS topics & subscriptions

EBS volumes

CloudWatch alarms

Elastic IPs

VPCs

Route tables

IAM users, roles, and policies

Route 53 hosted zones

Page 8: AWS London Loft: CloudFormation Workshop

Write a script?

What happens if an API call fails?

How do I roll back?

How can I update my environment?How long should I pause for?

S3 buckets

Auto Scaling groups

EC2 instances

CloudFront distributions

Elastic Load Balancing

RDS databases

Elasticache clusters

Security groups

SNS topics & subscriptions

EBS volumes

CloudWatch alarms

Elastic IPs

VPCs

Route tables

IAM users, roles, and policies

Route 53 hosted zones

Page 9: AWS London Loft: CloudFormation Workshop

Use CloudFormation?

Page 10: AWS London Loft: CloudFormation Workshop

AWS Application Management Services

Elastic Beanstalk OpsWorks CloudFormation EC2

Convenience Control

Higher-level services Do it yourself

Page 11: AWS London Loft: CloudFormation Workshop

CloudFormation Overview

• Define & manage your infrastructure as code (JSON templates)

• Declarative approach with managed infrastructure dependencies

• Self documenting (no more spreadsheets of SG rules)

• Intelligent updating of resources and automated rollback capabilities

• AWS API interactions taken care for you

• Reproducibility

• Versioning

Page 12: AWS London Loft: CloudFormation Workshop

Continuous integration for your complete

stack

Version Control Jenkins

Test

Live

Amazon

S3

AWS

CloudFormation

App commit

Infra commit

Pull

Deploy new

template

Deploy

new app

Page 13: AWS London Loft: CloudFormation Workshop

Continuous integration for your complete

stack

Version Control Jenkins

Test

Live

Amazon

S3

AWS

CloudFormation

App commit

Infra commit

Pull

Promote new template

Promote

new app

Page 14: AWS London Loft: CloudFormation Workshop

Template anatomy

Page 15: AWS London Loft: CloudFormation Workshop

Template structure

"Parameters"

"Mappings"

"Conditions"

"Resources"

"Outputs"

Page 16: AWS London Loft: CloudFormation Workshop

Resources

Page 17: AWS London Loft: CloudFormation Workshop

Parameters

"Parameters" : {

“InstanceType” : {

“Description” : “The EC2 Instance Type to launch.”,

“Type” : “String”,

“AllowedValues” : [“t1.micro”, “m1.small”, “m1.medium”]

}

},

“InstanceType” : { “Ref” : “InstanceType” }

Page 18: AWS London Loft: CloudFormation Workshop

Outputs

"Outputs" : {

"InstancePublicDnsName" : {

"Description" : "The public DNS name of the newly created EC2 instance",

"Value" : { ”Fn::GetAtt" : [ "Ec2Instance”, “PublicDnsName” ] }

}

}

Page 19: AWS London Loft: CloudFormation Workshop

Conditions

"Environment" : {

"Description" : "Specifies if this a Dev QA or Prod Environment",

"Type" : "String",

"Default" : "Dev",

"AllowedValues" : [ "Dev", "QA", "Prod"]

},

"Conditions" : {

"ProdEnvironment" : { "Fn::Equals" : [ { "Ref" : "Environment" }, "Prod" ]}

},

"InstanceType" : { "Fn::If" : [ "ProdEnvironment", “m3.2xlarge”, “m3.medium” ] }

Page 20: AWS London Loft: CloudFormation Workshop

Mappings

"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" }

}

},

"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "32"]},

Page 21: AWS London Loft: CloudFormation Workshop

Helpful Links

• http://aws.amazon.com/cloudformation/aws-cloudformation-templates/

– Search: “AWS CloudFormation Templates”

– Helpful solution-based templates (eg: Active Directory domain forest, or LAMP stack)

• http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/CHAP_TemplateQuickRef.html

– Search: “AWS CloudFormation Snippets”

– Handy snippets for common AWS services (eg: Autoscaling group)

• http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-reference.html

- Search: “AWS CloudFormation Template Reference”

- API docs for each CloudFormation resource type

Page 22: AWS London Loft: CloudFormation Workshop

Demo!

Page 23: AWS London Loft: CloudFormation Workshop

Bootstrapping instances

Page 24: AWS London Loft: CloudFormation Workshop

Use AWS::CloudFormation::Init

• Declarative – Specify the end state, not how to get there

• Debugable – cfn-init.log can integrate into CloudWatch Logs

• Secure – Supports IAM roles

• Updatable - Supported as part of the stack update process

• Bring your own tools – Use cfn-init to bootstrap Chef, Puppet etc.

Page 25: AWS London Loft: CloudFormation Workshop

Use AWS::CloudFormation::Init

"Metadata" : {

"AWS::CloudFormation::Init" : {

"webapp-config": {

"packages" : {},

"sources" : {},

"files" : {},

"groups" : {},

"users" : {},

"commands" : {},

"services" : {}

}

}

},

Page 26: AWS London Loft: CloudFormation Workshop

Using CloudFormation::Init to update

instances

"packages" : {},

"sources" : {},

"files" : {},

"groups" : {},

"users" : {},

"commands" : {},

"services" : {}

1. Update instance metadata in the template

2. UpdateStack

Instance

Metadata

cfn-hup

3. AWS CloudFormation daemon updates configuration

Page 27: AWS London Loft: CloudFormation Workshop

Managing your stacks

Page 28: AWS London Loft: CloudFormation Workshop

Organize by layers & environments

Frontend Services

• E-Commerce Website

Backend Services

• Search, Payment Gateway, Reviews, Recommendations

Shared Services

• Common Monitoring tools, Queues

BaseNetwork

• VPCs, Subnets, VPNs, NATs

Identity • IAM Users, Groups, Roles

Page 29: AWS London Loft: CloudFormation Workshop

Apply SOA principles

"Parameters" : {

“RecommendationsEndPoint” : {

“Description” : “URL of the recommendations ELB”,

“Type” : “String”

}

},

"Outputs" : {

"RecommendationsEndPoint" : {

"Description" : "URL of the recommendations ELB",

"Value" : { ”Fn::GetAtt" : [ "RecommendationsELB”, “PublicDnsName” ] }

}

}

E-Commerce Website Recommendations Engine

Depends On

“Wire”

Page 30: AWS London Loft: CloudFormation Workshop

Nested stacks for reusability

ELB_AND_AS

“Resources” : {

“ELB”,

“AutoScaling”

}

Website1

“Resources” : {

“NestedStack”,

“RDS”

}

Website2

“Resources” : {

“NestedStack”,

“DynamoDB”

}

Website1

“Resources” : {

“ELB”,

“AutoScaling”,

“RDS”

}

Website2

“Resources” : {

“ELB”,

“AutoScaling”,

“DynamoDB”

}

Page 31: AWS London Loft: CloudFormation Workshop

Deploying Applications

Page 32: AWS London Loft: CloudFormation Workshop

Choose a deployment style

In-place Blue-Green

Stacks

Templates

Amazon

Route 53

Page 33: AWS London Loft: CloudFormation Workshop

Use AutoScalingRollingUpdate for zero downtime

"UpdatePolicy" : {

"AutoScalingRollingUpdate" : {

"MaxBatchSize" : “2”,

"MinInstancesInService" : ”2”,

"PauseTime" : “PT20M”

}

}

Page 34: AWS London Loft: CloudFormation Workshop

Use

AutoScalingRollingUpdate

for zero downtime

• Developer deploys new template

containing change to Launch

Configuration

Auto Scaling group

CloudFormation Stack

• CloudFormation replaces a single batch of EC2 instances, the others remain in service

• ELB health check confirms new instances are healthy (otherwise rollback)

• Next batch of EC2 instances are replaced

Page 35: AWS London Loft: CloudFormation Workshop

Optimizing the ASG UpdatePolicy

• On test environments you can optimize for speed and replace all instances

at once

• Once live, you should update the ASG in batches making sure you don’t have

downtime

Page 36: AWS London Loft: CloudFormation Workshop

…for example

"UpdatePolicy": {

"AutoScalingRollingUpdate": {

"PauseTime": "PT0S",

"MaxBatchSize": “6",

"MinInstancesInService": "0"

}

}

"UpdatePolicy": {

"AutoScalingRollingUpdate": {

"PauseTime": "PT15S",

"MaxBatchSize": "2",

"MinInstancesInService": "2"

}

}

For a service with an ASG with 6 instances…

TEST LIVE

Page 37: AWS London Loft: CloudFormation Workshop

Best Practices

Page 38: AWS London Loft: CloudFormation Workshop

Validate your templates

ValidateTemplate API validates:

• JSON syntax

• Absence of circular dependencies

• Template structure

Page 39: AWS London Loft: CloudFormation Workshop

Use parameter types

"Parameters" : {

“VpcId" : {

"Type" : "AWS::EC2::VPC::Id"

},

“SubnetIds" : {

"Type" : "List<AWS::EC2::Subnet::Id>"

},

“SecurityGroups" : {

"Type" : "List<AWS::EC2::SecurityGroup::Id>"

},

“KeyPair" : {

"Type" : "AWS::EC2::KeyPair::KeyName"

}

Page 40: AWS London Loft: CloudFormation Workshop

Use Deletion Policies to protect resources

{

"AWSTemplateFormatVersion" : "2010-09-09",

"Resources" : {

"myS3Bucket" : {

"Type" : "AWS::S3::Bucket",

"DeletionPolicy" : "Retain"

"Properties" : {

"BucketName" : “MyBucket”

}

}

}

}

{

"AWSTemplateFormatVersion" : "2010-09-09",

"Resources" : {

"myVolume" : {

"Type":"AWS::EC2::Volume",

"DeletionPolicy" : "Snapshot”

"Properties" : {

"AvailabilityZone" :”us-east-1a”,

"Size” : “100”

}

}

}

}

MyBucket myVolume Snapshot

On stack deletion:

Page 41: AWS London Loft: CloudFormation Workshop

Use stack policies as update guardrails

“Do not update the databases”

"Effect" : "Deny",

"Principal" : "*",

"Action" : "Update:*",

"Resource" : "*",

"Condition" : {

"StringEquals" : {

"ResourceType” : [

"AWS::RDS::DBInstance”,

"AWS::Redshift::Cluster”

]

}

}

“Okay to update, unless the update requires replacement”

"Effect" : "Deny",

"Principal": "*",

"Action" : "Update:Replace",

"Resource" : "LogicalResourceId/MyInstance"

Page 42: AWS London Loft: CloudFormation Workshop

Stop “configuration drift” with IAM and tags{

"Version": "2012-10-17",

"Statement": [

{

"Effect": ”Deny",

"Action": “*",

"Resource": "*"

},

{

"Effect" : ”Allow",

"Action" : [

"Action": "ec2:Describe*”

],

"Condition": {

"Null": { "ec2:ResourceTag/*cloudformation*" : "true" }

},

"Resource" : "*"

}

]

}

Page 43: AWS London Loft: CloudFormation Workshop

Workshop Challenge

Page 44: AWS London Loft: CloudFormation Workshop

Build a web application deployment starting with the VPC

Sample Template

• Sample VPC Template: http://tinyurl.com/pgs3mjo

• Master Template: http://tinyurl.com/pd86795

• Extra Challenges:

– AutoScaling Group, ELB & RDS

– Compatible with any region

– Fetch Code from Github (passing secrets)

– Nested Templates for Segregation of Duties

– A/B Deployment Friendly

Page 45: AWS London Loft: CloudFormation Workshop