Create a Lightsail content delivery network distribution - Amazon Lightsail

Create a Lightsail content delivery network distribution

In this guide, we show you how to create an Amazon Lightsail distribution using the Lightsail console, and describe the distribution settings that you can configure. For more information about distributions, see Content delivery network distributions.

Contents

Prerequisites

Complete the following prerequisites before you get started with creating a distribution:

  1. Complete one of the following, depending on whether you want to use an instance, container service, or a bucket with your distribution.

    • Create a Lightsail instance to host your content. The instance serves as the origin of your distribution. The origin stores the original, definitive version of your content. For more information, see Create an instance.

      Attach a Lightsail static IP to your instance. Your instance's default public IP address changes if you stop and start your instance, which will break the connection between your distribution and your origin instance. A static IP does not change if you stop and start your instance. For more information, see Create a static IP and attach it to an instance.

      Upload your content and files to your instance. Your files, also known as objects, typically include web pages, images, and media files, but can be anything that can be served over HTTP.

    • Create a Lightsail container service to host your website or web application. The container service serves as the origin of your distribution. The origin stores the original, definitive version of your content. For more information, see Creating Amazon Lightsail container services.

    • Create a Lightsail bucket to store your static content. The bucket serves as the origin of your distribution. The origin stores the original, definitive version of your content. For more information, see Create a bucket.

      Upload files to your bucket using the Lightsail console, AWS Command Line Interface (AWS CLI), and AWS APIs. For more information about uploading files, see Upload files to a bucket.

  2. (Optional) Create a Lightsail load balancer if your website requires fault tolerance. Then attach multiple copies of your instance to your load balancer. You can configure your load balancer (with one or more instances attached to it) as the origin of your distribution, instead of configuring your instance as the origin. For more information, see Create a load balancer and attach instances to it.

Origin resource

An origin is the definitive source of content for your distribution. When you create your distribution, you choose the Lightsail instance, container service, bucket, or load balancer (with one or more instances attached to it) that hosts the content of your website or web application.

Note

IPv6-only instances cannot be configured as the origin for a Lightsail content delivery network (CDN) distribution at this time.

You can choose only one origin per distribution. You can change the origin at any time after you create your distribution. For more information, see Change the origin of your distribution.

Distribution origin selector

Origin protocol policy

The origin protocol policy is the protocol policy that your distribution uses when pulling content from your origin. After you choose an origin for your distribution, you should determine if your distribution should use Hypertext Transfer Protocol (HTTP) or Hypertext Transfer Protocol Secure (HTTPS) when pulling content from your origin. If your origin is not configured for HTTPS, then you must use HTTP.

You can choose one of the following origin protocol policies for your distribution:

  • HTTP Only - Your distribution uses only HTTP to access the origin. This is the default setting.

  • HTTPS Only - Your distribution uses only HTTPS to access the origin.

The steps to edit your origin protocol policy are included in the Create a distribution section later in this guide.

Note

When you select a Lightsail bucket as the origin of your distribution, the Origin protocol policy defaults to HTTPS only. You cannot change the origin protocol policy when a bucket is the origin of your distribution.

Caching behavior and caching presets

A caching preset automatically configures the settings of your distribution for the type of content that you host on your origin. For example, choosing the Best for static content preset automatically configures your distribution with settings that work best with static websites. If your website is hosted on a WordPress instance, then choose the Best for WordPress preset to have your distribution automatically configured to work with your WordPress website.

Note

The caching preset options are not available when you select a Lightsail bucket as the origin of your distribution. We automatically apply distribution settings that are best for static content being stored in a bucket.

You can choose one of the follow caching presets for your distribution:

  • Best for static content - This preset configures your distribution to cache everything. This preset is ideal if you host static content (e.g., static HTML pages) on your origin, or content that does not change for each user who visits your website. All content on your distribution is cached when you choose this preset.

  • Best for dynamic content - This preset configures your distribution to cache nothing except the files that you specify as Cache in the Directory and file overrides section of the Create a distribution page. For more information, see Directory and file overrides later in this guide. This preset is ideal if you host dynamic content on your origin, or content that may change for each user who visits your website or web application.

  • Best for WordPress - This preset configures your distribution to cache nothing except the files in the wp-includes/ and wp-content/ directories of your WordPress instance. This preset is ideal if your origin is an instance that uses the WordPress Certified by Bitnami and Automattic blueprint (excluding the multisite blueprint). For more information about this preset, see Best for WordPress caching preset.

    Note

    The Custom settings preset cannot be selected. It is automatically selected for you if you choose a preset but then manually modify your distribution's settings.

