-
Notifications
You must be signed in to change notification settings - Fork 793
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'); | ||
|
||
// shortcut to reference current repo | ||
const repo = `${github.context.repo.owner}/${github.context.repo.repo}`; | ||
|
@@ -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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also replace this to match v3: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/ec2/command/RunInstancesCommand/ |
||
ec2id = result.Instances[0].InstanceId; | ||
core.info(`Created AWS EC2 ${spot_str} instance ${ec2id} of ${ec2type} type with ${label} label`); | ||
break; | ||
|
@@ -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.`); | ||
|
@@ -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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]; | ||
|
||
|
@@ -138,7 +153,7 @@ async function stop(param_label) { | |
try { | ||
instances = await ec2.describeInstances({ | ||
Filters: [ { Name: "tag:Label", Values: [ label ] } ] | ||
}).promise(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`); | ||
|
@@ -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 ] }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update to v3 as well: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/ec2/command/TerminateInstancesCommand/ |
||
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}`); | ||
|
@@ -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")) : []; | ||
|
There was a problem hiding this comment.
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?