1. Answers
  2. Deploying AWS CloudFront Distribution

How do I deploy an AWS CloudFront distribution?

In this guide, we will deploy an AWS CloudFront distribution using Pulumi. AWS CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency and high transfer speeds.

We will define the necessary resources, including an S3 bucket to serve as the origin for the CloudFront distribution, and the CloudFront distribution itself.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an S3 bucket to serve as the origin for CloudFront
const bucket = new aws.s3.Bucket("my-bucket", {
    website: {
        indexDocument: "index.html",
    },
});

// Create an S3 Bucket Policy to allow CloudFront to access the bucket
const bucketPolicy = new aws.s3.BucketPolicy("bucketPolicy", {
    bucket: bucket.bucket,
    policy: bucket.bucket.apply(publicReadPolicyForBucket),
});

// Function to create a public read policy for the bucket
function publicReadPolicyForBucket(bucketName: string): string {
    return JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Effect: "Allow",
            Principal: "*",
            Action: [
                "s3:GetObject"
            ],
            Resource: [
                `arn:aws:s3:::${bucketName}/*`
            ],
        }],
    });
}

// Create a CloudFront distribution
const cdn = new aws.cloudfront.Distribution("my-cdn", {
    origins: [{
        originId: bucket.arn,
        domainName: bucket.websiteEndpoint,
        s3OriginConfig: {
            originAccessIdentity: "",
        },
    }],
    enabled: true,
    defaultRootObject: "index.html",
    defaultCacheBehavior: {
        targetOriginId: bucket.arn,
        viewerProtocolPolicy: "allow-all",
        allowedMethods: ["GET", "HEAD", "OPTIONS"],
        cachedMethods: ["GET", "HEAD"],
        forwardedValues: {
            queryString: false,
            cookies: {
                forward: "none",
            },
        },
        minTtl: 0,
        defaultTtl: 3600,
        maxTtl: 86400,
    },
    priceClass: "PriceClass_100",
    restrictions: {
        geoRestriction: {
            restrictionType: "none",
        },
    },
    viewerCertificate: {
        cloudfrontDefaultCertificate: true,
    },
});

// Export the bucket name and CloudFront distribution URL
export const bucketName = bucket.bucket;
export const cdnUrl = cdn.domainName;

Key Points:

  • S3 Bucket: We create an S3 bucket that will serve as the origin for the CloudFront distribution.
  • Bucket Policy: A bucket policy is created to allow public read access to the bucket.
  • CloudFront Distribution: The CloudFront distribution is set up with the S3 bucket as the origin, and configurations for caching and viewer protocols are defined.

Summary:

We successfully deployed an AWS CloudFront distribution using Pulumi. The S3 bucket serves as the origin, and the CloudFront distribution is configured to deliver content with low latency and high transfer speeds. The bucket name and CloudFront distribution URL are exported for further use.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up