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

Commit ea180e6

Browse files
authored
feat(aws-cloudfront): allow setting CloudFront distribution aliases (#654)
1 parent 6bb1443 commit ea180e6

File tree

9 files changed

+65
-2
lines changed

9 files changed

+65
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ myNextApplication:
183183
minTTL: 10
184184
maxTTL: 10
185185
defaultTTL: 10
186+
aliases: ["foo.example.com", "bar.example.com"]
186187
priceClass: "PriceClass_100"
187188
# You can add custom error responses
188189
errorPages:

packages/serverless-components/aws-cloudfront/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ distribution:
5555
region: us-east-1
5656
enabled: true # optional
5757
comment: 'My distribution' # optional
58+
aliases: ['foo.example.com', 'bar.example.com']
5859
priceClass: 'PriceClass_All' # optional
5960
errorPages: # optional
6061
- code: 503

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ 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+
},
115119
"CacheBehaviors": Object {
116120
"Items": Array [],
117121
"Quantity": 0,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ 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+
},
153157
"CacheBehaviors": Object {
154158
"Items": Array [
155159
Object {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ 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+
},
270274
"CacheBehaviors": Object {
271275
"Items": Array [],
272276
"Quantity": 0,
@@ -439,6 +443,10 @@ Object {
439443
exports[`S3 origins when origin is outside of us-east-1 updates distribution 1`] = `
440444
Object {
441445
"DistributionConfig": Object {
446+
"Aliases": Object {
447+
"Items": Array [],
448+
"Quantity": 0,
449+
},
442450
"CacheBehaviors": Object {
443451
"Items": Array [],
444452
"Quantity": 0,

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,40 @@ describe("General options propagation", () => {
9696
);
9797
});
9898

99+
it("create distribution with aliases and update it", async () => {
100+
await component.default({
101+
aliases: ["foo.example.com"],
102+
origins
103+
});
104+
105+
expect(mockCreateDistribution).toBeCalledWith(
106+
expect.objectContaining({
107+
DistributionConfig: expect.objectContaining({
108+
Aliases: {
109+
Items: ["foo.example.com"],
110+
Quantity: 1
111+
}
112+
})
113+
})
114+
);
115+
116+
await component.default({
117+
aliases: ["bar.example.com"],
118+
origins
119+
});
120+
121+
expect(mockUpdateDistribution).toBeCalledWith(
122+
expect.objectContaining({
123+
DistributionConfig: expect.objectContaining({
124+
Aliases: {
125+
Items: ["bar.example.com"],
126+
Quantity: 1
127+
}
128+
})
129+
})
130+
);
131+
});
132+
99133
it("create distribution with priceClass and update it", async () => {
100134
await component.default({
101135
priceClass: "PriceClass_All",

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
2828
CallerReference: String(Date.now()),
2929
Comment: inputs.comment,
3030
Aliases: {
31-
Quantity: 0,
32-
Items: []
31+
Quantity: inputs.aliases.length,
32+
Items: inputs.aliases
3333
},
3434
Origins: {
3535
Quantity: 0,
@@ -112,6 +112,10 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {
112112

113113
params.DistributionConfig.Enabled = inputs.enabled;
114114
params.DistributionConfig.Comment = inputs.comment;
115+
params.DistributionConfig.Aliases = {
116+
Items: inputs.aliases,
117+
Quantity: inputs.aliases.length
118+
};
115119
params.DistributionConfig.PriceClass = inputs.priceClass;
116120

117121
let s3CanonicalUserId;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class CloudFront extends Component {
2525
inputs.comment === null || inputs.comment === undefined
2626
? ""
2727
: String(inputs.comment);
28+
inputs.aliases = inputs.aliases || [];
2829
inputs.priceClass = [
2930
"PriceClass_All",
3031
"PriceClass_200",
@@ -56,6 +57,7 @@ class CloudFront extends Component {
5657
!equals(this.state.defaults, inputs.defaults) ||
5758
!equals(this.state.enabled, inputs.enabled) ||
5859
!equals(this.state.comment, inputs.comment) ||
60+
!equals(this.state.aliases, inputs.aliases) ||
5961
!equals(this.state.priceClass, inputs.priceClass) ||
6062
!equals(this.state.errorPages, inputs.errorPages)
6163
) {
@@ -79,6 +81,7 @@ class CloudFront extends Component {
7981
this.state.region = inputs.region;
8082
this.state.enabled = inputs.enabled;
8183
this.state.comment = inputs.comment;
84+
this.state.aliases = inputs.aliases;
8285
this.state.priceClass = inputs.priceClass;
8386
this.state.origins = inputs.origins;
8487
this.state.errorPages = inputs.errorPages;

packages/serverless-components/nextjs-component/src/component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ class NextjsComponent extends Component {
206206
const {
207207
defaults: cloudFrontDefaultsInputs,
208208
origins: cloudFrontOriginsInputs,
209+
aliases: cloudFrontAliasesInputs,
209210
priceClass: cloudFrontPriceClassInputs,
210211
errorPages: cloudFrontErrorPagesInputs,
211212
distributionId: cloudFrontDistributionId = null,
@@ -535,6 +536,9 @@ class NextjsComponent extends Component {
535536
compress: true
536537
},
537538
origins: cloudFrontOrigins,
539+
...(cloudFrontAliasesInputs && {
540+
aliases: cloudFrontAliasesInputs
541+
}),
538542
...(cloudFrontPriceClassInputs && {
539543
priceClass: cloudFrontPriceClassInputs
540544
}),

0 commit comments

Comments
 (0)