Skip to content

Commit 5ee387a

Browse files
committed
sdk-v3: Migrate CreateFunction to v3 with test (motdotla#759)
motdotlaGH-641
1 parent fa44231 commit 5ee387a

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

lib/main.js

Lines changed: 4 additions & 17 deletions
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
async _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')
@@ -1004,10 +989,12 @@ they may not work as expected in the Lambda environment.
1004989
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
1005990
}
1006991
console.log(params)
992+
// Migrating to v3.
1007993
const lambda = new aws.sdk.Lambda({
1008994
region,
1009995
apiVersion: '2015-03-31'
1010996
})
997+
const lambdaClient = new LambdaClient({ region })
1011998
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
1012999
const s3Events = new S3Events(aws.sdk, region)
10131000
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)

test/main.js

Lines changed: 16 additions & 3 deletions
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)