Tải bản đầy đủ
Step 3.3: Transfer Other DNS Records from Your Current DNS Provider to Amazon Route 53

Step 3.3: Transfer Other DNS Records from Your Current DNS Provider to Amazon Route 53

Tải bản đầy đủ

Amazon Simple Storage Service Developer Guide
Example: Setting up a Static
Website Using a Custom Domain

On your DNS provider's site, update the NS record with the delegation set values of the hosted zone as
shown in the following Amazon Route 53 console screenshot. For more information, see Updating Your
DNS Service's Name Server Records in Amazon Route 53 Developer Guide.

When the transfer to Amazon Route 53 is complete, verify that the name server for your domain
has indeed changed. On a Linux computer, use the dig DNS lookup utility. For example, use this dig
command:
dig +recurse +trace www.example.com any

It returns the following output (only partial output is shown). The output shows the same name servers
on the Amazon Route 53 hosted zone that you created for the example.com domain.
...
example.com.
example.com.
example.com.
example.com.

172800
172800
172800
172800

www.example.com. 300
east-1.amazonaws.com.
...

IN
IN
IN
IN

NS
NS
NS
NS

ns-9999.awsdns-99.com.
ns-9999.awsdns-99.org.
ns-9999.awsdns-99.co.uk.
ns-9999.awsdns-99.net.

IN

CNAME

www.example.com.s3-website-us-

Step 5: Testing
To verify that the website is working correctly, in your browser, try the following URLs:
• http://example.com - Displays the index document in the example.com bucket.
• http://www.example.com- Redirects your request to http://example.com.
In some cases, you might need to clear the cache of your web browser to see the expected behavior.

API Version 2006-03-01
490

Amazon Simple Storage Service Developer Guide
Example: Speed Up Your Website with Amazon CloudFront

Example: Speed Up Your Website with Amazon
CloudFront
You can use Amazon CloudFront to improve the performance of your website. CloudFront makes your
website's files (such as HTML, images, and video) available from data centers around the world (called
edge locations). When a visitor requests a file from your website, CloudFront automatically redirects the
request to a copy of the file at the nearest edge location. This results in faster download times than if the
visitor had requested the content from a data center that is located farther away.
CloudFront caches content at edge locations for a period of time that you specify. If a visitor requests
content that has been cached for longer than the expiration date, CloudFront checks the origin server
to see if a newer version of the content is available. If a newer version is available, CloudFront copies the
new version to the edge location. Changes that you make to the original content are replicated to edge
locations as visitors request the content.
To speed up your website, use CloudFront to complete the following tasks.
Tasks
• Create a CloudFront Distribution (p. 491)
• Update the Record Sets for Your Domain and Subdomain (p. 492)
• (Optional) Check the Log Files (p. 493)

Create a CloudFront Distribution
First, you create a CloudFront distribution. This makes your website available from data centers around
the world.

To create a distribution with an Amazon S3 origin
1.
2.

Open the CloudFront console at https://console.aws.amazon.com/cloudfront/.
Choose Create Distribution.

3.
4.

On the Select a delivery method for your content page, for Web, choose Get Started.
On the Create Distribution page, in the Origin Settings section, for Origin Domain Name, type the
Amazon S3 static website hosting endpoint for your bucket. For example, example.com.s3-websiteus-east-1.amazonaws.com.

Note

Be sure to specify the static website hosting endpoint, not the name of the bucket. For
more information, see Amazon Simple Storage Service Website Endpoints.
5.

CloudFront fills in the Origin ID for you.
For Default Cache Behavior Settings, leave the values set to the defaults. For more information
about these configuration options, see Values that You Specify When You Create or Update a Web
Distribution in the Amazon CloudFront Developer Guide.

6.

For Distribution Settings, do the following:
a.
b.

c.

Leave Price Class set to Use All Edge Locations (Best Performance).
Set Alternate Domain Names (CNAMEs) to the root domain and www subdomain; in this tutorial,
these are example.com and www.example.com. These values must be set before you create aliases
for the A records that connect the specified domain names to the CloudFront distribution.
Set Default Root Object to index.html. This is the default page that the CloudFront
distribution returns if the URL used to access the distribution doesn't contain a file name. This
value should match the index document value that you set in Configuring a Bucket for Website
Hosting (p. 469).
API Version 2006-03-01
491

Amazon Simple Storage Service Developer Guide
Example: Speed Up Your Website with Amazon CloudFront

