Skip to content

Commit 0e27527

Browse files
committed
Support trying multiple subnets
ported from machulav#85
1 parent e228833 commit 0e27527

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

action.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ inputs:
2727
required: false
2828
subnet-id:
2929
description: >-
30-
VPC Subnet Id. The subnet should belong to the same VPC as the specified security group.
30+
VPC Subnet Id. You may provide a comma-separated list of subnet ids to try multiple subnets.
31+
The subnet should belong to the same VPC as the specified security group.
3132
This input is required if you use the 'start' mode.
3233
required: false
3334
security-group-id:

src/aws.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,32 @@ async function startEc2Instance(label, githubRegistrationToken) {
3535

3636
const userData = buildUserDataScript(githubRegistrationToken, label);
3737

38-
const params = {
39-
ImageId: config.input.ec2ImageId,
40-
InstanceType: config.input.ec2InstanceType,
41-
MinCount: 1,
42-
MaxCount: 1,
43-
UserData: Buffer.from(userData.join('\n')).toString('base64'),
44-
SubnetId: config.input.subnetId,
45-
SecurityGroupIds: [config.input.securityGroupId],
46-
IamInstanceProfile: { Name: config.input.iamRoleName },
47-
TagSpecifications: config.tagSpecifications,
48-
};
38+
const subnetId = config.input.subnetId;
39+
const subnets = subnetId ? subnetId.replace(/\s/g, '').split(',') : [null];
4940

50-
try {
51-
const result = await ec2.runInstances(params).promise();
52-
const ec2InstanceId = result.Instances[0].InstanceId;
53-
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
54-
return ec2InstanceId;
55-
} catch (error) {
56-
core.error('AWS EC2 instance starting error');
57-
throw error;
41+
for (const subnet of subnets) {
42+
const params = {
43+
ImageId: config.input.ec2ImageId,
44+
InstanceType: config.input.ec2InstanceType,
45+
MinCount: 1,
46+
MaxCount: 1,
47+
UserData: Buffer.from(userData.join('\n')).toString('base64'),
48+
SubnetId: subnet,
49+
SecurityGroupIds: [config.input.securityGroupId],
50+
IamInstanceProfile: { Name: config.input.iamRoleName },
51+
TagSpecifications: config.tagSpecifications,
52+
};
53+
try {
54+
const result = await ec2.runInstances(params).promise();
55+
const ec2InstanceId = result.Instances[0].InstanceId;
56+
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
57+
return ec2InstanceId;
58+
} catch (error) {
59+
core.warning('AWS EC2 instance starting error');
60+
core.warning(error);
61+
}
5862
}
63+
core.setFailed(`Failed to launch instance after trying in ${subnets.length} subnets.`);
5964
}
6065

6166
async function terminateEc2Instance() {

0 commit comments

Comments
 (0)