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

Commit 2188d26

Browse files
authored
fix(aws-cloudfront): do not override aliases when not specified in serverless.yml inputs (#704)
1 parent 0c138a2 commit 2188d26

File tree

7 files changed

+41
-30
lines changed

7 files changed

+41
-30
lines changed

packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/custom-url-origin.test.js.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ Object {
112112
exports[`Input origin as a custom url updates distribution 1`] = `
113113
Object {
114114
"DistributionConfig": Object {
115-
"Aliases": Object {
116-
"Items": Array [],
117-
"Quantity": 0,
118-
},
119115
"CacheBehaviors": Object {
120116
"Items": Array [],
121117
"Quantity": 0,

packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-custom-origin-config.js.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ Object {
101101
exports[`Input origin with custom origin config updates distribution 1`] = `
102102
Object {
103103
"DistributionConfig": Object {
104-
"Aliases": Object {
105-
"Items": Array [],
106-
"Quantity": 0,
107-
},
108104
"CacheBehaviors": Object {
109105
"Items": Array [],
110106
"Quantity": 0,

packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/origin-with-path-pattern.test.js.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ Object {
150150
exports[`Input origin with path pattern updates distribution 1`] = `
151151
Object {
152152
"DistributionConfig": Object {
153-
"Aliases": Object {
154-
"Items": Array [],
155-
"Quantity": 0,
156-
},
157153
"CacheBehaviors": Object {
158154
"Items": Array [
159155
Object {

packages/serverless-components/aws-cloudfront/__tests__/__snapshots__/s3-origin.test.js.snap

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,6 @@ Object {
267267
exports[`S3 origins When origin is an S3 bucket URL updates distribution 1`] = `
268268
Object {
269269
"DistributionConfig": Object {
270-
"Aliases": Object {
271-
"Items": Array [],
272-
"Quantity": 0,
273-
},
274270
"CacheBehaviors": Object {
275271
"Items": Array [],
276272
"Quantity": 0,
@@ -443,10 +439,6 @@ Object {
443439
exports[`S3 origins when origin is outside of us-east-1 updates distribution 1`] = `
444440
Object {
445441
"DistributionConfig": Object {
446-
"Aliases": Object {
447-
"Items": Array [],
448-
"Quantity": 0,
449-
},
450442
"CacheBehaviors": Object {
451443
"Items": Array [],
452444
"Quantity": 0,

packages/serverless-components/aws-cloudfront/__tests__/general-options.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ describe("General options propagation", () => {
130130
);
131131
});
132132

133+
it("update distribution with undefined aliases does not override existing aliases", async () => {
134+
// Create distribution
135+
await component.default({ enabled: true, origins });
136+
137+
// Update distribution
138+
await component.default({
139+
enabled: false,
140+
origins
141+
});
142+
143+
expect(mockUpdateDistribution).toBeCalledWith(
144+
expect.objectContaining({
145+
DistributionConfig: expect.not.objectContaining({
146+
Aliases: expect.anything()
147+
})
148+
})
149+
);
150+
});
151+
133152
it("create distribution with priceClass and update it", async () => {
134153
await component.default({
135154
priceClass: "PriceClass_All",

packages/serverless-components/aws-cloudfront/lib/index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
3030
inputs.comment !== null && inputs.comment !== undefined
3131
? inputs.comment
3232
: "",
33-
Aliases: {
34-
Quantity: inputs.aliases.length,
35-
Items: inputs.aliases
36-
},
33+
Aliases:
34+
// For initial creation if aliases undefined, then have default empty array of aliases
35+
// Although this is not required per https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_DistributionConfig.html
36+
inputs.aliases !== null && inputs.aliases !== undefined
37+
? {
38+
Quantity: inputs.aliases.length,
39+
Items: inputs.aliases
40+
}
41+
: {
42+
Quantity: 0,
43+
Items: []
44+
},
3745
Origins: {
3846
Quantity: 0,
3947
Items: []
@@ -114,15 +122,19 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {
114122
// 5. then make our changes
115123

116124
params.DistributionConfig.Enabled = inputs.enabled;
117-
(params.DistributionConfig.Comment =
125+
params.DistributionConfig.Comment =
118126
inputs.comment !== null && inputs.comment !== undefined
119127
? inputs.comment
120-
: ""),
121-
(params.DistributionConfig.Aliases = {
128+
: "";
129+
params.DistributionConfig.PriceClass = inputs.priceClass;
130+
131+
// When updating, don't override any existing aliases if not set in inputs
132+
if (inputs.aliases !== null && inputs.aliases !== undefined) {
133+
params.DistributionConfig.Aliases = {
122134
Items: inputs.aliases,
123135
Quantity: inputs.aliases.length
124-
});
125-
params.DistributionConfig.PriceClass = inputs.priceClass;
136+
};
137+
}
126138

127139
let s3CanonicalUserId;
128140
let originAccessIdentityId;

packages/serverless-components/aws-cloudfront/serverless.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CloudFront extends Component {
2525
inputs.comment === null || inputs.comment === undefined
2626
? ""
2727
: String(inputs.comment);
28-
inputs.aliases = inputs.aliases || [];
28+
inputs.aliases = inputs.aliases || undefined; // by default will be undefined, not empty array
2929
inputs.priceClass = [
3030
"PriceClass_All",
3131
"PriceClass_200",

0 commit comments

Comments
 (0)