Skip to content

Commit

Permalink
Migrating AWS SDK v2 to AWS SDK v3
Browse files Browse the repository at this point in the history
  • Loading branch information
mettke authored and alexcasalboni committed Jan 8, 2024
1 parent 56cc874 commit 91360d8
Show file tree
Hide file tree
Showing 10 changed files with 3,423 additions and 955 deletions.
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
67 changes: 36 additions & 31 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 } = 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 Down Expand Up @@ -103,30 +104,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 +135,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 +150,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 +178,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 +190,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 +203,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 +217,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 +231,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 +244,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 +311,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 +348,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 +555,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 91360d8

Please sign in to comment.