Transcript
Page 1: (DVO304) AWS CloudFormation Best Practices

© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Abhishek Lal, Product Manager

Chris Whitaker, Development Manager

October 2015

DVO304

AWS CloudFormation Best

Practices

Page 2: (DVO304) AWS CloudFormation Best Practices

AWS CloudFormation

Create templates of the infrastructure

CloudFormation provisions AWS resources in order

Version control/replicate/update with infrastructure-as-code

Integrates with development, CI/CD, management tools

Page 3: (DVO304) AWS CloudFormation Best Practices

AWS CloudFormation Designer

Page 4: (DVO304) AWS CloudFormation Best Practices

Introducing AWS CloudFormation Designer

• Visualize template

resources

• Modify template with drag-

and-drop gestures

• Customize sample

templates

Page 5: (DVO304) AWS CloudFormation Best Practices

AWS CloudFormation Designer

demo – Visualize templates

Page 6: (DVO304) AWS CloudFormation Best Practices

AWS CloudFormation Designer

– Make updates

Page 7: (DVO304) AWS CloudFormation Best Practices
Page 8: (DVO304) AWS CloudFormation Best Practices

AWS CloudFormation Designer

– Authoring

Page 9: (DVO304) AWS CloudFormation Best Practices
Page 10: (DVO304) AWS CloudFormation Best Practices

CloudFormation Designer toolbar

Toolbar Navigation

Open: Local files/S3/stack

Save: Local files/launch stack

Validation: AWS resource

schema

Refresh: Synchronize JSON

text changes

Page 11: (DVO304) AWS CloudFormation Best Practices

CloudFormation Designer Resources

All supported resources

Organized by service

Drag and drop onto canvas

Color-coded icons

Page 12: (DVO304) AWS CloudFormation Best Practices

CloudFormation Designer canvas

Container Resources

e.g. EC2 VPCs, subnets

Connections between

resources

e.g. Ref, DependsOn, GetAtt

Contextual Resource menu

Code/Clone/Delete/Docs

Page 13: (DVO304) AWS CloudFormation Best Practices

CloudFormation Designer JSON Editor

Ctrl+Space : Within the Properties key of a

resource, lists all the available properties

for the resource

Ctrl+F : Search for a value in the JSON

editor.

Ctrl+\ : Formats the text with proper

indentation and new lines

Ctrl+Shift+\ : Removes all white space

Page 14: (DVO304) AWS CloudFormation Best Practices

New AWS Services Supported

by AWS CloudFormation

Page 15: (DVO304) AWS CloudFormation Best Practices

Use a wide range of AWS services

Amazon EC2

Amazon EC2 Container Service

AWS Lambda (including event sources – New)

Auto Scaling (including Spot Fleet - New)

Amazon VPC

Elastic Load Balancing

Amazon Route 53

Amazon CloudFront

Amazon SimpleDB

Amazon RDS

Amazon Redshift

Amazon DynamoDB

Amazon ElastiCache

Amazon RDS for Aurora (New)

Amazon S3

AWS IAM (including managed policies)

Simple AD (New)

Amazon Kinesis

Amazon SNS

Amazon SQS

AWS CloudTrail

Amazon CloudWatch

AWS Data Pipeline

AWS Elastic Beanstalk

AWS OpsWorks

AWS CodeDeploy (New)

Amazon WorkSpaces (New)

Page 16: (DVO304) AWS CloudFormation Best Practices

AWS CloudFormation in Your

Organization

Page 17: (DVO304) AWS CloudFormation Best Practices

Managing your costs with budgets

https://console.aws.amazon.com/billing/home?region=us-east-1/budgets#/

ow.ly/T84qv

Page 18: (DVO304) AWS CloudFormation Best Practices

Audit logs for all operationsStore/ Archive

Troubleshoot

Monitor and Alarm

You are making API

calls...

On a growing set of AWS

services around the world...

CloudTrail is continuously

recording API calls

Page 19: (DVO304) AWS CloudFormation Best Practices

AWS CloudFormation Advanced

Concepts

Page 20: (DVO304) AWS CloudFormation Best Practices

