Skip to content

Commit

Permalink
> be me, > forget to add alias record.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndGasper committed Mar 13, 2019
1 parent 9619728 commit 816019b
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions aws/src/stacks/constructs/static-site/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { CloudFrontWebDistribution, SSLMethod, SecurityPolicyProtocol } from '@aws-cdk/aws-cloudfront';
import { Bucket } from '@aws-cdk/aws-s3';
import { Construct, Output, SSMParameterProvider } from '@aws-cdk/cdk';
import { AliasRecordTargetProps } from '@aws-cdk/aws-route53';
import { AliasRecordTargetProps, HostedZoneProvider, AliasRecord } from '@aws-cdk/aws-route53';

export interface StaticSiteProps {
domainName: string;
Expand Down Expand Up @@ -33,6 +33,15 @@ export class StaticSite extends Construct {
// Create cloudfront distribution
const cloudFrontDistribution = this.createCloudFrontDistribution(siteDomain, s3Bucket);
new Output(this, 'DistributionId', { value: cloudFrontDistribution.dnsName });
this.createAliasRecord(domainName, siteDomain, cloudFrontDistribution);
}
protected createAliasRecord(domainName: string, siteDomain: string, distribution: CloudFrontWebDistribution ) {
const zone = new HostedZoneProvider(this, { domainName: domainName }).findAndImport(this, 'Zone');
new AliasRecord(this, 'SiteAliasRecord', {
recordName: siteDomain,
target: distribution,
zone
});
}
protected createSiteBucket(siteSubDomain: string, domainName: string): Bucket {
const siteDomain = siteSubDomain + '.' + domainName;
Expand All @@ -47,7 +56,7 @@ export class StaticSite extends Construct {
bucket.grantPublicAccess();
return bucket;
}
protected createCloudFrontDistribution(siteDomain: string, siteBucket: Bucket): AliasRecordTargetProps {
protected createCloudFrontDistribution(siteDomain: string, siteBucket: Bucket): CloudFrontWebDistribution {
// CloudFront distribution that provides HTTPS
const ssmParameterProviderConfig = {
parameterName: `CertificateArn-${siteDomain}`
Expand All @@ -68,12 +77,14 @@ export class StaticSite extends Construct {
},
behaviors : [ {isDefaultBehavior: true}],
originHeaders: {
"ayy": "lmao"
"have": "you",
"heard": "the",
"tragedy": "of"
}
}
]
});
return distribution.asAliasRecordTarget();
return distribution;
}

}
Expand Down

0 comments on commit 816019b

Please sign in to comment.