Skip to content

Instantly share code, notes, and snippets.

@cnunciato
Last active June 21, 2022 20:33
Show Gist options
  • Select an option

  • Save cnunciato/23047e80103081a50236236609c5e513 to your computer and use it in GitHub Desktop.

Select an option

Save cnunciato/23047e80103081a50236236609c5e513 to your computer and use it in GitHub Desktop.
TypeScript Class encapsulating a StaticWebsite
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
/**
* The StaticWebsite class provisions a cloud storage bucket
* and a content-delivery network for it, exposing the name of
* the storage bucket and the CDN URL for later use.
*/
export class StaticWebsite {
private bucket: aws.s3.Bucket;
private cdn: aws.cloudfront.Distribution;
public bucketName: pulumi.Output<string>;
public originURL: pulumi.Output<string>;
constructor(domainName: string, defaultDocument: string = "index.html") {
// Create a storage bucket.
this.bucket = new aws.s3.Bucket("website-bucket", {
bucket: domainName,
website: {
indexDocument: defaultDocument
}
});
// Create a CloudFront CDN.
this.cdn = new aws.cloudfront.Distribution("website-cdn", {
enabled: true,
origins: [
{
originId: this.bucket.arn,
domainName: this.bucket.bucketRegionalDomainName
}
],
defaultRootObject: defaultDocument,
defaultCacheBehavior: {
targetOriginId: this.bucket.arn,
allowedMethods: ["HEAD", "GET"],
cachedMethods: ["HEAD", "GET"],
viewerProtocolPolicy: "redirect-to-https",
forwardedValues: {
cookies: { forward: "none" },
queryString: false
}
},
restrictions: {
geoRestriction: {
restrictionType: "none"
}
},
viewerCertificate: {
cloudfrontDefaultCertificate: true
}
});
// Expose the bucket name and origin URL so consumers can use them.
this.bucketName = this.bucket.bucket;
this.originURL = pulumi.interpolate`https://${this.cdn.domainName}`;
}
}
// Consumers of your API can provide and use only the properties they care about.
const { bucketName, originURL } = new StaticWebsite("www.my-domain.com");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment