This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
serverless.js
191 lines (158 loc) · 5.33 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const path = require('path')
const util = require('util')
const { getPolicy } = require('./utils')
const types = require('./serverless.types.js')
const { Component, utils } = require('@serverless/core')
const exec = util.promisify(require('child_process').exec)
/**
* Component: Backend
*/
class Backend extends Component {
/**
* Types
*/
types() {
return types
}
/**
* Default
*/
async default(inputs = {}) {
this.context.status('Deploying')
inputs.bucketName = inputs.bucketName || 'backend-' + this.context.resourceId()
inputs.region = inputs.region || 'us-east-1'
// Default to current working directory
inputs.code = inputs.code || {}
inputs.code.root = inputs.code.root ? path.resolve(inputs.code.root) : process.cwd()
if (inputs.code.src) {
inputs.code.src = path.join(inputs.code.root, inputs.code.src)
}
let exists
if (inputs.code.src) {
exists = await utils.fileExists(path.join(inputs.code.src, 'index.js'))
} else {
exists = await utils.fileExists(path.join(inputs.code.root, 'index.js'))
}
if (!exists) {
throw Error(
`No index.js file found in the directory "${inputs.code.src || inputs.code.root}"`
)
}
// If a hook is provided, build the assets
if (inputs.code.hook) {
this.context.status('Building assets')
this.context.debug(`Running ${inputs.code.hook} in ${inputs.code.root}.`)
const options = { cwd: inputs.code.root }
try {
await exec(inputs.code.hook, options)
} catch (err) {
console.error(err.stderr) // eslint-disable-line
throw new Error(
`Failed building website via "${inputs.code.hook}" due to the following error: "${err.stderr}"`
)
}
}
// Check if the runtime is allowed
if (inputs.runtime && !['nodejs8.10', 'nodejs10.x'].includes(inputs.runtime)) {
throw new Error(
`The runtime can only be 'nodejs8.10' or 'nodejs10.x'. Runtime specified: ${inputs.runtime}`
)
}
const bucket = await this.load('@serverless/aws-s3')
const role = await this.load('@serverless/aws-iam-role')
const lambda = await this.load('@serverless/aws-lambda')
const apig = await this.load('@serverless/aws-api-gateway')
const domain = await this.load('@serverless/domain')
this.context.status('Deploying AWS S3 Bucket')
const bucketOutputs = await bucket({
name: inputs.bucketName,
region: inputs.region
})
this.context.status('Deploying AWS IAM Role')
const roleInputs = {
name: 'backend-' + this.context.resourceId(),
region: inputs.region,
service: 'lambda.amazonaws.com',
policy: getPolicy(inputs.permissions)
}
const roleOutputs = await role(roleInputs)
this.context.status('Deploying AWS Lambda & Uploading Code')
const lambdaInputs = {
name: 'backend-' + this.context.resourceId(),
description: inputs.description || 'A function for the Backend Component',
memory: inputs.memory || 896,
timeout: inputs.timeout || 10,
runtime: inputs.runtime || 'nodejs10.x',
code: inputs.code.src || inputs.code.root,
role: roleOutputs,
handler: 'shim.handler',
shims: [path.join(__dirname, 'shim.js')],
env: inputs.env || {},
bucket: bucketOutputs.name,
region: inputs.region
}
const lambdaOutputs = await lambda(lambdaInputs)
this.context.status('Deploying AWS API Gateway')
const apigInputs = {
name: 'backend-' + this.context.resourceId(),
stage: 'production',
description: 'An API for a Backend component',
region: inputs.region,
endpoints: [
{
path: '/',
method: 'any',
function: lambdaOutputs.arn
},
{
path: '/{proxy+}',
method: 'any',
function: lambdaOutputs.arn
}
]
}
const apigOutputs = await apig(apigInputs)
const outputs = { url: apigOutputs.url }
if (inputs.domain) {
const subdomain = inputs.domain.split('.')[0]
const secondLevelDomain = inputs.domain.replace(`${subdomain}.`, '')
const domainInputs = {
domain: secondLevelDomain,
subdomains: {},
region: inputs.region
}
domainInputs.subdomains[subdomain] = apigOutputs
const domainOutputs = await domain(domainInputs)
outputs.domain = domainOutputs.domains[0]
}
this.state.url = apigOutputs.url
this.state.domain = apigOutputs.domain
await this.save()
return outputs
}
/**
* Remove
*/
async remove() {
this.context.status('Removing')
const role = await this.load('@serverless/aws-iam-role')
const bucket = await this.load('@serverless/aws-s3')
const lambda = await this.load('@serverless/aws-lambda')
const apig = await this.load('@serverless/aws-api-gateway')
const domain = await this.load('@serverless/domain')
this.context.status('Removing AWS IAM Role')
await role.remove()
this.context.status('Removing AWS S3 Bucket')
await bucket.remove()
this.context.status('Removing AWS Lambda')
await lambda.remove()
this.context.status('Removing AWS API Gateway')
await apig.remove()
this.context.status('Removing Domain')
await domain.remove()
this.state = {}
await this.save()
return {}
}
}
module.exports = Backend