Skip to content

Commit 5cd7622

Browse files
committed
sdk-v3: Migrate CreateFunction to v3 with test
GH-641
1 parent 92d07cc commit 5cd7622

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

lib/main.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const { createNamespace } = require('continuation-local-storage')
2828
* https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html
2929
*/
3030
const { LambdaClient, CreateFunctionCommand } = require('@aws-sdk/client-lambda')
31-
const lambdaClient = new LambdaClient()
3231

3332
const maxBufferSize = 50 * 1024 * 1024
3433

@@ -699,22 +698,7 @@ Emulate only the body of the API Gateway event.
699698
}
700699

701700
_uploadNew (lambda, params) {
702-
if (process.env.USE_AWS_SDK_V3) {
703-
console.log('DEBUG: Use AWS SDK V3: CreateFunctionCommand()')
704-
const command = new CreateFunctionCommand(params)
705-
return lambdaClient.send(command)
706-
}
707-
708-
return new Promise((resolve, reject) => {
709-
const request = lambda.createFunction(params, (err, data) => {
710-
if (err) return reject(err)
711-
resolve(data)
712-
})
713-
request.on('retry', (response) => {
714-
console.log(response.error.message)
715-
console.log('=> Retrying')
716-
})
717-
})
701+
return lambda.send(new CreateFunctionCommand(params))
718702
}
719703

720704
_readArchive (program) {
@@ -991,6 +975,7 @@ they may not work as expected in the Lambda environment.
991975
}
992976

993977
_deployToRegion (program, params, region, buffer) {
978+
// sdk v3 todo: Migration of aws.updateConfig.
994979
aws.updateConfig(program, region)
995980

996981
console.log('=> Reading event source file to memory')
@@ -1012,10 +997,13 @@ they may not work as expected in the Lambda environment.
1012997
}
1013998
console.log(params)
1014999

1000+
// Migrating to v3.
10151001
const lambda = new aws.sdk.Lambda({
10161002
region,
10171003
apiVersion: '2015-03-31'
10181004
})
1005+
const lambdaClient = new LambdaClient({ region })
1006+
10191007
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
10201008
const s3Events = new S3Events(aws.sdk, region)
10211009
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
@@ -1069,7 +1057,7 @@ they may not work as expected in the Lambda environment.
10691057
throw err
10701058
}
10711059
// Function does not exist
1072-
return this._uploadNew(lambda, params).then((results) => {
1060+
return this._uploadNew(lambdaClient, params).then((results) => {
10731061
console.log('=> Done uploading. Results follow: ')
10741062
console.log(results)
10751063

test/main.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ let assert
1111
import('chai').then(chai => {
1212
assert = chai.assert
1313
})
14+
const sinon = require('sinon')
15+
1416
const awsMock = require('aws-sdk-mock')
1517
awsMock.setSDK(path.resolve('node_modules/aws-sdk'))
1618

19+
// Migrating to v3.
20+
const { LambdaClient } = require('@aws-sdk/client-lambda')
21+
const lambdaClient = new LambdaClient({ region: 'us-east-1' })
22+
1723
const originalProgram = {
1824
packageManager: 'npm',
1925
environment: 'development',
@@ -22,7 +28,7 @@ const originalProgram = {
2228
sessionToken: 'token',
2329
functionName: '___node-lambda',
2430
handler: 'index.handler',
25-
role: 'some:arn:aws:iam::role',
31+
role: 'arn:aws:iam::999999999999:role/test',
2632
memorySize: 128,
2733
timeout: 3,
2834
description: '',
@@ -155,8 +161,15 @@ describe('lib/main', function () {
155161
return
156162
}
157163
execFileSync('npm', ['ci'], { cwd: sourceDirectoryForTest })
164+
165+
// for sdk v3
166+
const stub = sinon.stub(lambdaClient, 'send')
167+
stub.returns(lambdaMockSettings.createFunction)
168+
})
169+
after(() => {
170+
_awsRestore()
171+
sinon.restore() // for sdk v3
158172
})
159-
after(() => _awsRestore())
160173

161174
beforeEach(() => {
162175
program = Object.assign({}, originalProgram) // clone
@@ -1555,7 +1568,7 @@ describe('lib/main', function () {
15551568
describe('_uploadNew', () => {
15561569
it('simple test with mock', () => {
15571570
const params = lambda._params(program, null)
1558-
return lambda._uploadNew(awsLambda, params, (results) => {
1571+
return lambda._uploadNew(lambdaClient, params, (results) => {
15591572
assert.deepEqual(results, lambdaMockSettings.createFunction)
15601573
})
15611574
})

0 commit comments

Comments
 (0)