Skip to main content

AWS Static Website

The AWS Static Website template scaffolds a Pulumi project that stores site files in a private Amazon S3 bucket and serves them through an Amazon CloudFront distribution using Origin Access Control. The bucket isn’t publicly readable; CloudFront authenticates to the S3 REST endpoint via a bucket policy scoped to the distribution. The template ships with placeholder web content so the project deploys end to end out of the box.

An architecture diagram of the AWS Static Website template

Using this template#

To use this template to deploy a website of your own, make sure you’ve installed Pulumi and configured your AWS credentials, then create a new project using the template in the language of your choice:

Terminal window
mkdir my-site && cd my-site
pulumi new static-website-aws-typescript

Alternatively, you can create and configure a new project with this template (static-website-aws-typescript) in Pulumi Cloud.

Deploy with Pulumi

Follow the prompts to complete the new-project wizard. When it’s done, you’ll have a finished project that’s ready to deploy and configured with the most common settings. Feel free to inspect the code in index.ts for a closer look.

Deploying the project#

The template requires no additional configuration. Once the new project is created, you can deploy it immediately with pulumi up:

Terminal window
pulumi up

When the deployment completes, Pulumi exports the following stack output values:

cdnHostname

The provider-assigned hostname of the CloudFront CDN. Useful for creating CNAME records to associate custom domains.

cdnURL

The fully-qualified HTTPS URL of the CloudFront CDN.

Output values like these are useful in many ways, most commonly as inputs for other stacks or related cloud resources. The computed CDN URL, for example, can be used from the command line to open the newly deployed website in your favorite web browser:

Terminal window
open $(pulumi stack output cdnURL)

Customizing the project#

Projects created with the Static Website template expose the following configuration settings:

path

The path to the folder containing the files of the website. Defaults to www, which is the name (and relative path) of the folder included with the template.

indexDocument

The file to use for top-level pages. Defaults to index.html.

errorDocument

The file to use for error pages. Defaults to error.html.

All of these settings are optional and may be adjusted either by editing the stack configuration file directly (by default, Pulumi.dev.yaml) or by changing their values with pulumi config set:

Using your own web content#

If you already have a static website you’d like to deploy on AWS with Pulumi, you can do so either by replacing placeholder content in the www folder or by configuring the stack to point to another folder on your computer with the path setting:

Terminal window
pulumi config set path ../my-existing-website/build
pulumi up

Adjusting your cache settings#

By default, the generated program configures the CloudFront CDN to cache files for 600 seconds (10 minutes), which may or may not be the best fit for your project or stack. You can adjust these settings by changing the code in index.ts:

const cdn = new aws.cloudfront.Distribution("cdn", {
defaultCacheBehavior: {
defaultTtl: 600,
maxTtl: 600,
minTtl: 600,
defaultTtl: 3600,
maxTtl: 3600,
minTtl: 3600,

Alternatively, and perhaps better, you could make these settings configurable as well, which would allow them to vary between other stacks in your project.

Next steps#

Templated projects are meant to be customized, and every web project comes with its own unique set of needs. This section includes a few examples aimed at helping you to adapt your new project to address some of the more common ones.

Adding a custom domain#

Once your website is deployed on AWS, you may want to give it a domain of its own. For this, you have many options, and they generally fall into one of two categories: using Amazon Route 53, which is a good choice if your domain is already being managed on AWS, or using a third-party service like DNSimple or Google Cloud DNS. Both options are easily managed with Pulumi.

Using a Route 53 managed domain#

If the domain you’d like to use is already configured as a Route 53 hosted zone, you can easily add a subdomain for your website by making a few small changes to your program.

To do so, start by adding two new configuration settings — one for the domain, another for subdomain to use for the website:

Terminal window
pulumi config set domain example.com
pulumi config set subdomain www

Then, in your editor of choice, open index.ts and add the following lines to the configuration section at the top of the program to import the new settings and capture the domain as a reusable value:

const domain = config.require("domain");
const subdomain = config.require("subdomain");
const domainName = `${subdomain}.${domain}`;

Next, just above the aws.cloudfront.Distribution declaration, add these lines to provision and validate a new SSL/TLS certificate with AWS Certificate Manager (ACM):

// Look up your existing Route 53 hosted zone.
const zone = aws.route53.getZoneOutput({ name: domain });
// Provision a new ACM certificate.
const certificate = new aws.acm.Certificate("certificate",
{
domainName: domainName,
validationMethod: "DNS",
},
{
// ACM certificates must be created in the us-east-1 region.
provider: new aws.Provider("us-east-provider", {
region: "us-east-1",
}),
},
);
// Validate the ACM certificate with DNS.
const validationOption = certificate.domainValidationOptions[0];
const certificateValidation = new aws.route53.Record("certificate-validation", {
name: validationOption.resourceRecordName,
type: validationOption.resourceRecordType,
records: [ validationOption.resourceRecordValue ],
zoneId: zone.zoneId,
ttl: 60,
});

Extend the CloudFront configuration to handle requests for the new domain by adding an aliases argument to the CDN configuration and adjusting viewerCertificate to use the newly provisioned ACM certificate:

const cdn = new aws.cloudfront.Distribution("cdn", {
// ...
aliases: [
domainName,
],
viewerCertificate: {
cloudfrontDefaultCertificate: false,
acmCertificateArn: certificate.arn,
sslSupportMethod: "sni-only",
},
});

Below that, add a Route 53 A record to create a DNS record pointing to the CloudFront CDN:

// Create a DNS A record to point to the CDN.
const record = new aws.route53.Record(domainName, {
name: subdomain,
zoneId: zone.zoneId,
type: "A",
aliases: [
{
name: cdn.domainName,
zoneId: cdn.hostedZoneId,
evaluateTargetHealth: true,
}
],
}, { dependsOn: certificate });

And finally, complete the program by exporting the new URL as a Pulumi stack output:

export const domainURL = `https://${domainName}`;

Save your changes, then preview and deploy with another pulumi up:

Terminal window
pulumi up

In a few moments, you should be able to browse to your website using the custom domain:

Terminal window
open $(pulumi stack output domainURL)

Using a third-party DNS service#

If the domain you’d like to use is being managed by a third-party DNS service, you can generally use the exported cdnHostname to create a CNAME record with your DNS provider. You can obtain this value with pulumi stack output:

Terminal window
pulumi stack output cdnHostname

Pulumi supports many third-party DNS providers, all of which are available in the Pulumi Registry and accompanied by examples, including:

Integration details vary by provider, so we suggest exploring the Pulumi API documentation of your provider of choice to learn more. See the Registry for a complete list of supported providers.

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure as code platform for any cloud.