Skip to content

Commit

Permalink
Support trying multiple subnets
Browse files Browse the repository at this point in the history
ported from machulav#85
  • Loading branch information
brandboat committed Mar 14, 2022
1 parent 2698ed6 commit 1a735e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
3 changes: 2 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ inputs:
required: false
subnet-id:
description: >-
VPC Subnet Id. The subnet should belong to the same VPC as the specified security group.
VPC Subnet Id. You may provide a comma-separated list of subnet ids to try multiple subnets.
The subnet should belong to the same VPC as the specified security group.
This input is required if you use the 'start' mode.
required: false
security-group-id:
Expand Down
43 changes: 24 additions & 19 deletions src/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,32 @@ async function startEc2Instance(label, githubRegistrationToken) {

const userData = buildUserDataScript(githubRegistrationToken, label);

const params = {
ImageId: config.input.ec2ImageId,
InstanceType: config.input.ec2InstanceType,
MinCount: 1,
MaxCount: 1,
UserData: Buffer.from(userData.join('\n')).toString('base64'),
SubnetId: config.input.subnetId,
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
};
const subnetId = config.input.subnetId;
const subnets = subnetId ? subnetId.replace(/\s/g, '').split(',') : [null];

try {
const result = await ec2.runInstances(params).promise();
const ec2InstanceId = result.Instances[0].InstanceId;
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
return ec2InstanceId;
} catch (error) {
core.error('AWS EC2 instance starting error');
throw error;
for (const subnet of subnets) {
const params = {
ImageId: config.input.ec2ImageId,
InstanceType: config.input.ec2InstanceType,
MinCount: 1,
MaxCount: 1,
UserData: Buffer.from(userData.join('\n')).toString('base64'),
SubnetId: subnet,
SecurityGroupIds: [config.input.securityGroupId],
IamInstanceProfile: { Name: config.input.iamRoleName },
TagSpecifications: config.tagSpecifications,
};
try {
const result = await ec2.runInstances(params).promise();
const ec2InstanceId = result.Instances[0].InstanceId;
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
return ec2InstanceId;
} catch (error) {
core.warning('AWS EC2 instance starting error');
core.warning(error);
}
}
core.setFailed(`Failed to launch instance after trying in ${subnets.length} subnets.`);
}

async function terminateEc2Instance() {
Expand Down

0 comments on commit 1a735e4

Please sign in to comment.