Skip to content

chore: migrate AWS SDK for JavaScript v2 APIs to v3 #11890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions devops/actions/aws-ec2/aws-ec2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const core = require('@actions/core');
const github = require('@actions/github');
const AWS = require('aws-sdk');
const { EC2, waitUntilInstanceRunning } = require('@aws-sdk/client-ec2');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was you able to test these changes in your own repo or it can potentially break stuff?


// shortcut to reference current repo
const repo = `${github.context.repo.owner}/${github.context.repo.repo}`;
Expand Down Expand Up @@ -35,7 +35,13 @@ let reg_token;

// starts AWS EC2 instance that will spawn Github runner for a given label
async function start(param_type, param_label, param_ami, param_spot, param_disk, param_timebomb, param_onejob) {
const ec2 = new AWS.EC2();
const ec2 = new EC2({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this code for ec2 object creation outside this function (ideally it should be only called only once before start ort stop function call) since this function can be called in the loop and creating several copies of this EC2 object is not the best practice I guess.

credentials: {
accessKeyId: core.getInput("AWS_ACCESS_KEY"),
secretAccessKey: core.getInput("AWS_SECRET_KEY")
},
region: core.getInput("aws-region")
});

reg_token = reg_token ? reg_token : await getGithubRegToken();
const ec2types = typeof param_type === 'string' ? [ param_type ] : param_type;
Expand Down Expand Up @@ -87,7 +93,7 @@ async function start(param_type, param_label, param_ami, param_spot, param_disk,
const items = ec2disk.split(':');
params.BlockDeviceMappings = [ {DeviceName: items[0], Ebs: {VolumeSize: items[1]}} ];
}
const result = await ec2.runInstances(params).promise();
const result = await ec2.runInstances(params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ec2id = result.Instances[0].InstanceId;
core.info(`Created AWS EC2 ${spot_str} instance ${ec2id} of ${ec2type} type with ${label} label`);
break;
Expand All @@ -107,9 +113,12 @@ async function start(param_type, param_label, param_ami, param_spot, param_disk,
// wait untill instance will be found running before continuing (spot instance
// can be created but never run and will be in pending state untill
// termination)
let p = ec2.waitFor("instanceRunning", {
let p = waitUntilInstanceRunning({
client: ec2,
maxWaitTime: 200
}, {
Filters: [ { Name: "tag:Label", Values: [ label ] } ]
}).promise();
});
for (let i = 0; i < 2; i++) {
p = p.catch(function() {
core.warning(`Error searching for running AWS EC2 instance ${ec2id} with ${label} label. Will retry.`);
Expand All @@ -129,7 +138,13 @@ async function start(param_type, param_label, param_ami, param_spot, param_disk,
async function stop(param_label) {
// last error that will be thrown in case something will break here
let last_error;
const ec2 = new AWS.EC2();
const ec2 = new EC2({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to avoid this code duplication with start function and pass ec2 as start/stop function param.

credentials: {
accessKeyId: core.getInput("AWS_ACCESS_KEY"),
secretAccessKey: core.getInput("AWS_SECRET_KEY")
},
region: core.getInput("aws-region")
});

const label = typeof param_label === 'string' ? param_label : param_label[0];

Expand All @@ -138,7 +153,7 @@ async function stop(param_label) {
try {
instances = await ec2.describeInstances({
Filters: [ { Name: "tag:Label", Values: [ label ] } ]
}).promise();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});
core.info(`Searched for AWS EC2 instance with label ${label}`);
} catch (error) {
core.error(`Error searching for AWS EC2 instance with label ${label}: ${error}`);
Expand All @@ -150,7 +165,7 @@ async function stop(param_label) {
for (const reservation of instances.Reservations) {
for (const instance of reservation.Instances) {
try {
await ec2.terminateInstances({ InstanceIds: [ instance.InstanceId ] }).promise();
await ec2.terminateInstances({ InstanceIds: [ instance.InstanceId ] });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core.info(`Terminated AWS EC2 instance ${instance.InstanceId} with label ${label}`);
} catch (error) {
core.error(`Error terminating AWS EC2 instance ${instance.InstanceId} with label ${label}: ${error}`);
Expand Down Expand Up @@ -202,12 +217,6 @@ async function stop(param_label) {

(async function() {
try {
// provide AWS credentials
AWS.config.update({
accessKeyId: core.getInput("AWS_ACCESS_KEY"),
secretAccessKey: core.getInput("AWS_SECRET_KEY"),
region: core.getInput("aws-region")
});
// mode is start or stop
const mode = core.getInput("mode");
const runs_on_list = core.getInput("runs-on-list") ? JSON.parse(core.getInput("runs-on-list")) : [];
Expand Down
2 changes: 1 addition & 1 deletion devops/actions/aws-ec2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"dependencies": {
"@actions/core": "1.10.0",
"@actions/github": "5.0.3",
"aws-sdk": "2.1179.0"
"@aws-sdk/client-ec2": "3.451.0"
}
}