Skip to content

Commit 3f3dbd1

Browse files
committed
published 0.1.2
1 parent fbaa5cb commit 3f3dbd1

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed

package-lock.json

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@serverless/aws-lambda",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"main": "./serverless.js",
55
"publishConfig": {
66
"access": "public"
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"@serverless/aws-iam-role": "^0.1.0",
1616
"@serverless/aws-s3": "^0.1.0",
17+
"@serverless/aws-lambda-layer": "^0.1.0",
1718
"@serverless/components": "^0.1.0",
1819
"aws-sdk": "^2.424.0",
1920
"fs-extra": "^7.0.0",

serverless.js

+34-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path')
12
const aws = require('aws-sdk')
23
const { mergeDeepRight, pick } = require('ramda')
34
const { Component, hashFile } = require('@serverless/components')
@@ -10,7 +11,7 @@ const {
1011
pack
1112
} = require('./utils')
1213

13-
const outputMask = [
14+
const outputsList = [
1415
'name',
1516
'description',
1617
'memory',
@@ -22,16 +23,17 @@ const outputMask = [
2223
'runtime',
2324
'env',
2425
'role',
26+
'layer',
2527
'arn'
2628
]
2729

2830
const defaults = {
29-
name: 'serverless',
31+
name: 'serverless-layered',
3032
description: 'AWS Lambda Component',
3133
memory: 512,
3234
timeout: 10,
3335
code: process.cwd(),
34-
bucket: null,
36+
bucket: undefined,
3537
shims: [],
3638
handler: 'handler.hello',
3739
runtime: 'nodejs8.10',
@@ -54,33 +56,52 @@ class AwsLambda extends Component {
5456

5557
config.role = config.role || (await awsIamRole(config))
5658

57-
this.cli.status(`Packaging`)
59+
if (config.bucket && config.runtime === 'nodejs8.10') {
60+
const layer = await this.load('@serverless/aws-lambda-layer')
61+
62+
const layerInputs = {
63+
name: `${config.name}-dependencies`,
64+
description: `${config.name} Dependencies Layer`,
65+
code: path.join(config.code, 'node_modules'),
66+
runtimes: ['nodejs8.10'],
67+
prefix: 'nodejs/node_modules',
68+
bucket: config.bucket,
69+
region: config.region
70+
}
71+
72+
this.cli.status('Deploying Dependencies')
73+
const promises = [pack(config.code, config.shims, false), layer(layerInputs)]
74+
const res = await Promise.all(promises)
75+
config.zipPath = res[0]
76+
config.layer = res[1]
77+
} else {
78+
this.cli.status('Packaging')
79+
config.zipPath = await pack(config.code, config.shims)
80+
}
5881

59-
config.zipPath = await pack(config.code, config.shims)
6082
config.hash = await hashFile(config.zipPath)
6183

6284
let deploymentBucket
6385
if (config.bucket) {
6486
deploymentBucket = await this.load('@serverless/aws-s3')
65-
await deploymentBucket({ name: config.bucket })
6687
}
6788

6889
const prevLambda = await getLambda({ lambda, ...config })
6990

7091
if (!prevLambda) {
7192
if (config.bucket) {
7293
this.cli.status(`Uploading`)
73-
await deploymentBucket.upload({ file: config.zipPath })
94+
await deploymentBucket.upload({ name: config.bucket, file: config.zipPath })
7495
}
7596

7697
this.cli.status(`Creating`)
7798
config.arn = await createLambda({ lambda, ...config })
7899
} else {
79100
config.arn = prevLambda.arn
80101
if (configChanged(prevLambda, config)) {
81-
if (config.bucket) {
102+
if (config.bucket && prevLambda.hash !== config.hash) {
82103
this.cli.status(`Uploading`)
83-
await deploymentBucket.upload({ file: config.zipPath })
104+
await deploymentBucket.upload({ name: config.bucket, file: config.zipPath })
84105
}
85106

86107
this.cli.status(`Updating`)
@@ -97,14 +118,15 @@ class AwsLambda extends Component {
97118
this.state.arn = config.arn
98119
await this.save()
99120

100-
const outputs = pick(outputMask, config)
121+
const outputs = pick(outputsList, config)
101122
this.cli.outputs(outputs)
102123
return outputs
103124
}
104125

105126
async remove(inputs = {}) {
106127
const config = mergeDeepRight(defaults, inputs)
107128
config.name = inputs.name || this.state.name || defaults.name
129+
108130
const lambda = new aws.Lambda({
109131
region: config.region,
110132
credentials: this.context.credentials.aws
@@ -113,12 +135,12 @@ class AwsLambda extends Component {
113135
this.cli.status(`Removing`)
114136

115137
const awsIamRole = await this.load('@serverless/aws-iam-role')
116-
const deploymentBucket = await this.load('@serverless/aws-s3')
138+
const layer = await this.load('@serverless/aws-lambda-layer')
117139

118140
// there's no need to pass names as input
119141
// since it's saved in the child component state
120142
await awsIamRole.remove()
121-
await deploymentBucket.remove()
143+
await layer.remove()
122144

123145
await deleteLambda({ lambda, name: config.name })
124146

utils.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const createLambda = async ({
2121
description,
2222
zipPath,
2323
bucket,
24-
role
24+
role,
25+
layer
2526
}) => {
2627
const params = {
2728
FunctionName: name,
@@ -38,6 +39,10 @@ const createLambda = async ({
3839
}
3940
}
4041

42+
if (layer && layer.arn) {
43+
params.Layers = [layer.arn]
44+
}
45+
4146
if (bucket) {
4247
params.Code.S3Bucket = bucket
4348
params.Code.S3Key = path.basename(zipPath)
@@ -61,7 +66,8 @@ const updateLambda = async ({
6166
description,
6267
zipPath,
6368
bucket,
64-
role
69+
role,
70+
layer
6571
}) => {
6672
const functionCodeParams = {
6773
FunctionName: name,
@@ -88,6 +94,10 @@ const updateLambda = async ({
8894
}
8995
}
9096

97+
if (layer && layer.arn) {
98+
functionConfigParams.Layers = [layer.arn]
99+
}
100+
91101
await lambda.updateFunctionCode(functionCodeParams).promise()
92102
const res = await lambda.updateFunctionConfiguration(functionConfigParams).promise()
93103

@@ -161,18 +171,25 @@ const configChanged = (pervLambda, lambda) => {
161171
return not(equals(inputs, prevInputs))
162172
}
163173

164-
const pack = async (code, shims = []) => {
174+
const pack = async (code, shims = [], packDeps = true) => {
165175
if (isArchivePath(code)) {
166176
return path.resolve(code)
167177
}
178+
179+
let exclude = []
180+
181+
if (!packDeps) {
182+
exclude = ['node_modules/**']
183+
}
184+
168185
const outputFilePath = path.join(
169186
tmpdir(),
170187
`${Math.random()
171188
.toString(36)
172189
.substring(6)}.zip`
173190
)
174191

175-
return packDir(code, outputFilePath, shims)
192+
return packDir(code, outputFilePath, shims, exclude)
176193
}
177194

178195
module.exports = {

0 commit comments

Comments
 (0)