forked from Droplr/serverless-api-cloudfront
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- automatically create ALIAS record(s) for distribution - automatically request and certificate using route53
- Loading branch information
Showing
3 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Route53AliasTemplate: | ||
Type: AWS::Route53::RecordSetGroup | ||
Properties: | ||
HostedZoneId: hostedZoneId | ||
RecordSets: | ||
- Name: domain.tld | ||
Type: A | ||
AliasTarget: | ||
HostedZoneId: Z2FDTNDATAQYW2 # The static CloudFront Hosted Zone ID | ||
DNSName: # !GetAtt [ApiDistribution, DomainName] | ||
EvaluateTargetHealth: false | ||
# ... | ||
|
||
CertTemplate: | ||
Type: AWS::CertificateManager::Certificate | ||
Properties: | ||
DomainName: domain.tld | ||
DomainValidationOptions: | ||
# - DomainName: domain.tld | ||
# HostedZoneId: hostedZoneId | ||
# - DomainName: example.com | ||
# ValidationDomain: example.com | ||
# ... | ||
ValidationMethod: DNS | ||
# SubjectAlternativeNames: |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// get hosted zones, group domains by HZ.Id using .reduce and extract values | ||
const groupDomainsByHostedZone = async (serverless, domains) => { | ||
const r53response = await serverless.getProvider('aws').request('Route53', 'listHostedZones', {}); | ||
// we only want raw Ids | ||
const hostedZones = r53response.HostedZones.map(hz => ({...hz, Id: hz.Id.split("/").reverse()[0]})); | ||
|
||
const hostedZoneMap = domains.reduce((accumulator, domain) => { | ||
const hostedZone = hostedZones.find(hostedZone => `${domain}.`.includes(hostedZone.Name)); | ||
if (accumulator[hostedZone?.Id]) accumulator[hostedZone?.Id].domains.push(domain); | ||
else accumulator[hostedZone?.Id] = {...hostedZone, domains: [domain]}; | ||
return accumulator; | ||
}, {}); | ||
return Object.values(hostedZoneMap); | ||
} | ||
|
||
module.exports = { | ||
groupDomainsByHostedZone | ||
}; |