AWS CloudFormation language features

Page 21: (DVO304) AWS CloudFormation Best Practices

Extending AWS CloudFormation

Page 22: (DVO304) AWS CloudFormation Best Practices

Security group

Auto Scaling group

EC2

instance

Elastic Load

Balancing

ElastiCache

Memcached cluster

Software pkgs,

config, & dataCloudWatch

alarmsWeb Analytics

ServiceAWS

CloudFormation

Provision

AWS resources

“Create, Update,

Rollback, or Delete”

Extend with stack events

Worker

Amazon

SNS Topic

Stack Events

Page 23: (DVO304) AWS CloudFormation Best Practices

Security group

Auto Scaling group

EC2

instance

Elastic Load

Balancing

ElastiCache

Memcached cluster

Software pkgs,

config, & dataCloudWatch

alarmsWeb Analytics

ServiceAWS

CloudFormation

Provision

AWS Resources

"Resources" : {

"WebAnalyticsTrackingID" : {

"Type" : "Custom::WebAnalyticsService::TrackingID",

"Properties" : {

"ServiceToken" : "arn:aws:sns:...",

"Target" : {"Fn::GetAtt" : ["LoadBalancer", "DNSName"]},

"Plan" : "Gold"

}

},

...

“Success” + Metadata

“Create, Update, Rollback, or Delete”

+ Metadata

Extend with custom resources

ow.ly/DiSXp

Page 24: (DVO304) AWS CloudFormation Best Practices

AWS Lambda-backed custom resources

Security group

Auto Scaling group

EC2

instance

Elastic Load

Balancing

ElastiCache

memcached cluster

Software pkgs,

config, & dataCloudWatch

alarms

Your AWS CloudFormation stack

// Implement custom logic here

Look up an AMI ID

Your AWS Lambda functions

Look up an VPC ID and Subnet ID

Reverse an IP address

Lambda-powered

custom resources

Page 25: (DVO304) AWS CloudFormation Best Practices

Security Best Practices

Page 26: (DVO304) AWS CloudFormation Best Practices

Security – Restricting user access

• Only allow specific templates and stack policies

{

"Effect":"Allow”,

"Action":[

"cloudformation:CreateStack",

"cloudformation:UpdateStack”

],

"Condition":{

"ForAllValues:StringLike":{

"cloudformation:TemplateUrl":

["https://.amazonaws.com/TestBucket/*"]

}

}

}

{

"Effect":"Allow”,

"Action":[

"cloudformation:UpdateStack”

],

"Condition":{

"ForAllValues:StringEquals":{

"cloudformation:StackPolicyUrl":

["https://.amazonaws.com/TestBucket/Foo.json"]

}

}

}

Page 27: (DVO304) AWS CloudFormation Best Practices

Security – Restricting user access

• Only allow specific resource types

{

"Effect":"Allow”,

"Action":[

"cloudformation:CreateStack”

],

"Condition":{

"ForAllValues:StringEquals":{

"cloudformation:ResourceType":

[”AWS::EC2::Instance”…]

}

}

}

{

"Effect":"Allow”,

"Action":[

"cloudformation:CreateStack”

]

},

{

"Effect":”Deny”,

"Action":[

"cloudformation:CreateStack”

]

"Condition":{

"ForAnyValue:StringLike":{

"cloudformation:ResourceType":

[”AWS::IAM::*"]

}

}

}

Page 28: (DVO304) AWS CloudFormation Best Practices

Security – Controlling resource types

• Programmatically restrict access to resource types

• CreateStack and UpdateStack take a new parameter

• Restrict the set of resources that can be created

• Independent of any user policies

$ aws cloudformation create-stack … --resource-types=“[AWS::EC2::*, AWS::RDS::DBInstance, Custom::MyCustomResource]”

Page 29: (DVO304) AWS CloudFormation Best Practices

Best Practices for Templates

Page 30: (DVO304) AWS CloudFormation Best Practices

Reusing templates across AWS regions

• Consider environmental or regional differences

• Amazon EC2 image IDs

• VPC environment or “classic” environment

• Available instance types

• IAM policy principals

• Endpoint names

• Amazon Resource Names (ARNs)

Page 31: (DVO304) AWS CloudFormation Best Practices

Reusable templates – “Pseudo-parameters”

Use “pseudo-parameters” to retrieve

environmental data

• Account ID

• Region

• Stack Name and ID

"LogsBucketPolicy": {"Type": "AWS::S3::BucketPolicy","Properties": {

"Bucket": {"Ref": "LogsBucket”},"PolicyDocument": {

"Version": "2008-10-17","Statement": [{"Sid": "ELBAccessLogs","Effect": "Allow","Resource": {"Fn::Join": [ "", [ “arn:aws:s3:::",

{ "Ref": "LogsBucket" }, "/", "Logs", "/AWSLogs/", { "Ref": "AWS::AccountId" }, "/*”

] ]},"Principal": …,"Action": [ "s3:PutObject" ]

}

Page 32: (DVO304) AWS CloudFormation Best Practices

Reusable templates – Using mappings

Use mappings to define variables

• Single place for configuration

• Reusable within the template"LogsBucketPolicy": {"Type": "AWS::S3::BucketPolicy","Properties": {

"Bucket": {"Ref": "LogsBucket”},"PolicyDocument": {"Version": "2008-10-17","Statement": [{

"Sid": "ELBAccessLogs","Effect": "Allow","Resource": {"Fn::Join": [ "", [

{ "Fn::FindInMap" : ["RegionalConfig", {"Ref" : "AWS::Region"},"ArnPrefix”]},

"s3:::”, { "Ref": "LogsBucket" }, "/", "Logs", "/AWSLogs/”,

{ "Ref": "AWS::AccountId" }, "/*" ] ]},"Principal": {"AWS": { "Fn::FindInMap": [ "RegionalConfig",

{ "Ref": "AWS::Region" },”ELBAccountId" ] } },

"Action": [ "s3:PutObject" ]}]

“Mappings” : {“RegionalConfig” : {

“us-east-1” : {“AMI” : “ami-

12345678”,”ELBAccountId":

"127311923021”,“ArnPrefix” :

“arn:aws:”},“us-west-1” : {

“AMI” : “ami-98765432””ELBAccountId":

“027434742980"“ArnPrefix” :

“arn:aws:”},:

}}

Page 33: (DVO304) AWS CloudFormation Best Practices

Re-usable Templates – Using conditionals

Use conditionals to customize

resources and parameters

"DBEC2SG": {"Type": "AWS::EC2::SecurityGroup","Condition" : "Is-EC2-VPC","Properties" : {

:}

},

"DBSG": {"Type": "AWS::RDS::DBSecurityGroup","Condition" : "Is-EC2-Classic","Properties": {

:}

},

"MySQLDatabase": {"Type": "AWS::RDS::DBInstance","Properties": {

:"VPCSecurityGroups": { "Fn::If" : [ "Is-EC2-VPC",

[ { "Fn::GetAtt": [ "DBEC2SG", "GroupId" ] } ],

{ "Ref" : "AWS::NoValue"}]},

"DBSecurityGroups": { "Fn::If" : [ "Is-EC2-Classic", [ { "Ref": "DBSG" } ],{ "Ref" :

"AWS::NoValue"}]}

"Conditions" : {"Is-EC2-VPC” : { "Fn::Or" : [

{"Fn::Equals" : [{"Ref" : "AWS::Region"}, "eu-central-1" ]},

{"Fn::Equals" : [{"Ref" : "AWS::Region"}, "cn-north-1" ]}]},

"Is-EC2-Classic" : { "Fn::Not" : [{ "Condition" : "Is-EC2-VPC"}]}},

Page 34: (DVO304) AWS CloudFormation Best Practices

Thank you!

Abhishek Lal, Product Manager

Chris Whitaker, Development Manager

Page 35: (DVO304) AWS CloudFormation Best Practices

Remember to complete

your evaluations!

Page 36: (DVO304) AWS CloudFormation Best Practices

Related Sessions


Top Related