7.

d.

Set Logging to On.

e.

For Bucket for Logs, choose the logging bucket that you created.

f.

To store the logs generated by traffic to the CloudFront distribution in a folder, named cdn, in
the log bucket, type cdn/ for Log Prefix.

g.

Leave the other settings at their default values.

Choose Create Distribution.

To see the status of the distribution, find the distribution in the console and check the Status column. A
status of InProgress indicates that the distribution is not yet fully deployed.
After your distribution is deployed, you can reference your content with the new CloudFront domain
name. Record the value of Domain Name shown in the CloudFront console. You'll need it in the next
step. In this example, the value is dj4p1rv6mvubz.cloudfront.net.
To verify that your CloudFront distribution is working, type the domain name of the distribution in a web
browser. If it is working, your website is visible.

Update the Record Sets for Your Domain and Subdomain
Now that you have successfully created a CloudFront distribution, update the A records in Amazon
Route 53 to point to the new CloudFront distribution.

To update A records to point to a CloudFront distribution
1.

Open the Amazon Route 53 console at https://console.aws.amazon.com/route53/.

2.

On the Hosted Zones page, choose the hosted zone that you created for your domain.

3.

Choose Go to Record Sets.

4.

Choose the A record that you created for the www subdomain.

5.

For Alias Target, choose the CloudFront distribution.

6.

Choose Save Record Set.

7.

To redirect the A record for the root domain to the CloudFront distribution, repeat this procedure.

The update to the record sets takes effect within 2 to 48 hours. To see if the new A records have taken
effect, in a web browser, type http://www.example.com. If the browser no longer redirects you to
http://example.com, the new A records are in place.
This change in behavior occurs because traffic routed by the old A record to the www subdomain S3 bucket
is redirected by the settings in Amazon S3 to the root domain. When the new A record has taken effect,
traffic routed by the new A record to the CloudFront distribution is not redirected to the root domain.

Tip

Browsers can cache redirect settings. If you think the new A record settings should have taken
effect, but your browser still redirects http://www.example.com to http://example.com, try
clearing your browser history and cache, closing and reopening your browser application, or
using a different web browser.
When the new A records are in effect, any visitors who reference the site by using http://example.com
or http://www.example.com are redirected to the nearest CloudFront edge location, where they benefit
from faster download times.
If you created your site as a learning exercise only, you can delete the resources that you allocated so
that you no longer accrue charges. To do so, continue on to Clean Up Your Example Resources (p. 493).
After you delete your AWS resources, your website is no longer available.
API Version 2006-03-01
492

Amazon Simple Storage Service Developer Guide
Clean Up Example Resources

(Optional) Check the Log Files
The access logs tell you how many people are visiting the website. They also contain valuable business
data that you can analyze with other services, such as Amazon EMR.
In your bucket, older Amazon S3 log files are located in the root folder. All new log files, which should be
CloudFront logs, are located in the cdn folder. Amazon S3 writes website access logs to your log bucket
every two hours. CloudFront writes logs to your log bucket within 24 hours from when the corresponding
requests are made.

To see the log files for your website
1.

Open the Amazon S3 console at https://console.aws.amazon.com/s3/.

2.

Choose the logging bucket for your website.

3.

To see the log files that are stored in the cdn or root folder, choose cdn or root.

4.

Open Amazon S3 log files, which are text files, in a browser. Download the .gzip files written by
CloudFront before opening them.

Clean Up Your Example Resources
If you created your static website as a learning exercise only, be sure to delete the AWS resources that
you allocated so that you no longer accrue charges. After you delete your AWS resources, your website is
no longer available.
Tasks
• Delete the Amazon CloudFront Distribution (p. 493)
• Delete the Amazon Route 53 Hosted Zone (p. 493)
• Delete the S3 Bucket (p. 494)

Delete the Amazon CloudFront Distribution
Before you delete an Amazon CloudFront distribution, you must disable it. A disabled distribution is no
longer functional and does not accrue charges. You can enable a disabled distribution at any time. After
you delete a disabled distribution, it is no longer available.

To disable and delete a CloudFront distribution
1.

Open the CloudFront console at https://console.aws.amazon.com/cloudfront/.

2.

Select the distribution that you want to disable, and then choose Disable.

3.

When prompted for confirmation, choose Yes, Disable.

4.

Select the disabled distribution, and then choose Delete.

5.

When prompted for confirmation, choose Yes, Delete.

