Last active
June 21, 2022 20:33
-
-
Save cnunciato/23047e80103081a50236236609c5e513 to your computer and use it in GitHub Desktop.
TypeScript Class encapsulating a StaticWebsite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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