Skip to content

Commit

Permalink
Merge pull request #92 from getlift/export-cloudfront-domain
Browse files Browse the repository at this point in the history
Add cname variable for static-website construct
  • Loading branch information
fredericbarthelet authored Sep 3, 2021
2 parents 1bac3f0 + 75de3ed commit b06714f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
29 changes: 29 additions & 0 deletions docs/static-website.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ serverless landing:upload

This command only takes seconds: it directly uploads files to S3 and clears the CloudFront cache.

## Variables

All static-website constructs expose the following variables:

- `cname`: the domain name of the resource, such as `d111111abcdef8.cloudfront.net`

This can be used to reference the bucket from Route53 configuration, for example:

```yaml
constructs:
landing:
type: static-website
path: public
resources:
Resources:
Route53Record:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: ZXXXXXXXXXXXXXXXXXXJ # Your HostedZoneId
Name: app.mydomain
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2 # Cloudfront Route53 HostedZoneId. This does not change.
DNSName: ${construct:landing.cname}
```

_How it works: the `${construct:landing.cname}` variable will automatically be replaced with a CloudFormation reference to the CloudFront Distribution._

## Configuration reference

### Path
Expand Down
15 changes: 11 additions & 4 deletions src/constructs/aws/StaticWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class StaticWebsite extends AwsConstruct {
},
};

private readonly distribution: Distribution;
private readonly bucketNameOutput: CfnOutput;
private readonly domainOutput: CfnOutput;
private readonly cnameOutput: CfnOutput;
Expand Down Expand Up @@ -108,7 +109,7 @@ export class StaticWebsite extends AwsConstruct {
configuration.certificate !== undefined
? acm.Certificate.fromCertificateArn(this, "Certificate", configuration.certificate)
: undefined;
const distribution = new Distribution(this, "CDN", {
this.distribution = new Distribution(this, "CDN", {
comment: `${provider.stackName} ${id} website CDN`,
// Send all page requests to index.html
defaultRootObject: "index.html",
Expand Down Expand Up @@ -141,7 +142,7 @@ export class StaticWebsite extends AwsConstruct {
description: "Name of the bucket that stores the static website.",
value: bucket.bucketName,
});
let websiteDomain: string = distribution.distributionDomainName;
let websiteDomain: string = this.distribution.distributionDomainName;
if (configuration.domain !== undefined) {
// In case of multiple domains, we take the first one
websiteDomain = typeof configuration.domain === "string" ? configuration.domain : configuration.domain[0];
Expand All @@ -152,14 +153,20 @@ export class StaticWebsite extends AwsConstruct {
});
this.cnameOutput = new CfnOutput(this, "CloudFrontCName", {
description: "CloudFront CNAME.",
value: distribution.distributionDomainName,
value: this.distribution.distributionDomainName,
});
this.distributionIdOutput = new CfnOutput(this, "DistributionId", {
description: "ID of the CloudFront distribution.",
value: distribution.distributionId,
value: this.distribution.distributionId,
});
}

variables(): Record<string, unknown> {
return {
cname: this.distribution.distributionDomainName,
};
}

outputs(): Record<string, () => Promise<string | undefined>> {
return {
url: () => this.getUrl(),
Expand Down

0 comments on commit b06714f

Please sign in to comment.