53
© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Alex Dunlap GM, Amazon CloudFront Jarrod Guthrie Sr. Product Manager, Amazon CloudFront Calin Nemes Sr. Support Engineer, Amazon CloudFront Matthew Baldwin Sr. Software Development Engineer, Amazon CloudFront October 2015 Using Amazon CloudFront to Improve Performance, Availability and Cacheability STG206

(STG206) Using Amazon CloudFront For Your Websites & Apps

Embed Size (px)

Citation preview

Page 1: (STG206) Using Amazon CloudFront For Your Websites & Apps

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

Alex Dunlap

GM, Amazon CloudFront

Jarrod Guthrie

Sr. Product Manager, Amazon CloudFront

Calin Nemes

Sr. Support Engineer, Amazon CloudFront

Matthew Baldwin

Sr. Software Development Engineer, Amazon CloudFront

October 2015

Using Amazon CloudFront to Improve

Performance, Availability and Cacheability

STG206

Page 2: (STG206) Using Amazon CloudFront For Your Websites & Apps

What to expect from the session

• Best practices for setting up your origin

• Gaining visibility into your distribution

• How to improve cacheability

• How to test your configuration

Page 3: (STG206) Using Amazon CloudFront For Your Websites & Apps

Best practices for setting up

your origin

Page 4: (STG206) Using Amazon CloudFront For Your Websites & Apps

Following origin best practices can give you…

Easier debugging

Better performance

Higher availability

Page 5: (STG206) Using Amazon CloudFront For Your Websites & Apps

Five best practices

1. Use Amazon Route 53 health checks and DNS failover

2. Configure multiple origins

3. Secure your origin

4. Log request IDs

5. Set origin response headers

Page 6: (STG206) Using Amazon CloudFront For Your Websites & Apps

Failover

Only return answers for resources

that are healthy and reachable

from the outside world, so that

your end users are routed away

from a failed or unhealthy part of

your application

Health Checks

Automated requests sent over

the Internet to your application

to verify that your application is

reachable, available, and

functional

+

Amazon Route 53 health checks and

DNS failover

Page 7: (STG206) Using Amazon CloudFront For Your Websites & Apps

Use Route 53 to improve availability & performance

Page 8: (STG206) Using Amazon CloudFront For Your Websites & Apps

Use Route 53 to improve availability & performance

Page 9: (STG206) Using Amazon CloudFront For Your Websites & Apps

Use Route 53 to improve availability & performance

Page 10: (STG206) Using Amazon CloudFront For Your Websites & Apps

Use Route 53 to improve availability & performance

Page 11: (STG206) Using Amazon CloudFront For Your Websites & Apps

Use Route 53 to improve availability & performance

eu-west-1

Page 12: (STG206) Using Amazon CloudFront For Your Websites & Apps

Configure multiple origins

Elastic Load

Balancing

Dynamic content

Amazon EC2

Static content

Amazon S3

*

(default)

