forked from serverless-components/aws-cloudfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.js
87 lines (68 loc) · 2 KB
/
serverless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const aws = require('aws-sdk')
const { equals } = require('ramda')
const { Component } = require('@serverless/core')
const {
createCloudFrontDistribution,
updateCloudFrontDistribution,
deleteCloudFrontDistribution
} = require('./lib')
/*
* Website
*/
class CloudFront extends Component {
/*
* Default
*/
async default(inputs = {}) {
this.context.status('Deploying')
inputs.region = inputs.region || 'us-east-1'
this.context.debug(
`Starting deployment of CloudFront distribution to the ${inputs.region} region.`
)
const cf = new aws.CloudFront({
credentials: this.context.credentials.aws,
region: inputs.region
})
const s3 = new aws.S3({
credentials: this.context.credentials.aws,
region: inputs.region
})
if (this.state.id) {
if (
!equals(this.state.origins, inputs.origins) ||
!equals(this.state.defaults, inputs.defaults)
) {
this.context.debug(`Updating CloudFront distribution of ID ${this.state.id}.`)
this.state = await updateCloudFrontDistribution(cf, s3, this.state.id, inputs)
}
} else {
this.context.debug(`Creating CloudFront distribution in the ${inputs.region} region.`)
this.state = await createCloudFrontDistribution(cf, s3, inputs)
}
this.state.region = inputs.region
this.state.origins = inputs.origins
this.state.defaults = inputs.defaults
await this.save()
this.context.debug(`CloudFront deployed successfully with URL: ${this.state.url}.`)
return this.state
}
/**
* Remove
*/
async remove() {
this.context.status(`Removing`)
if (!this.state.id) {
return
}
const cf = new aws.CloudFront({
credentials: this.context.credentials.aws,
region: this.state.region
})
await deleteCloudFrontDistribution(cf, this.state.id)
this.state = {}
await this.save()
this.context.debug(`CloudFront distribution was successfully removed.`)
return {}
}
}
module.exports = CloudFront