Delete the Amazon Route 53 Hosted Zone
Before you delete the hosted zone, you must delete the record sets that you created. You don't need to
delete the NS and SOA records; these are automatically deleted when you delete the hosted zone.

To delete the record sets
1.

Open the Amazon Route 53 console at https://console.aws.amazon.com/route53/.
API Version 2006-03-01
493

Amazon Simple Storage Service Developer Guide
Clean Up Example Resources

2.
3.
4.
5.

In the list of domain names, select your domain name, and then choose Go to Record Sets.
In the list of record sets, select the A records that you created. The type of each record set is listed in
the Type column.
Choose Delete Record Set.
When prompted for confirmation, choose Confirm.

To delete an Amazon Route 53 hosted zone
1.
2.
3.

Continuing from the previous procedure, choose Back to Hosted Zones.
Select your domain name, and then choose Delete Hosted Zone.
When prompted for confirmation, choose Confirm.

Delete the S3 Bucket
Before you delete your S3 bucket, make sure that logging is disabled for the bucket. Otherwise, AWS
continues to write logs to your bucket as you delete it.

To disable logging for a bucket
1.
2.

Open the Amazon S3 console at https://console.aws.amazon.com/s3/.
Select your bucket, and then choose Properties.

3.
4.
5.

From Properties, choose Logging.
Clear the Enabled check box.
Choose Save.

Now, you can delete your bucket. For more information, see How Do I Delete an S3 Bucket? in the
Amazon Simple Storage Service Console User Guide.

API Version 2006-03-01
494

Amazon Simple Storage Service Developer Guide
Overview

Configuring Amazon S3 Event
Notifications

The Amazon S3 notification feature enables you to receive notifications when certain events happen
in your bucket. To enable notifications, you must first add a notification configuration identifying
the events you want Amazon S3 to publish, and the destinations where you want Amazon S3 to
send the event notifications. You store this configuration in the notification subresource (see Bucket
Configuration Options (p. 60)) associated with a bucket. Amazon S3 provides an API for you to manage
this subresource.
Topics
• Overview (p. 495)
• How to Enable Event Notifications (p. 496)
• Event Notification Types and Destinations (p. 498)
• Configuring Notifications with Object Key Name Filtering (p. 499)
• Granting Permissions to Publish Event Notification Messages to a Destination (p. 504)
• Example Walkthrough 1: Configure a Bucket for Notifications (Message Destination: SNS Topic and
SQS Queue) (p. 505)
• Example Walkthrough 2: Configure a Bucket for Notifications (Message Destination: AWS
Lambda) (p. 511)
• Event Message Structure (p. 511)

Overview
Currently, Amazon S3 can publish the following events:
• A new object created event—Amazon S3 supports multiple APIs to create objects. You can request
notification when only a specific API is used (e.g., s3:ObjectCreated:Put) or you can use a wildcard
(e.g., s3:ObjectCreated:*) to request notification when an object is created regardless of the API used.
• An object removal event—Amazon S3 supports deletes of versioned and unversioned objects. For
information about object versioning, see Object Versioning (p. 110) and Using Versioning (p. 444).
API Version 2006-03-01
495

Amazon Simple Storage Service Developer Guide
How to Enable Event Notifications

