Last active
June 21, 2022 20:33
-
-
Save cnunciato/73b2a24b23410e98fd3903d348c2fbad to your computer and use it in GitHub Desktop.
TypeScript component encapsulating a StaticWebsite
This file contains 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"; | |
interface StaticWebsiteArgs { | |
domainName: string; | |
defaultDocument?: string; | |
} | |
/** | |
* The StaticWebsite component provisions a cloud storage bucket | |
* and a content-delivery network for it. Exposes the name of | |
* the storage bucket and the CDN origin URL. | |
*/ | |
export class StaticWebsite extends pulumi.ComponentResource { | |
private bucket: aws.s3.Bucket; | |
private cdn: aws.cloudfront.Distribution; | |
public bucketName: pulumi.Output<string>; | |
public originURL: pulumi.Output<string>; | |
constructor(name: string, args: StaticWebsiteArgs, opts?: pulumi.ComponentResourceOptions) { | |
super("acmecorp:index:StaticWebsite", name, args, opts); | |
if (!args.defaultDocument) { | |
args.defaultDocument = "index.html"; | |
} | |
// Create a storage bucket. | |
this.bucket = new aws.s3.Bucket("website-bucket", { | |
bucket: args.domainName, | |
website: { | |
indexDocument: args.defaultDocument | |
} | |
}, { parent: this }); | |
// Create a CloudFront CDN. | |
this.cdn = new aws.cloudfront.Distribution("website-cdn", { | |
enabled: true, | |
origins: [ | |
{ | |
originId: this.bucket.arn, | |
domainName: this.bucket.bucketRegionalDomainName | |
} | |
], | |
defaultRootObject: "index.html", | |
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 | |
} | |
}, { parent: this }); | |
// 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}`; | |
// Register public properties as Pulumi outputs. | |
this.registerOutputs({ | |
bucketName: this.bucketName, | |
originURL: this.originURL | |
}); | |
} | |
} | |
// Consumers of your API can provide and use only the properties they care about. | |
export const { bucketName, originURL } = new StaticWebsite("my-website", { | |
domainName: "www.my-domain.com" | |
}); |
This file contains 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
$ pulumi up | |
... | |
Updating (dev) | |
View Live: https://app.pulumi.com/cnunciato/terraform-migrate-examples/dev/updates/18 | |
Type Name Status | |
+ pulumi:pulumi:Stack terraform-migrate-examples-dev created | |
+ └─ acmecorp:index:StaticWebsite my-website created | |
+ ├─ aws:s3:Bucket website-bucket created | |
+ └─ aws:cloudfront:Distribution website-cdn created | |
Outputs: | |
bucketName : "www.my-domain.com" | |
originURL : "https://d27w8wobfwa73r.cloudfront.net" | |
Resources: | |
+ 4 created | |
Duration: 3m20s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment