Skip to content

fixed removal of cloudfront distribution #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 13 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {
}
}

const disableCloudFrontDistribution = async (cf, distributionId) => {
const disableCloudFrontDistribution = async (cf, distributionId, debug) => {
const params = await cf.getDistributionConfig({ Id: distributionId }).promise()

params.IfMatch = params.ETag
Expand All @@ -143,27 +143,22 @@ const disableCloudFrontDistribution = async (cf, distributionId) => {
params.DistributionConfig.Enabled = false

const res = await cf.updateDistribution(params).promise()
debug(`Waiting for CloudFront distribution changes to be deployed.`)
await cf.waitFor('distributionDeployed', { Id: distributionId }).promise()

return {
id: res.Distribution.Id,
arn: res.Distribution.ARN,
url: `https://${res.Distribution.DomainName}`
}
return res
}

const deleteCloudFrontDistribution = async (cf, distributionId) => {
try {
const res = await cf.getDistributionConfig({ Id: distributionId }).promise()

const params = { Id: distributionId, IfMatch: res.ETag }
await cf.deleteDistribution(params).promise()
} catch (e) {
if (e.code === 'DistributionNotDisabled') {
await disableCloudFrontDistribution(cf, distributionId)
} else {
throw e
}
const deleteCloudFrontDistribution = async (cf, distributionId, debug) => {
let res = await cf.getDistributionConfig({ Id: distributionId }).promise()

if (res.DistributionConfig.Enabled) {
debug(`Disabling CloudFront distribution of ID ${distributionId} before removal.`)
res = await disableCloudFrontDistribution(cf, distributionId, debug)
}

const params = { Id: distributionId, IfMatch: res.ETag }
await cf.deleteDistribution(params).promise()
}

module.exports = {
Expand Down
5 changes: 4 additions & 1 deletion serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ class CloudFront extends Component {
region: this.state.region
})

await deleteCloudFrontDistribution(cf, this.state.id)
this.context.debug(
`Removing CloudFront distribution of ID ${this.state.id}. It could take a while.`
)
await deleteCloudFrontDistribution(cf, this.state.id, this.context.debug)

this.state = {}
await this.save()
Expand Down