Skip to content

Commit 0f346f3

Browse files
authored
Support mutli subnet (#5)
ported from machulav#85
1 parent 2698ed6 commit 0f346f3

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

action.yml

Lines changed: 2 additions & 1 deletion
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:

dist/index.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55575,27 +55575,32 @@ async function startEc2Instance(label, githubRegistrationToken) {
5557555575

5557655576
const userData = buildUserDataScript(githubRegistrationToken, label);
5557755577

55578-
const params = {
55579-
ImageId: config.input.ec2ImageId,
55580-
InstanceType: config.input.ec2InstanceType,
55581-
MinCount: 1,
55582-
MaxCount: 1,
55583-
UserData: Buffer.from(userData.join('\n')).toString('base64'),
55584-
SubnetId: config.input.subnetId,
55585-
SecurityGroupIds: [config.input.securityGroupId],
55586-
IamInstanceProfile: { Name: config.input.iamRoleName },
55587-
TagSpecifications: config.tagSpecifications,
55588-
};
55589-
55590-
try {
55591-
const result = await ec2.runInstances(params).promise();
55592-
const ec2InstanceId = result.Instances[0].InstanceId;
55593-
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
55594-
return ec2InstanceId;
55595-
} catch (error) {
55596-
core.error('AWS EC2 instance starting error');
55597-
throw error;
55578+
const subnetId = config.input.subnetId;
55579+
const subnets = subnetId ? subnetId.replace(/\s/g, '').split(',') : [null];
55580+
55581+
for (const subnet of subnets) {
55582+
const params = {
55583+
ImageId: config.input.ec2ImageId,
55584+
InstanceType: config.input.ec2InstanceType,
55585+
MinCount: 1,
55586+
MaxCount: 1,
55587+
UserData: Buffer.from(userData.join('\n')).toString('base64'),
55588+
SubnetId: subnet,
55589+
SecurityGroupIds: [config.input.securityGroupId],
55590+
IamInstanceProfile: { Name: config.input.iamRoleName },
55591+
TagSpecifications: config.tagSpecifications,
55592+
};
55593+
try {
55594+
const result = await ec2.runInstances(params).promise();
55595+
const ec2InstanceId = result.Instances[0].InstanceId;
55596+
core.info(`AWS EC2 instance ${ec2InstanceId} is started`);
55597+
return ec2InstanceId;
55598+
} catch (error) {
55599+
core.warning('AWS EC2 instance starting error');
55600+
core.warning(error);
55601+
}
5559855602
}
55603+
core.setFailed(`Failed to launch instance after trying in ${subnets.length} subnets.`);
5559955604
}
5560055605

5560155606
async function terminateEc2Instance() {

src/aws.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,32 @@ async function startEc2Instance(label, githubRegistrationToken) {
3333

3434
const userData = buildUserDataScript(githubRegistrationToken, label);
3535

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

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

5964
async function terminateEc2Instance() {

0 commit comments

Comments
 (0)