You can request notification when an object is deleted or a versioned object is permanently deleted by
using the s3:ObjectRemoved:Delete event type. Or you can request notification when a delete marker
is created for a versioned object by using s3:ObjectRemoved:DeleteMarkerCreated. You can also use
a wildcard s3:ObjectRemoved:* to request notification anytime an object is deleted. For information
about deleting versioned objects, see Deleting Object Versions (p. 457).
• A Reduced Redundancy Storage (RRS) object lost event—Amazon S3 sends a notification message
when it detects that an object of the RRS storage class has been lost.
For a list of supported event types, see Supported Event Types (p. 498).
Amazon S3 supports the following destinations where it can publish events:
• Amazon Simple Notification Service (Amazon SNS) topic
Amazon SNS is a flexible, fully managed push messaging service. Using this service, you can push
messages to mobile devices or distributed services. With SNS you can publish a message once, and
deliver it one or more times. An SNS topic is an access point that recipients can dynamically subscribe
to in order to receive event notifications. For more information about SNS, see the Amazon SNS
product detail page.
• Amazon Simple Queue Service (Amazon SQS) queue
Amazon SQS is a scalable and fully managed message queuing service. You can use SQS to transmit
any volume of data without requiring other services to be always available. In your notification
configuration you can request that Amazon S3 publish events to an SQS queue. For more information
about SQS, see Amazon SQS product detail page.
• AWS Lambda
AWS Lambda is a compute service that makes it easy for you to build applications that respond quickly
to new information. AWS Lambda runs your code in response to events such as image uploads, in-app
activity, website clicks, or outputs from connected devices. You can use AWS Lambda to extend other
AWS services with custom logic, or create your own back-end that operates at AWS scale, performance,
and security. With AWS Lambda, you can easily create discrete, event-driven applications that execute
only when needed and scale automatically from a few requests per day to thousands per second.
AWS Lambda can run custom code in response to Amazon S3 bucket events. You upload your custom
code to AWS Lambda and create what is called a Lambda function. When Amazon S3 detects an event
of a specific type (for example, an object created event), it can publish the event to AWS Lambda
and invoke your function in Lambda. In response, AWS Lambda executes your function. For more
information, see AWS Lambda product detail page.
The following sections offer more detail about how to enable event notifications on a bucket. The
subtopics also provide example walkthroughs to help you explore the notification feature.
• Example Walkthrough 1: Configure a Bucket for Notifications (Message Destination: SNS Topic and
SQS Queue) (p. 505)
• Example Walkthrough 2: Configure a Bucket for Notifications (Message Destination: AWS
Lambda) (p. 511)

How to Enable Event Notifications
Enabling notifications is a bucket-level operation; that is, you store notification configuration
information in the notification subresource associated with a bucket. You can use any of the following
methods to manage notification configuration:
API Version 2006-03-01
496

Amazon Simple Storage Service Developer Guide
How to Enable Event Notifications

• Using the Amazon S3 console
The console UI enables you to set a notification configuration on a bucket without having to write any
code. For instruction, see How Do I Enable and Configure Event Notifications for an S3 Bucket? in the
Amazon Simple Storage Service Console User Guide.
• Programmatically using the AWS SDKs

Note

If you need to, you can also make the Amazon S3 REST API calls directly from your code.
However, this can be cumbersome because it requires you to write code to authenticate your
requests.
Internally, both the console and the SDKs call the Amazon S3 REST API to manage notification
subresources associated with the bucket. For notification configuration using AWS SDK examples, see
the walkthrough link provided in the preceding section.
Regardless of the method you use, Amazon S3 stores the notification configuration as XML in the
notification subresource associated with a bucket. For information about bucket subresources, see
Bucket Configuration Options (p. 60)). By default, notifications are not enabled for any type of event.
Therefore, initially the notification subresource stores an empty configuration.



To enable notifications for events of specific types, you replace the XML with the appropriate
configuration that identifies the event types you want Amazon S3 to publish and the destination
where you want the events published. For each destination, you add a corresponding XML
configuration. For example:
• Publish event messages to an SQS queue—To set an SQS queue as the notification destination for
one or more event types, you add the QueueConfiguration.


optional-id-string
sqs-queue-arn
event-type
event-type
...

...


• Publish event messages to an SNS topic—To set an SNS topic as the notification destination for
specific event types, you add the TopicConfiguration.


optional-id-string
sns-topic-arn
event-type
event-type
...

...


• Invoke the AWS Lambda function and provide an event message as an argument—To set
a Lambda function as the notification destination for specific event types, you add the
CloudFunctionConfiguration.
API Version 2006-03-01
497

Amazon Simple Storage Service Developer Guide
Event Notification Types and Destinations


  
optional-id-string   
cloud-function-arn        
event-type      
event-type      
...  

...


To remove all notifications configured on a bucket, you save an empty
element in the notification subresource.
When Amazon S3 detects an event of the specific type, it publishes a message with the event
information. For more information, see Event Message Structure (p. 511).

Event Notification Types and Destinations
This section describes the event notification types that are supported by Amazon S3 and the type of
destinations where the notifications can be published.

Supported Event Types
Amazon S3 can publish events of the following types. You specify these event types in the notification
configuration.
Event types

Description

s3:ObjectCreated:*

s3:ObjectCreated:Post

Amazon S3 APIs such as PUT, POST, and COPY can create an
object. Using these event types, you can enable notification
when an object is created using a specific API, or you can
use the s3:ObjectCreated:* event type to request notification
regardless of the API that was used to create an object.

s3:ObjectCreated:Copy

You will not receive event notifications from failed operations.

s3:ObjectCreated:Put

