Skip to content

Commit

Permalink
Merge pull request #225 from mettke/migrate-sdk-v2-to-sdk-v3
Browse files Browse the repository at this point in the history
Migrate sdk v2 to sdk v3
  • Loading branch information
alexcasalboni authored Jan 9, 2024
2 parents 56cc874 + 2242632 commit 4a28bfc
Show file tree
Hide file tree
Showing 11 changed files with 3,432 additions and 963 deletions.
3 changes: 2 additions & 1 deletion lambda/cleaner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { ResourceNotFoundException } = require("@aws-sdk/client-lambda");
const utils = require('./utils');

/**
Expand Down Expand Up @@ -39,7 +40,7 @@ const cleanup = async(lambdaARN, alias) => {
await utils.deleteLambdaAlias(lambdaARN, alias);
await utils.deleteLambdaVersion(lambdaARN, FunctionVersion);
} catch (error) {
if (error.code === 'ResourceNotFoundException') {
if (error instanceof ResourceNotFoundException) {
console.error('OK, even if version/alias was not found');
console.error(error);
} else {
Expand Down
6 changes: 0 additions & 6 deletions lambda/executor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
'use strict';

const AWS = require('aws-sdk');

// the executor needs a longer socket timeout to invoke long-running functions
// 15 minutes is fine here because the Executor will timeout anyway
AWS.config.update({httpOptions: {timeout: 15 * 60 * 1000}});

const utils = require('./utils');

const minRAM = parseInt(process.env.minRAM, 10);
Expand Down
70 changes: 38 additions & 32 deletions lambda/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const AWS = require('aws-sdk');
const { CreateAliasCommand, DeleteAliasCommand, DeleteFunctionCommand, GetAliasCommand, GetFunctionConfigurationCommand, InvokeCommand, LambdaClient, PublishVersionCommand, UpdateAliasCommand, UpdateFunctionConfigurationCommand, waitUntilFunctionActive, waitUntilFunctionUpdated, ResourceNotFoundException } = require("@aws-sdk/client-lambda");
const { GetObjectCommand, S3Client } = require("@aws-sdk/client-s3");
const url = require('url');


Expand Down Expand Up @@ -44,7 +45,7 @@ module.exports.getLambdaAlias = (lambdaARN, alias) => {
Name: alias,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.getAlias(params).promise();
return lambda.send(new GetAliasCommand(params));
};

/**
Expand All @@ -55,7 +56,8 @@ module.exports.verifyAliasExistance = async(lambdaARN, alias) => {
await utils.getLambdaAlias(lambdaARN, alias);
return true;
} catch (error) {
if (error.code === 'ResourceNotFoundException') {
console.log("Error during verifyAlias (probably OK!)")
if (error instanceof ResourceNotFoundException) {
// OK, the alias isn't supposed to exist
console.log('OK, even if missing alias ');
return false;
Expand Down Expand Up @@ -103,30 +105,25 @@ module.exports.waitForFunctionUpdate = async(lambdaARN) => {
console.log('Waiting for update to complete');
const params = {
FunctionName: lambdaARN,
$waiter: { // override delay (5s by default)
delay: 0.5,
},
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.waitFor('functionUpdated', params).promise();
return waitUntilFunctionUpdated({
client: lambda,
minDelay: 1,
}, params);
};

module.exports.waitForAliasActive = async(lambdaARN, alias) => {
console.log(`Waiting for alias ${alias} to be active`);
const params = {
FunctionName: lambdaARN,
Qualifier: alias,
$waiter: {
// https://aws.amazon.com/blogs/developer/waiters-in-modular-aws-sdk-for-javascript/
// "In v2, there is no direct way to provide maximum wait time for a waiter.
// You need to configure delay and maxAttempts to indirectly suggest the maximum time you want the waiter to run for."
// 10s * 90 is ~15 minutes (max invocation time)
delay: 10,
maxAttempts: 90,
},
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.waitFor('functionActive', params).promise();
return waitUntilFunctionActive({
client: lambda,
maxDelay: 10 * 90,
}, params);
};

/**
Expand All @@ -139,7 +136,7 @@ module.exports.getLambdaPower = async(lambdaARN) => {
Qualifier: '$LATEST',
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
const config = await lambda.getFunctionConfiguration(params).promise();
const config = await lambda.send(new GetFunctionConfigurationCommand(params));
return config.MemorySize;
};

Expand All @@ -154,7 +151,7 @@ module.exports.getLambdaConfig = async(lambdaARN, alias) => {
};
let architecture, isPending;
const lambda = utils.lambdaClientFromARN(lambdaARN);
const config = await lambda.getFunctionConfiguration(params).promise();
const config = await lambda.send(new GetFunctionConfigurationCommand(params));
if (typeof config.Architectures !== 'undefined') {
architecture = config.Architectures[0];
} else {
Expand Down Expand Up @@ -182,7 +179,7 @@ module.exports.setLambdaPower = (lambdaARN, value) => {
MemorySize: parseInt(value, 10),
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.updateFunctionConfiguration(params).promise();
return lambda.send(new UpdateFunctionConfigurationCommand(params));
};

/**
Expand All @@ -194,7 +191,7 @@ module.exports.publishLambdaVersion = (lambdaARN /*, alias*/) => {
FunctionName: lambdaARN,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.publishVersion(params).promise();
return lambda.send(new PublishVersionCommand(params));
};