A caching preset can be specified only in the Lightsail console. It cannot be specified using the Lightsail API, AWS CLI, and SDKs.

Best for WordPress caching preset

When you select an instance that uses the WordPress Certified by Bitnami and Automattic blueprint as the origin of your distribution, Lightsail asks if you want to apply the Best for WordPress caching preset to your distribution. If you apply the present, then your distribution is automatically configured to work best with your WordPress website. There are no other distribution settings that you need to apply. The Best for WordPress preset to cache nothing except the files in the wp-includes/ and wp-content/ directories of your WordPress website. It also configures your distribution to clear its cache every day (cache lifespan of 1 day), allow all HTTP methods, forward only the Host header, forward no cookies, and forwards all query strings.

Important

You must edit the WordPress configuration file in your instance to make your WordPress website work with your distribution. For more information, see Configure your WordPress instance to work with your distribution.

Default behavior

A default behavior specifies how your distribution handles content caching. The default behavior of your distribution is automatically specified for you depending on the caching preset that you select. If you select a different default behavior, then the caching preset is automatically changed to Custom settings.

Note

The default behavior options are not available when you select a Lightsail bucket as the origin of your distribution. We automatically apply distribution settings that are best for static content being stored in a bucket.

You can choose one of the follow default behaviors for your distribution:

  • Cache everything - This behavior configures your distribution to cache and serve your entire website as static content. This option is ideal if your origin hosts content that doesn't change depending on who views it, or if your website does not use cookies, headers, or query strings to personalize content.

  • Cache nothing - This behavior configures your distribution to cache only the origin files and folder paths that you specify. This option is ideal if your website or web application uses cookies, headers, and query strings to personalize content for individual users. If you select this option, you must specify the directory and file path overrides to cache.

Directory and file overrides

A directory and file override can be used to override, or add an exception to, the default behavior you selected. For example, if you chose to cache everything, use an override to specify a directory, file, or file type that your distribution shouldn't cache. Alternately, if you chose to cache nothing, use an override to specify a directory, file, or file type that your distribution should cache.