s3:ObjectCreated:CompleteMultipartUpload
s3:ObjectRemoved:*
s3:ObjectRemoved:Delete

By using the ObjectRemoved event types, you can enable
notification when an object or a batch of objects is removed
from a bucket.

s3:ObjectRemoved:DeleteMarkerCreated You can request notification when an object is deleted or
a versioned object is permanently deleted by using the
s3:ObjectRemoved:Delete event type. Or you can request
notification when a delete marker is created for a versioned
object by using s3:ObjectRemoved:DeleteMarkerCreated.
For information about deleting versioned objects, see
Deleting Object Versions (p. 457). You can also use a wildcard
s3:ObjectRemoved:* to request notification anytime an object
is deleted.
You will not receive event notifications from automatic deletes
from lifecycle policies or from failed operations.

API Version 2006-03-01
498

Amazon Simple Storage Service Developer Guide
Supported Destinations

Event types

Description

s3:ReducedRedundancyLostObject

You can use this event type to request Amazon S3 to send a
notification message when Amazon S3 detects that an object
of the RRS storage class is lost.

Supported Destinations
Amazon S3 can send event notification messages to the following destinations. You specify the ARN
value of these destinations in the notification configuration.
• Publish event messages to an Amazon Simple Notification Service (Amazon SNS) topic
• Publish event messages to an Amazon Simple Queue Service (Amazon SQS) queue

Note

At this time S3 supports only standard SQS queues that are not server-side encryption (SSE)
enabled.
• Publish event messages to AWS Lambda by invoking a Lambda function and providing the event
message as an argument
You must grant Amazon S3 permissions to post messages to an Amazon SNS topic or an Amazon
SQS queue. You must also grant Amazon S3 permission to invoke an AWS Lambda function on your
behalf. For information about granting these permissions, see Granting Permissions to Publish Event
Notification Messages to a Destination (p. 504).

Configuring Notifications with Object Key Name
Filtering
You can configure notifications to be filtered by the prefix and suffix of the key name of objects. For
example, you can set up a configuration so that you are sent a notification only when image files with
a ".jpg" extension are added to a bucket. Or you can have a configuration that delivers a notification to
an Amazon SNS topic when an object with the prefix "images/" is added to the bucket, while having
notifications for objects with a "logs/" prefix in the same bucket delivered to an AWS Lambda function.
You can setup notification configurations that use object key name filtering in the Amazon S3 console
and by using Amazon S3 APIs through the AWS SDKs or the REST APIs directly. For information about
using the console UI to set a notification configuration on a bucket, see How Do I Enable and Configure
Event Notifications for an S3 Bucket? in the Amazon Simple Storage Service Console User Guide.
Amazon S3 stores the notification configuration as XML in the notification subresource associated with
a bucket as described in How to Enable Event Notifications (p. 496). You use the Filter XML structure
to define the rules for notifications to be filtered by the prefix and/or suffix of an object key name. For
information about the details of the Filter XML structure, see PUT Bucket notification in the Amazon
Simple Storage Service API Reference.
Notification configurations that use Filter cannot define filtering rules with overlapping prefixes,
overlapping suffixes, or prefix and suffix overlapping. The following sections have examples of valid
notification configurations with object key name filtering and examples of notification configurations
that are invalid because of prefix/suffix overlapping.

API Version 2006-03-01
499

Amazon Simple Storage Service Developer Guide
Examples of Valid Notification Configurations
with Object Key Name Filtering

Examples of Valid Notification Configurations with
Object Key Name Filtering
The following notification configuration contains a queue configuration identifying an Amazon SQS
queue for Amazon S3 to publish events to of the s3:ObjectCreated:Put type. The events will be
published whenever an object that has a prefix of images/ and a jpg suffix is PUT to a bucket.


1



prefix
images/


suffix
jpg



arn:aws:sqs:us-west-2:444455556666:s3notificationqueue
s3:ObjectCreated:Put



The following notification configuration has multiple non-overlapping prefixes. The configuration
defines that notifications for PUT requests in the images/ folder will go to queue-A while notifications
for PUT requests in the logs/ folder will go to queue-B.


1



prefix
images/



arn:aws:sqs:us-west-2:444455556666:sqs-queue-A
s3:ObjectCreated:Put


2



prefix
logs/



arn:aws:sqs:us-west-2:444455556666:sqs-queue-B
s3:ObjectCreated:Put



API Version 2006-03-01
500