|
1 |
| -const aws = require("aws-sdk"); |
2 |
| -const AwsSdkLambda = aws.Lambda; |
3 |
| -const { mergeDeepRight, pick } = require("ramda"); |
4 |
| -const { Component, utils } = require("@serverless/core"); |
5 |
| -const { |
6 |
| - createLambda, |
7 |
| - updateLambdaCode, |
8 |
| - updateLambdaConfig, |
9 |
| - getLambda, |
10 |
| - deleteLambda, |
11 |
| - configChanged, |
12 |
| - pack |
13 |
| -} = require("./utils"); |
| 1 | +const component = require("./dist/component.js"); |
14 | 2 |
|
15 |
| -const outputsList = [ |
16 |
| - "name", |
17 |
| - "hash", |
18 |
| - "description", |
19 |
| - "memory", |
20 |
| - "timeout", |
21 |
| - "code", |
22 |
| - "bucket", |
23 |
| - "shims", |
24 |
| - "handler", |
25 |
| - "runtime", |
26 |
| - "env", |
27 |
| - "role", |
28 |
| - "arn", |
29 |
| - "region", |
30 |
| - "tags" |
31 |
| -]; |
32 |
| - |
33 |
| -const defaults = { |
34 |
| - description: "AWS Lambda Component", |
35 |
| - memory: 512, |
36 |
| - timeout: 10, |
37 |
| - code: process.cwd(), |
38 |
| - bucket: undefined, |
39 |
| - shims: [], |
40 |
| - handler: "handler.hello", |
41 |
| - runtime: "nodejs10.x", |
42 |
| - env: {}, |
43 |
| - region: "us-east-1", |
44 |
| - tags: undefined |
45 |
| -}; |
46 |
| - |
47 |
| -class AwsLambda extends Component { |
48 |
| - async default(inputs = {}) { |
49 |
| - this.context.status(`Deploying`); |
50 |
| - |
51 |
| - const config = mergeDeepRight(defaults, inputs); |
52 |
| - |
53 |
| - config.name = inputs.name || this.state.name || this.context.resourceId(); |
54 |
| - config.tags = inputs.tags || this.state.tags; |
55 |
| - |
56 |
| - this.context.debug( |
57 |
| - `Starting deployment of lambda ${config.name} to the ${config.region} region.` |
58 |
| - ); |
59 |
| - |
60 |
| - const lambda = new AwsSdkLambda({ |
61 |
| - region: config.region, |
62 |
| - credentials: this.context.credentials.aws |
63 |
| - }); |
64 |
| - |
65 |
| - if (!config.role || !config.role.arn) { |
66 |
| - const awsIamRole = await this.load("@serverless/aws-iam-role"); |
67 |
| - const outputsAwsIamRole = await awsIamRole(config.role); |
68 |
| - config.role = { arn: outputsAwsIamRole.arn }; |
69 |
| - } |
70 |
| - |
71 |
| - this.context.status("Packaging"); |
72 |
| - this.context.debug(`Packaging lambda code from ${config.code}.`); |
73 |
| - config.zipPath = await pack(config.code, config.shims); |
74 |
| - |
75 |
| - config.hash = await utils.hashFile(config.zipPath); |
76 |
| - |
77 |
| - const prevLambda = await getLambda({ lambda, ...config }); |
78 |
| - |
79 |
| - if (!prevLambda) { |
80 |
| - this.context.status(`Creating`); |
81 |
| - this.context.debug( |
82 |
| - `Creating lambda ${config.name} in the ${config.region} region.` |
83 |
| - ); |
84 |
| - |
85 |
| - const createResult = await createLambda({ lambda, ...config }); |
86 |
| - config.arn = createResult.arn; |
87 |
| - config.hash = createResult.hash; |
88 |
| - } else { |
89 |
| - config.arn = prevLambda.arn; |
90 |
| - |
91 |
| - if (configChanged(prevLambda, config)) { |
92 |
| - if (prevLambda.hash !== config.hash) { |
93 |
| - this.context.status(`Uploading code`); |
94 |
| - this.context.debug(`Uploading ${config.name} lambda code.`); |
95 |
| - await updateLambdaCode({ lambda, ...config }); |
96 |
| - } |
97 |
| - |
98 |
| - this.context.status(`Updating`); |
99 |
| - this.context.debug(`Updating ${config.name} lambda config.`); |
100 |
| - |
101 |
| - const updateResult = await updateLambdaConfig({ lambda, ...config }); |
102 |
| - config.hash = updateResult.hash; |
103 |
| - } |
104 |
| - } |
105 |
| - |
106 |
| - // todo we probably don't need this logic now that we auto generate names |
107 |
| - if (this.state.name && this.state.name !== config.name) { |
108 |
| - this.context.status(`Replacing`); |
109 |
| - await deleteLambda({ lambda, name: this.state.name }); |
110 |
| - } |
111 |
| - |
112 |
| - this.context.debug( |
113 |
| - `Successfully deployed lambda ${config.name} in the ${config.region} region.` |
114 |
| - ); |
115 |
| - |
116 |
| - const outputs = pick(outputsList, config); |
117 |
| - |
118 |
| - this.state = outputs; |
119 |
| - await this.save(); |
120 |
| - |
121 |
| - return outputs; |
122 |
| - } |
123 |
| - |
124 |
| - async publishVersion() { |
125 |
| - const { name, region, hash } = this.state; |
126 |
| - |
127 |
| - const lambda = new AwsSdkLambda({ |
128 |
| - region, |
129 |
| - credentials: this.context.credentials.aws |
130 |
| - }); |
131 |
| - |
132 |
| - const { Version } = await lambda |
133 |
| - .publishVersion({ |
134 |
| - FunctionName: name, |
135 |
| - CodeSha256: hash |
136 |
| - }) |
137 |
| - .promise(); |
138 |
| - |
139 |
| - return { version: Version }; |
140 |
| - } |
141 |
| - |
142 |
| - async remove() { |
143 |
| - this.context.status(`Removing`); |
144 |
| - |
145 |
| - if (!this.state.name) { |
146 |
| - this.context.debug(`Aborting removal. Function name not found in state.`); |
147 |
| - return; |
148 |
| - } |
149 |
| - |
150 |
| - const { name, region } = this.state; |
151 |
| - |
152 |
| - const lambda = new AwsSdkLambda({ |
153 |
| - region, |
154 |
| - credentials: this.context.credentials.aws |
155 |
| - }); |
156 |
| - |
157 |
| - const awsIamRole = await this.load("@serverless/aws-iam-role"); |
158 |
| - |
159 |
| - await awsIamRole.remove(); |
160 |
| - |
161 |
| - this.context.debug(`Removing lambda ${name} from the ${region} region.`); |
162 |
| - await deleteLambda({ lambda, name }); |
163 |
| - this.context.debug( |
164 |
| - `Successfully removed lambda ${name} from the ${region} region.` |
165 |
| - ); |
166 |
| - |
167 |
| - const outputs = pick(outputsList, this.state); |
168 |
| - |
169 |
| - this.state = {}; |
170 |
| - await this.save(); |
171 |
| - |
172 |
| - return outputs; |
173 |
| - } |
174 |
| -} |
175 |
| - |
176 |
| -module.exports = AwsLambda; |
| 3 | +module.exports = component.default; |
0 commit comments