Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

fix(domain, nextjs-component): set aliases correctly with domainType of apex and domain starting with www #723

Merged
merged 2 commits into from
Oct 29, 2020
Merged
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
8 changes: 5 additions & 3 deletions packages/serverless-components/domain/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ class Domain extends Component {
subdomain,
certificate.CertificateArn,
inputs.domainType,
inputs.defaultCloudfrontInputs
inputs.defaultCloudfrontInputs,
this.context
);

this.context.debug(
Expand All @@ -146,7 +147,7 @@ class Domain extends Component {
domainHostedZoneId,
subdomain.url.replace("https://", ""),
inputs.domainType,
this
this.context
);
} else {
this.context.debug(
Expand Down Expand Up @@ -215,7 +216,8 @@ class Domain extends Component {
clients.route53,
domainState.domain,
domainHostedZoneId,
domainState.url.replace("https://", "")
domainState.url.replace("https://", ""),
this.context
);
} else {
this.context.debug(
Expand Down
47 changes: 36 additions & 11 deletions packages/serverless-components/domain/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const getDomainHostedZoneId = async (route53, domain, privateZone) => {
const params = {
DNSName: domain
};

const hostedZonesRes = await route53.listHostedZonesByName(params).promise();

const hostedZone = hostedZonesRes.HostedZones.find(
Expand Down Expand Up @@ -288,7 +288,7 @@ const configureDnsForCloudFrontDistribution = async (
domainHostedZoneId,
distributionUrl,
domainType,
that
context
) => {
const dnsRecordParams = {
HostedZoneId: domainHostedZoneId,
Expand Down Expand Up @@ -329,6 +329,11 @@ const configureDnsForCloudFrontDistribution = async (
});
}

context.debug(
"Updating Route53 DNS records with parameters:\n" +
JSON.stringify(dnsRecordParams, null, 2)
);

return route53.changeResourceRecordSets(dnsRecordParams).promise();
};

Expand All @@ -339,7 +344,8 @@ const removeCloudFrontDomainDnsRecords = async (
route53,
domain,
domainHostedZoneId,
distributionUrl
distributionUrl,
context
) => {
const params = {
HostedZoneId: domainHostedZoneId,
Expand All @@ -364,6 +370,10 @@ const removeCloudFrontDomainDnsRecords = async (
// TODO: should the CNAME records be removed too?

try {
context.debug(
"Updating Route53 with parameters:\n" + JSON.stringify(params, null, 2)
);

await route53.changeResourceRecordSets(params).promise();
} catch (e) {
if (e.code !== "InvalidChangeBatch") {
Expand All @@ -377,7 +387,8 @@ const addDomainToCloudfrontDistribution = async (
subdomain,
certificateArn,
domainType,
defaultCloudfrontInputs
defaultCloudfrontInputs,
context
) => {
// Update logic is a bit weird...
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#updateDistribution-property
Expand All @@ -398,21 +409,21 @@ const addDomainToCloudfrontDistribution = async (

// 5. then make our changes
params.DistributionConfig.Aliases.Items.push(subdomain.domain);
params.DistributionConfig.Aliases.Quantity += 1;
if (subdomain.domain.startsWith("www.")) {
if (domainType === "apex") {
params.DistributionConfig.Aliases.Items[-1] = `${subdomain.domain.replace(
"www.",
""
)}`;
params.DistributionConfig.Aliases.Items[
params.DistributionConfig.Aliases.Items.length - 1
] = `${subdomain.domain.replace("www.", "")}`;
} else if (domainType !== "www") {
params.DistributionConfig.Aliases.Items.push(
`${subdomain.domain.replace("www.", "")}`
);
params.DistributionConfig.Aliases.Quantity += 1;
}
}

params.DistributionConfig.Aliases.Quantity =
params.DistributionConfig.Aliases.Items.length;

params.DistributionConfig.ViewerCertificate = {
ACMCertificateArn: certificateArn,
SSLSupportMethod: "sni-only",
Expand All @@ -422,6 +433,11 @@ const addDomainToCloudfrontDistribution = async (
...defaultCloudfrontInputs.viewerCertificate
};

context.debug(
"Updating CloudFront distribution with parameters:\n" +
JSON.stringify(params, null, 2)
);

// 6. then finally update!
const res = await cf.updateDistribution(params).promise();

Expand All @@ -432,7 +448,11 @@ const addDomainToCloudfrontDistribution = async (
};
};

const removeDomainFromCloudFrontDistribution = async (cf, subdomain) => {
const removeDomainFromCloudFrontDistribution = async (
cf,
subdomain,
context
) => {
const params = await cf
.getDistributionConfig({ Id: subdomain.distributionId })
.promise();
Expand All @@ -453,6 +473,11 @@ const removeDomainFromCloudFrontDistribution = async (cf, subdomain) => {
MinimumProtocolVersion: DEFAULT_MINIMUM_PROTOCOL_VERSION
};

context.debug(
"Updating CloudFront distribution with parameters:\n" +
JSON.stringify(params, null, 2)
);

const res = await cf.updateDistribution(params).promise();

return {
Expand Down
2 changes: 2 additions & 0 deletions packages/serverless-components/nextjs-component/.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ declare module "@serverless/core" {
instance: {
debugMode: boolean;
};
status(status: string): void;
debug(message: string): void;
};
}
}