In the Directory and file overrides section of the page, you can specify a path to a directory or a file to cache, or not cache. Use an asterisk symbol to specify wildcard directories (path/to/assets/*), and file types (*.html, *jpg, *js). Directories and file paths are case-sensitive.

Note

The directory and file override options are not available when you select a Lightsail bucket as the origin of your distribution. Everything that is stored in the selected bucket is cached.

These are just a few examples of how you can specify directory and file overrides:

  • Specify the following to cache all files in the document root of an Apache web server running on a Lightsail instance.

    var/www/html/
  • Specify the following file to cache only the index page in the document root of an Apache web server.

    var/www/html/index.html
  • Specify the following to cache only the .html files in the document root of an Apache web server.

    var/www/html/*.html
  • Specify the following to cache only the .jpg, .png, and .gif files in the images sub-directory of the document root of an Apache web server.

    var/www/html/images/*.jpg
    var/www/html/images/*.png
    var/www/html/images/*.gif

    Specify the following to cache all files in the images sub-directory of the document root of an Apache web server.

    var/www/html/images/

Advanced cache settings

The advanced settings can be used to specify the cache lifespan of content on your distribution, the allowed HTTP methods, HTTP header forwarding, cookie forwarding, and query string forwarding. The advanced settings that you specify apply only to the directory and files that your distribution caches, including the directory and file overrides that you specify as Cache.

Note

The advanced cache settings are not available on the Create distribution page when you select a Lightsail bucket as the origin of your distribution. We automatically apply distribution settings that are best for static content being stored in a bucket. However, you can modify the advanced cache settings in the distribution management page after your distribution is created.

You can configure the following advanced settings:

Cache lifespan (TTL)

Controls the amount of time your content stays in your distribution's cache before your distribution forwards another request to your origin to determine if your content has been updated. The default value is one day. Reducing the duration allows you to better serve dynamic content. Increasing the duration means that your users get better performance because your files are more likely to be served directly from the edge location. Increasing the duration also reduces the load on your origin, because your distribution pulls content less frequently.

Note

The cache lifespan value that you specify applies only when your origin does not add HTTP headers such as Cache-Control max-age, Cache-Control s-maxage, or Expires to your content.

Allowed HTTP methods

Controls the HTTP methods that your distribution processes and forwards to your origin. HTTP methods indicate the desired action to be performed on the origin. For example, the GET method retrieves data from your origin, and the PUT method requests that the enclosed entity be stored on your origin.

You can choose one of the following HTTP method options for your distribution:

  • Allow GET, HEAD, OPTIONS, PUT, PATCH, POST, and DELETE methods

  • Allow the GET, HEAD, and OPTIONS methods

  • Allow the GET and HEAD methods

Your distribution always caches responses to the GET and HEAD requests. Your distribution also caches responses to the OPTIONS requests, if you choose to allow those requests. Your distribution does not cache responses to any other HTTP methods. For more information, see HTTP methods.

Important

If you configure your distribution to allow all of the HTTP methods that are supported, you must configure your origin instance to handle all methods. For example, if you configure your distribution to allow these methods because you want to use POST, you must configure your origin server to handle DELETE requests appropriately so viewers can't delete resources that you don't want them to. For more information, search the documentation for your website or web application.

HTTP header forwarding

Controls whether your distribution caches your content based on the values of specified headers, and if so, which ones. HTTP headers carry information about the client browser, the requested page, the origin and more. For example, the Accept-Language header sends the language of the client (e.g., en-US for English), so that the origin can respond with content in the language of the client, if it's available.

You can choose one of the following HTTP header options for your distribution:

  • Forward no headers

  • Forward only the headers I specify

When you select Forward no headers, your distribution doesn't cache your content based on header values. Regardless of the option that you select, your distribution forwards certain headers to your origin and takes specific actions based on the headers that you forward. For more information about how your distribution handles header forwarding, see HTTP request headers and distribution behavior.

Cookie forwarding

Controls whether your distribution forwards cookies to your origin and, if so, which ones. A cookie contains a small piece of data sent to the origin, such as information about a visitor's actions on a web page of your origin, as well as any information the visitor has provided, such as their name and interests.

You can choose one of the following cookie forwarding options for your distribution:

  • Don't forward cookies

  • Forward all cookies

  • Forward cookies I specify

If you choose Forward all cookies, your distribution forwards all cookies regardless of how many your application uses. If you chose Forward cookies I specify, then enter the names of cookies that you want your distribution to forward in the text box that appears. You can specify the following wildcards when you specify cookie names:

  • * matches 0 or more characters in the cookie name

  • ? matches exactly one character in the cookie name

For example, suppose that a viewer's request for an object includes a cookie named userid_member-number. Where each of your users has a unique value for member-number (userid_123, userid_124, userid_125, etc.). You want your distribution to cache a separate version of the content for each member. You could accomplish this by forwarding all cookies to your origin, but the viewer requests include some cookies that you don't want your distribution to cache. You could specify the following value as a cookie name, which causes your distribution to forward all of the cookies that begin with userid_ to your origin: userid_*

Query string forwarding

Controls whether your distribution forwards query strings to your origin and, if so, which ones. A query string is a part of a URL that assigns values to specified parameters. For example, the https://example.com/over/there?name=ferret URL contains the name=ferret query string. When a server receives a request for such a page, it may run a program, passing the name=ferret query string unchanged, to the program. The question mark is used as a separator, and is not part of the query string.

You can choose to have your distribution forward no query strings, or forward only the query strings that you specify. Choose not to forward query strings if your origin returns the same version of your content regardless of the values of query string parameters. This increases the likelihood that your distribution can serve a request from the cache, which improves performance and reduces the load on your origin. Choose to forward only the query strings that you specify if your origin server returns different versions of your content based on one or more query string parameters.

Distribution plan

A distribution plan specifies the monthly data transfer quota and cost of your distribution. If your distribution transfers more data than your plan's monthly data transfer quota, you are charged an overage. For more information, see the Lightsail pricing page.

To avoid an overage fee, change your distribution's current plan to a different plan that offers a greater amount of monthly data transfer before your distribution exceeds its monthly quota. You can change your distribution's plan only one time during each AWS billing cycle. For more information about changing your distributions plan after you create it, see Change the plan of your distribution.

Create a distribution

Complete the following procedure to create a distribution.

  1. Sign in to the Lightsail console.

  2. In the left navigation pane, choose Networking.

  3. Choose Create distribution.

  4. In the Choose your origin section of the page, choose the AWS Region in which your origin resource was created.

    Distributions are global resources. They can reference an origin in any AWS Region, and distribute its content globally.

  5. Choose your origin. An origin can be a Lightsail instance, container service, bucket, or a load balancer (with one or more instances attached to it). For more information, see Origin resource.

    Important

    If you choose a Lightsail container service as the origin of your distribution, Lightsail automatically adds the default domain name of your distribution as a custom domain on your container service. This enables traffic to be routed between your distribution and your container service. However, there are some circumstances in which you might need to manually add the default domain name of your distribution to your container service. For more information, see Add the default domain of a distribution to a container service.

  6. (Optional) To change your origin protocol policy, choose the pencil icon displayed next to the current origin protocol policy that your distribution uses. For more information, see Origin protocol policy.

    This option is listed in the Choose your origin section of the page, under the origin resource you selected for your distribution.

    Note

    When you select a Lightsail bucket as the origin of your distribution, the Origin protocol policy defaults to HTTPS only. You cannot change the origin protocol policy when a bucket is the origin of your distribution.

    Origin protocol policy
  7. Choose the caching behavior (also known as a caching preset) for your distribution. For more information, see Caching behavior and caching preset.

    Note

    The caching preset options are not available when you select a Lightsail bucket as the origin of your distribution. We automatically apply distribution settings that are best for static content being stored in a bucket.

  8. (Optional) Choose Show all settings to view additional caching behavior settings for your distribution.

    Note

    The caching behavior settings are not available when you select a Lightsail bucket as the origin of your distribution. We automatically apply distribution settings that are best for static content being stored in a bucket.

  9. (Optional) Choose the default behavior for your distribution. For more information, see Default behavior.

    Note

    The default behavior options are not available when you select a Lightsail bucket as the origin of your distribution. We automatically apply distribution settings that are best for static content being stored in a bucket.

  10. (Optional) Choose Add path to add a directory and file override to your distribution's caching behavior. For more information, see Directory and file overrides.

    Note

    The directory and file override options are not available when you select a Lightsail bucket as the origin of your distribution. We automatically apply distribution settings that are best for static content being stored in a bucket.

  11. (Optional) Choose the pencil icon displayed next to the advanced setting you want to edit for your distribution. For more information, see Advanced cache settings.

    Note

    The advanced cache settings are not available on the Create distribution page when you select a Lightsail bucket as the origin of your distribution. We automatically apply distribution settings that are best for static content being stored in a bucket. However, you can modify the advanced cache settings in the distribution management page after your distribution is created.

  12. Choose your distribution plan. For more information, see Distribution plans.

  13. Enter a name for your distribution.

    Resource names:

    • Must be unique within each AWS Region in your Lightsail account.

    • Must contain 2 to 255 characters.

    • Must start and end with an alphanumeric character or number.

    • Can include alphanumeric characters, numbers, periods, dashes, and underscores.

  14. Review the cost of your distribution.

  15. Choose Create distribution.

    Your distribution is created after a few moments.

Next steps

We recommend that you complete the following next steps after your distribution is up and running.

  1. If your distribution's origin is a WordPress instance, you must edit the WordPress configuration file in your instance to make your WordPress website work with your distribution. For more information, see Configure your WordPress instance to work with your distribution.

  2. (Optional) Create a Lightsail DNS zone to manage your domain's DNS in the Lightsail console. This allows you to easily map your domain to your Lightsail resources. For more information, see Create a DNS zone to manage your domain’s DNS records. Alternately, you can continue hosting your domain's DNS where it's currently being hosted.

  3. Create a Lightsail SSL/TLS certificate for your domain to use it with your distribution. Lightsail distributions require HTTPS, so you must request an SSL/TLS certificate for your domain before you can use it with your distribution. For more information, see Create SSL/TLS certificates for your distribution.

  4. Enable custom domains for your distribution to use your domain with your distribution. Enabling custom domains requires that you specify the Lightsail SSL/TLS certificate that you created for your domain. This adds your domain to your distribution and enables HTTPS. For more information, see Enable custom domains for your distribution.

  5. Add an alias record to your domain's DNS to begin routing traffic for your domain to your distribution. After you add the alias record, users who visit your domain are routed through your distribution. For more information, see Point your domain to a distribution.

  6. Test that your distribution is caching your content. For more information, see Test your distribution.