Skip to content

Commit

Permalink
sdk-v3: Migrate CreateFunction to v3 with test
Browse files Browse the repository at this point in the history
  • Loading branch information
abetomo committed Oct 27, 2024
1 parent 92d07cc commit 5cd7622
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
24 changes: 6 additions & 18 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const { createNamespace } = require('continuation-local-storage')
* https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html
*/
const { LambdaClient, CreateFunctionCommand } = require('@aws-sdk/client-lambda')
const lambdaClient = new LambdaClient()

const maxBufferSize = 50 * 1024 * 1024

Expand Down Expand Up @@ -699,22 +698,7 @@ Emulate only the body of the API Gateway event.
}

_uploadNew (lambda, params) {
if (process.env.USE_AWS_SDK_V3) {
console.log('DEBUG: Use AWS SDK V3: CreateFunctionCommand()')
const command = new CreateFunctionCommand(params)
return lambdaClient.send(command)
}

return new Promise((resolve, reject) => {
const request = lambda.createFunction(params, (err, data) => {
if (err) return reject(err)
resolve(data)
})
request.on('retry', (response) => {
console.log(response.error.message)
console.log('=> Retrying')
})
})
return lambda.send(new CreateFunctionCommand(params))
}

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

_deployToRegion (program, params, region, buffer) {
// sdk v3 todo: Migration of aws.updateConfig.
aws.updateConfig(program, region)

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

// Migrating to v3.
const lambda = new aws.sdk.Lambda({
region,
apiVersion: '2015-03-31'
})
const lambdaClient = new LambdaClient({ region })

const scheduleEvents = new ScheduleEvents(aws.sdk, region)
const s3Events = new S3Events(aws.sdk, region)
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
Expand Down Expand Up @@ -1069,7 +1057,7 @@ they may not work as expected in the Lambda environment.
throw err
}
// Function does not exist
return this._uploadNew(lambda, params).then((results) => {
return this._uploadNew(lambdaClient, params).then((results) => {
console.log('=> Done uploading. Results follow: ')
console.log(results)

Expand Down
19 changes: 16 additions & 3 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ let assert
import('chai').then(chai => {
assert = chai.assert
})
const sinon = require('sinon')

const awsMock = require('aws-sdk-mock')
awsMock.setSDK(path.resolve('node_modules/aws-sdk'))

// Migrating to v3.
const { LambdaClient } = require('@aws-sdk/client-lambda')
const lambdaClient = new LambdaClient({ region: 'us-east-1' })

const originalProgram = {
packageManager: 'npm',
environment: 'development',
Expand All @@ -22,7 +28,7 @@ const originalProgram = {
sessionToken: 'token',
functionName: '___node-lambda',
handler: 'index.handler',
role: 'some:arn:aws:iam::role',
role: 'arn:aws:iam::999999999999:role/test',
memorySize: 128,
timeout: 3,
description: '',
Expand Down Expand Up @@ -155,8 +161,15 @@ describe('lib/main', function () {
return
}
execFileSync('npm', ['ci'], { cwd: sourceDirectoryForTest })

// for sdk v3
const stub = sinon.stub(lambdaClient, 'send')
stub.returns(lambdaMockSettings.createFunction)
})
after(() => {
_awsRestore()
sinon.restore() // for sdk v3
})
after(() => _awsRestore())

beforeEach(() => {
program = Object.assign({}, originalProgram) // clone
Expand Down Expand Up @@ -1555,7 +1568,7 @@ describe('lib/main', function () {
describe('_uploadNew', () => {
it('simple test with mock', () => {
const params = lambda._params(program, null)
return lambda._uploadNew(awsLambda, params, (results) => {
return lambda._uploadNew(lambdaClient, params, (results) => {
assert.deepEqual(results, lambdaMockSettings.createFunction)
})
})
Expand Down

0 comments on commit 5cd7622

Please sign in to comment.