Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudfront Alternate Domain Names #93

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ stage: dev # (optional) serverless dashboard stage. defaul
inputs:
src: ./src # (optional) path to the source folder. default is a hello world html file.
domain: serverless.com # (optional) domain name. this could also be a subdomain.
alternateDomainNames: altDomainA.serverless.com,altDomainB.serverless.com # (optional) alternate cloudfront domain names, separated by a comma.
region: us-east-2 # (optional) aws region to deploy to. default is us-east-1.
bucketName: my-bucket # (optional) aws bucket name. default is an auto generated name.
indexDocument: index.html # (optional) index document for your website. default is index.html.
Expand Down
4 changes: 4 additions & 0 deletions src/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ class Website extends Component {
outputs.domain = `https://${config.domain}`
}

if (config.alternateDomainNames) {
outputs.alternateDomainNames = config.alternateDomainNames
}

return outputs
}

Expand Down
19 changes: 19 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ const getConfig = (inputs, state) => {
config.domainHostedZoneId = config.domain ? state.domainHostedZoneId : null
config.certificateArn = state.certificateArn

// for alternate cloudfront CNAME domains
config.alternateDomainNames = inputs.alternateDomainNames
? inputs.alternateDomainNames.split(',')
: null

// if user input example.com, make sure we also setup www.example.com
if (config.domain && config.domain === config.nakedDomain) {
config.domain = `www.${config.domain}`
Expand Down Expand Up @@ -598,6 +603,13 @@ const createCloudFrontDistribution = async (clients, config) => {
distributionConfig.Aliases.Quantity = 2
distributionConfig.Aliases.Items.push(config.nakedDomain)
}

if (Array.isArray(config.alternateDomainNames)) {
config.alternateDomainNames.forEach((domain) => {
distributionConfig.Aliases.Quantity += 1
distributionConfig.Aliases.Items.push(domain)
})
}
}

try {
Expand Down Expand Up @@ -667,6 +679,13 @@ const updateCloudFrontDistribution = async (clients, config) => {
params.DistributionConfig.Aliases.Quantity = 2
params.DistributionConfig.Aliases.Items.push(config.nakedDomain)
}

if (Array.isArray(config.alternateDomainNames)) {
config.alternateDomainNames.forEach((domain) => {
params.DistributionConfig.Aliases.Quantity += 1
params.DistributionConfig.Aliases.Items.push(domain)
})
}
}
// 6. then finally update!
const res = await clients.cf.updateDistribution(params).promise()
Expand Down