Skip to content

Commit c9aca4d

Browse files
committed
disable distribution on removal
1 parent adccb9e commit c9aca4d

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@serverless/aws-cloudfront",
3-
"version": "1.0.0",
3+
"version": "1.0.5",
44
"main": "./serverless.js",
55
"publishConfig": {
66
"access": "public"

serverless.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ class CloudFront extends Component {
3333
if (this.state.id) {
3434
if (!equals(this.state.origins, inputs.origins)) {
3535
this.context.debug(`Updating CloudFront distribution of ID ${this.state.id}.`)
36-
this.state = await updateCloudFrontDistribution(cf, this.state.id, inputs.origins)
36+
this.state = await updateCloudFrontDistribution(cf, this.state.id, inputs)
3737
}
3838
} else {
3939
this.context.debug(`Creating CloudFront distribution in the ${inputs.region} region.`)
40-
this.state = await createCloudFrontDistribution(cf, inputs.origins)
40+
this.state = await createCloudFrontDistribution(cf, inputs)
4141
}
4242

4343
this.state.region = inputs.region
4444
this.state.origins = inputs.origins
4545
await this.save()
4646

47-
this.context.debug(`CloudFront deployed successfully with URL: https://${this.state.url}.`)
47+
this.context.debug(`CloudFront deployed successfully with URL: ${this.state.url}.`)
4848

4949
return this.state
5050
}

utils.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const getOriginConfig = (origin) => {
1212
}
1313

1414
if (origin.includes('s3')) {
15-
const bucketName = origin.replace('http://', '').split('.')[0]
15+
const bucketName = origin.replace('https://', '').split('.')[0]
1616
originConfig.Id = bucketName
1717
originConfig.DomainName = `${bucketName}.s3.amazonaws.com`
1818
}
@@ -97,7 +97,7 @@ const createCloudFrontDistribution = async (cf, inputs) => {
9797
return {
9898
id: res.Distribution.Id,
9999
arn: res.Distribution.ARN,
100-
url: res.Distribution.DomainName
100+
url: `https://${res.Distribution.DomainName}`
101101
}
102102
}
103103

@@ -133,7 +133,27 @@ const updateCloudFrontDistribution = async (cf, distributionId, inputs) => {
133133
return {
134134
id: res.Distribution.Id,
135135
arn: res.Distribution.ARN,
136-
url: res.Distribution.DomainName
136+
url: `https://${res.Distribution.DomainName}`
137+
}
138+
}
139+
140+
const disableCloudFrontDistribution = async (cf, distributionId) => {
141+
const params = await cf.getDistributionConfig({ Id: distributionId }).promise()
142+
143+
params.IfMatch = params.ETag
144+
145+
delete params.ETag
146+
147+
params.Id = distributionId
148+
149+
params.DistributionConfig.Enabled = false
150+
151+
const res = await cf.updateDistribution(params).promise()
152+
153+
return {
154+
id: res.Distribution.Id,
155+
arn: res.Distribution.ARN,
156+
url: `https://${res.Distribution.DomainName}`
137157
}
138158
}
139159

@@ -144,7 +164,9 @@ const deleteCloudFrontDistribution = async (cf, distributionId) => {
144164
const params = { Id: distributionId, IfMatch: res.ETag }
145165
await cf.deleteDistribution(params).promise()
146166
} catch (e) {
147-
if (e.code !== '') {
167+
if (e.code === 'DistributionNotDisabled') {
168+
await disableCloudFrontDistribution(cf, distributionId)
169+
} else {
148170
throw e
149171
}
150172
}

0 commit comments

Comments
 (0)