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

feat(aws-cloudfront): allow setting CloudFront distribution aliases #654

Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ myNextApplication:
minTTL: 10
maxTTL: 10
defaultTTL: 10
aliases: ["foo.example.com", "bar.example.com"]
priceClass: "PriceClass_100"
# You can add custom error responses
errorPages:
Expand Down
1 change: 1 addition & 0 deletions packages/serverless-components/aws-cloudfront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ distribution:
region: us-east-1
enabled: true # optional
comment: 'My distribution' # optional
aliases: ['foo.example.com', 'bar.example.com']
priceClass: 'PriceClass_All' # optional
errorPages: # optional
- code: 503
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ Object {
exports[`Input origin as a custom url updates distribution 1`] = `
Object {
"DistributionConfig": Object {
"Aliases": Object {
"Items": Array [],
"Quantity": 0,
},
"CacheBehaviors": Object {
"Items": Array [],
"Quantity": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ Object {
exports[`Input origin with path pattern updates distribution 1`] = `
Object {
"DistributionConfig": Object {
"Aliases": Object {
"Items": Array [],
"Quantity": 0,
},
"CacheBehaviors": Object {
"Items": Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ Object {
exports[`S3 origins When origin is an S3 bucket URL updates distribution 1`] = `
Object {
"DistributionConfig": Object {
"Aliases": Object {
"Items": Array [],
"Quantity": 0,
},
"CacheBehaviors": Object {
"Items": Array [],
"Quantity": 0,
Expand Down Expand Up @@ -439,6 +443,10 @@ Object {
exports[`S3 origins when origin is outside of us-east-1 updates distribution 1`] = `
Object {
"DistributionConfig": Object {
"Aliases": Object {
"Items": Array [],
"Quantity": 0,
},
"CacheBehaviors": Object {
"Items": Array [],
"Quantity": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,40 @@ describe("General options propagation", () => {
);
});

it("create distribution with aliases and update it", async () => {
await component.default({
aliases: ["foo.example.com"],
origins
});

expect(mockCreateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
Aliases: {
Items: ["foo.example.com"],
Quantity: 1
}
})
})
);

await component.default({
aliases: ["bar.example.com"],
origins
});

expect(mockUpdateDistribution).toBeCalledWith(
expect.objectContaining({
DistributionConfig: expect.objectContaining({
Aliases: {
Items: ["bar.example.com"],
Quantity: 1
}
})
})
);
});

it("create distribution with priceClass and update it", async () => {
await component.default({
priceClass: "PriceClass_All",
Expand Down
8 changes: 6 additions & 2 deletions packages/serverless-components/aws-cloudfront/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
CallerReference: String(Date.now()),
Comment: inputs.comment,
Aliases: {
Quantity: 0,
Items: []
Quantity: inputs.aliases.length,
Items: inputs.aliases
},
Origins: {
Quantity: 0,
Expand Down Expand Up @@ -112,6 +112,10 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {

params.DistributionConfig.Enabled = inputs.enabled;
params.DistributionConfig.Comment = inputs.comment;
params.DistributionConfig.Aliases = {
Items: inputs.aliases,
Quantity: inputs.aliases.length
};
params.DistributionConfig.PriceClass = inputs.priceClass;

let s3CanonicalUserId;
Expand Down
3 changes: 3 additions & 0 deletions packages/serverless-components/aws-cloudfront/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CloudFront extends Component {
inputs.comment === null || inputs.comment === undefined
? ""
: String(inputs.comment);
inputs.aliases = inputs.aliases || [];
inputs.priceClass = [
"PriceClass_All",
"PriceClass_200",
Expand Down Expand Up @@ -56,6 +57,7 @@ class CloudFront extends Component {
!equals(this.state.defaults, inputs.defaults) ||
!equals(this.state.enabled, inputs.enabled) ||
!equals(this.state.comment, inputs.comment) ||
!equals(this.state.aliases, inputs.aliases) ||
!equals(this.state.priceClass, inputs.priceClass) ||
!equals(this.state.errorPages, inputs.errorPages)
) {
Expand All @@ -79,6 +81,7 @@ class CloudFront extends Component {
this.state.region = inputs.region;
this.state.enabled = inputs.enabled;
this.state.comment = inputs.comment;
this.state.aliases = inputs.aliases;
this.state.priceClass = inputs.priceClass;
this.state.origins = inputs.origins;
this.state.errorPages = inputs.errorPages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class NextjsComponent extends Component {
const {
defaults: cloudFrontDefaultsInputs,
origins: cloudFrontOriginsInputs,
aliases: cloudFrontAliasesInputs,
priceClass: cloudFrontPriceClassInputs,
errorPages: cloudFrontErrorPagesInputs,
distributionId: cloudFrontDistributionId = null,
Expand Down Expand Up @@ -535,6 +536,9 @@ class NextjsComponent extends Component {
compress: true
},
origins: cloudFrontOrigins,
...(cloudFrontAliasesInputs && {
aliases: cloudFrontAliasesInputs
}),
...(cloudFrontPriceClassInputs && {
priceClass: cloudFrontPriceClassInputs
}),
Expand Down