/**
Expand All @@ -207,7 +204,7 @@ module.exports.deleteLambdaVersion = (lambdaARN, version) => {
Qualifier: version,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.deleteFunction(params).promise();
return lambda.send(new DeleteFunctionCommand(params));
};

/**
Expand All @@ -221,7 +218,7 @@ module.exports.createLambdaAlias = (lambdaARN, alias, version) => {
Name: alias,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.createAlias(params).promise();
return lambda.send(new CreateAliasCommand(params));
};

/**
Expand All @@ -235,7 +232,7 @@ module.exports.updateLambdaAlias = (lambdaARN, alias, version) => {
Name: alias,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.updateAlias(params).promise();
return lambda.send(new UpdateAliasCommand(params));
};

/**
Expand All @@ -248,7 +245,7 @@ module.exports.deleteLambdaAlias = (lambdaARN, alias) => {
Name: alias,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.deleteAlias(params).promise();
return lambda.send( new DeleteAliasCommand(params));
};

/**
Expand Down Expand Up @@ -315,7 +312,7 @@ module.exports.invokeLambda = (lambdaARN, alias, payload, disablePayloadLogs) =>
LogType: 'Tail', // will return logs
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.invoke(params).promise();
return lambda.send(new InvokeCommand(params));
};

/**
Expand Down Expand Up @@ -352,20 +349,26 @@ module.exports.fetchPayloadFromS3 = async(s3Path) => {
};

module.exports._fetchS3Object = async(bucket, key) => {
const s3 = new AWS.S3();
try {
const response = await s3.getObject({
const s3Client = new S3Client({});
const input = {
Bucket: bucket,
Key: key,
}).promise();
return response.Body.toString('utf-8');
};
var response = undefined;
response = await s3Client.send(new GetObjectCommand(input));
return await response.Body.transformToString('utf-8');
} catch (err) {
if (err.statusCode === 403) {
var statusCode = err.statusCode
if (err.$response && err.$response.statusCode) {
statusCode = err.$response.statusCode
}
if (statusCode === 403) {
throw new Error(
`Permission denied when trying to read s3://${bucket}/${key}. ` +
'You might need to re-deploy the app with the correct payloadS3Bucket parameter.',
);
} else if (err.statusCode === 404) {
} else if (statusCode === 404) {
throw new Error(
`The object s3://${bucket}/${key} does not exist. ` +
'Make sure you are trying to access an existing object in the correct bucket.',
Expand Down Expand Up @@ -553,7 +556,10 @@ module.exports.regionFromARN = (arn) => {

module.exports.lambdaClientFromARN = (lambdaARN) => {
const region = this.regionFromARN(lambdaARN);
return new AWS.Lambda({region});
return new LambdaClient({
region,
requestTimeout: 15 * 60 * 1000
})
};

/**
Expand Down
Loading

0 comments on commit 4a28bfc

Please sign in to comment.