/error/*

/assets/*

Amazon CloudFront

example.com

Page 13: (STG206) Using Amazon CloudFront For Your Websites & Apps

Access control: Restricting origin access

Amazon S3

Origin Access Identify (OAI)• Prevents direct access to your Amazon

S3 bucket

• Ensure performance benefits to all

customers

Custom origin

Block by IP address• Whitelist only the Amazon CloudFront

IP Range

• Protects origin from overload

• Ensure performance benefits to all

customers

Page 14: (STG206) Using Amazon CloudFront For Your Websites & Apps

Object Access Identity (OAI)

• Ensure only Amazon CloudFront

can access Amazon S3 bucket

• We make it simple for you

Amazon CloudFront

Region

Amazon S3

bucket

Custom origin

Page 15: (STG206) Using Amazon CloudFront For Your Websites & Apps

Object Access Identity (OAI)

• Ensure only Amazon CloudFront

can access Amazon S3 bucket

• We make it simple for you

Amazon CloudFront

Region

Amazon S3

bucket

Custom origin

Page 16: (STG206) Using Amazon CloudFront For Your Websites & Apps

Shield custom origin

• Shield your custom origin

• Whitelist Amazon CloudFront IP rangeAmazon CloudFront

Region

Amazon S3

bucket

Custom Origin

Page 17: (STG206) Using Amazon CloudFront For Your Websites & Apps

Shield custom origin

• Shield your custom origin

• Whitelist Amazon CloudFront IP rangeAmazon CloudFront

Region

Amazon S3

bucket

Custom origin

Page 18: (STG206) Using Amazon CloudFront For Your Websites & Apps

Shield custom origin

• Subscribe to Amazon SNS notifications on changes to

IP ranges

• Automatically update security groups

AWS Lambda

Amazon CloudFront

Amazon SNS

Security group

Web app

server

Web app

server

AWS IP ranges

Update IP rangeSNS message

Page 19: (STG206) Using Amazon CloudFront For Your Websites & Apps

Log Amazon CloudFront request IDs at origin

Nginx:

log_format main '$remote_addr - $remote_user [$time_local] "$request" ‘ '$status $body_bytes_sent "$http_referer" ‘ '"$http_user_agent" http_x_forwarded_for" "$http_x_amz_cf_id"';

Apache:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Amz-Cf-Id}i\"" combined

Page 20: (STG206) Using Amazon CloudFront For Your Websites & Apps

Set origin response headers

*Strict-Transport-Security: max-age=15552000;

*X-Frame-Options: SAMEORIGIN

*X-XSS-Protection: 1; mode=block Options

*Cache-Control: max-age=300; public

Page 21: (STG206) Using Amazon CloudFront For Your Websites & Apps

Demo

Page 22: (STG206) Using Amazon CloudFront For Your Websites & Apps

Key takeaways

• Use Amazon Route53’s health checks and DNS failover

• Improve security by setting headers at your origin

• Enable logging

• Serve your static assets on Amazon S3

• Serve error pages from Amazon S3

Page 23: (STG206) Using Amazon CloudFront For Your Websites & Apps

Gaining visibility into your distribution

Page 24: (STG206) Using Amazon CloudFront For Your Websites & Apps

Visibility into your distribution

Four different ways to get visibility:

• AWS CloudTrail – for monitoring distribution config changes

• Near real-time metrics – Provided by Amazon CloudFront

• Amazon CloudFront reports – for analytics

• Amazon CloudWatch Logs – for custom monitoring

Page 25: (STG206) Using Amazon CloudFront For Your Websites & Apps

Monitor distribution config changes

• Monitor changes to distribution configuration using

AWS CloudTrail

• Get alarms when:

• Distribution is disabled

• Trusted signers are disabled

• Custom TLS certificate changes

Page 26: (STG206) Using Amazon CloudFront For Your Websites & Apps

Amazon CloudFront near real-time metrics

• Provided in near real-time

• Via Amazon CloudWatch

• Alarm on 6 metrics:

• Requests

• Bytes downloaded

• Bytes uploaded

• 4XX error rate

• 5XX error rate

• Total error rate

Page 27: (STG206) Using Amazon CloudFront For Your Websites & Apps

Amazon CloudFront reports to identify trends

• Cache hit/miss

• Incomplete downloads

• Top countries

• Mobile users

• Popular objects

Page 28: (STG206) Using Amazon CloudFront For Your Websites & Apps

Generate custom metrics

• Amazon CloudFront access logs

• Delivered via Amazon Kinesis to Amazon S3 buckets

• Typically within an hour of an event happening

Edge locationAmazon

S3

Edge location

Edge location

Page 29: (STG206) Using Amazon CloudFront For Your Websites & Apps

Generate custom metrics

• Upload logs to Amazon CloudWatch using AWS Lambda

Amazon

CloudFront

Amazon

S3

AWS

LambdaAmazon

CloudWatchAlarm

Keywords e.g. Bots Granular Response Code e.g. 4XX Request to certain URLs TLS versions

Page 30: (STG206) Using Amazon CloudFront For Your Websites & Apps

Demo

Page 31: (STG206) Using Amazon CloudFront For Your Websites & Apps

Key takeaways

• Enable AWS CloudTrail

• Monitor your Amazon CloudFront reports

• Create alarms

• Subscribe to Amazon CloudFront access logs

• Push logs to Amazon CloudWatch

Page 32: (STG206) Using Amazon CloudFront For Your Websites & Apps

How to improve cacheability

Page 33: (STG206) Using Amazon CloudFront For Your Websites & Apps

Improving cacheability of your objects

• Versioning website assets

• Cache headers

• Shared distribution

• Forwarded values

• Path prefix invalidations

Page 34: (STG206) Using Amazon CloudFront For Your Websites & Apps

Versioning website assets

<link

href="//assets.example.com/assets/v1/css/jumbotron-narrow.css“rel="stylesheet">

<link

href="//assets.example.com/assets/v2/css/jumbotron-narrow.css“rel="stylesheet">

<link

href="//assets.example.com/assets/css/jumbotron-narrow.css?<md5sum>“rel="stylesheet">

Page 35: (STG206) Using Amazon CloudFront For Your Websites & Apps

Cache-Control & expires

< Cache-Control: max-age=300

< Cache-Control: max-age=30, s-maxage=3000

< Expires: Thu, 18 Sep 2025 21:34:50 GMT

Min TTL Default TTL Max TTL

Set min, max and default TTL on Amazon CloudFront

Page 36: (STG206) Using Amazon CloudFront For Your Websites & Apps

Cache-Control & expires

*.css, *.js or Images

Cache-Control: public; max-age=31536000

index.html

Cache-Control: no-cache=Set-Cookie; max-age=30

Live streaming

/*.m3u8 => Cache-Control: public; max-age=2

Page 37: (STG206) Using Amazon CloudFront For Your Websites & Apps

Shared distributions

• Shared assets across multiple properties

Static content

Amazon S3

Amazon CloudFront

assets.example.com

www.example.com

www.example.org

Page 38: (STG206) Using Amazon CloudFront For Your Websites & Apps

Forwarded values

• Check forwarded headers

• Query string forwarding

• Cookie forwarding

• Trusted Advisor checks

Page 39: (STG206) Using Amazon CloudFront For Your Websites & Apps

Invalidations

• Last resort

• Only applies to Amazon CloudFront cache, not browsers or

intermediary caches

Page 40: (STG206) Using Amazon CloudFront For Your Websites & Apps

Demo

Page 41: (STG206) Using Amazon CloudFront For Your Websites & Apps

Key takeaways

• Use versioning

• Use multiple cache behaviors

• Forward only required headers

• Use invalidations as last resort

Page 42: (STG206) Using Amazon CloudFront For Your Websites & Apps

How to test your configuration

Page 43: (STG206) Using Amazon CloudFront For Your Websites & Apps

Test your configuration

• Test in development mode

• Ensure content is cached correctly

• Performance testing

• Load testing

• Test SSL configuration

Page 44: (STG206) Using Amazon CloudFront For Your Websites & Apps

Turn on development mode

• Set maximum TTL to 0

• Without need to constantly invalidate!

• Forwarding all headers changes behavior

• Whitelist your IP using AWS WAF so only

accessible from corporate network

• Signed cookies

Page 45: (STG206) Using Amazon CloudFront For Your Websites & Apps

Ensure content is cached correctly

Check cache/hit miss ratio of distribution on reports

Developer console on Chrome or Firefox

• Age header

• X-Cache

• Cache-Control headers

• HSTS headers

Page 46: (STG206) Using Amazon CloudFront For Your Websites & Apps

Performance testing

Backbone testing Last mile testing Real user testing

Page 47: (STG206) Using Amazon CloudFront For Your Websites & Apps

Load testing

Traditional load testing

• Use one client

• From a single region

• Hits single IP

DNS load balancing

Simulate real user environment

Ideal load testing

• Clients from multiple locations

• Independent DNS requests

• Distribute across IPs

DNS load balancing

Simulate real user environment

Page 48: (STG206) Using Amazon CloudFront For Your Websites & Apps

SSL labs

• Verify your SSL config

Page 49: (STG206) Using Amazon CloudFront For Your Websites & Apps

Demo

Page 50: (STG206) Using Amazon CloudFront For Your Websites & Apps

Key takeaways

• Switch off caching during development mode

• Use RUM for performance testing

• If you load test - test from multiple locations

Page 51: (STG206) Using Amazon CloudFront For Your Websites & Apps

Thank you!

Page 52: (STG206) Using Amazon CloudFront For Your Websites & Apps

Remember to complete

your evaluations!

Page 53: (STG206) Using Amazon CloudFront For Your Websites & Apps

Related Sessions

SEC323: Securing Web Applications with AWS WAF

Friday, Oct 9 at 9:00 AM – 10:00 AM

Lando 4301B