Skip to content
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

[WIP] Secondary instance type #586

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
101 changes: 70 additions & 31 deletions modules/runners/lambdas/runners/src/scale-runners/runners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ export interface ListRunnerFilters {
environment?: string;
}

export interface instanceParams {
MaxCount: number,
MinCount: number,
LaunchTemplate: {
LaunchTemplateName: string,
Version: string,
},
SubnetId: string,
TagSpecifications: [
{
ResourceType: string,
Tags: [
{ Key: string, Value?: string },
{ Key: string, Value?: string },
],
},
],
InstanceType?: string
}

export async function listRunners(filters: ListRunnerFilters | undefined = undefined): Promise<RunnerInfo[]> {
const ec2 = new EC2();
const ec2Filters = [
Expand Down Expand Up @@ -66,39 +86,13 @@ export async function terminateRunner(runner: RunnerInfo): Promise<void> {
console.debug('Runner terminated.' + runner.instanceId);
}

export async function createRunner(runnerParameters: RunnerInputParameters): Promise<void> {
const launchTemplateName = process.env.LAUNCH_TEMPLATE_NAME as string;
const launchTemplateVersion = process.env.LAUNCH_TEMPLATE_VERSION as string;

const subnets = (process.env.SUBNET_IDS as string).split(',');
const randomSubnet = subnets[Math.floor(Math.random() * subnets.length)];
console.debug('Runner configuration: ' + JSON.stringify(runnerParameters));
function launchInstance(params: instanceParams): Promise<EC2.Reservation> {
const ec2 = new EC2();
const runInstancesResponse = await ec2
.runInstances({
MaxCount: 1,
MinCount: 1,
LaunchTemplate: {
LaunchTemplateName: launchTemplateName,
Version: launchTemplateVersion,
},
SubnetId: randomSubnet,
TagSpecifications: [
{
ResourceType: 'instance',
Tags: [
{ Key: 'Application', Value: 'github-action-runner' },
{
Key: runnerParameters.orgName ? 'Org' : 'Repo',
Value: runnerParameters.orgName ? runnerParameters.orgName : runnerParameters.repoName,
},
],
},
],
})
.promise();
console.info('Created instance(s): ', runInstancesResponse.Instances?.map((i) => i.InstanceId).join(','));
return ec2.runInstances(params).promise();
}

async function putSSMparameter(runInstancesResponse:EC2.Reservation, runnerParameters:RunnerInputParameters) {
console.info('Created instance(s): ', runInstancesResponse.Instances?.map((i) => i.InstanceId).join(','));
const ssm = new SSM();
runInstancesResponse.Instances?.forEach(async (i: EC2.Instance) => {
await ssm
Expand All @@ -110,3 +104,48 @@ export async function createRunner(runnerParameters: RunnerInputParameters): Pro
.promise();
});
}

export async function createRunner(runnerParameters: RunnerInputParameters): Promise<void> {
const launchTemplateName = process.env.LAUNCH_TEMPLATE_NAME as string;
const launchTemplateVersion = process.env.LAUNCH_TEMPLATE_VERSION as string;

const subnets = (process.env.SUBNET_IDS as string).split(',');
const randomSubnet = subnets[Math.floor(Math.random() * subnets.length)];
console.debug('Runner configuration: ' + JSON.stringify(runnerParameters));

const instanceParams : instanceParams = {
MaxCount: 1,
MinCount: 1,
LaunchTemplate: {
LaunchTemplateName: launchTemplateName,
Version: launchTemplateVersion,
},
SubnetId: randomSubnet,
TagSpecifications: [
{
ResourceType: 'instance',
Tags: [
{ Key: 'Application', Value: 'github-action-runner' },
{
Key: runnerParameters.orgName ? 'Org' : 'Repo',
Value: runnerParameters.orgName ? runnerParameters.orgName : runnerParameters.repoName,
},
],
},
],
};

launchInstance(instanceParams)
.then(async function (runInstancesResponse) {
putSSMparameter(runInstancesResponse, runnerParameters);
})
.catch( async function (err) {
console.info(err);
instanceParams.InstanceType = process.env.SECONDARY_INSTANCE_TYPE as string;
return launchInstance(instanceParams).then(runInstancesResponse => {
console.info('Created instance(s): ', runInstancesResponse.Instances?.map((i) => i.InstanceId).join(','));
putSSMparameter(runInstancesResponse, runnerParameters);
})
})
.catch(e => console.error(e));
}
1 change: 1 addition & 0 deletions modules/runners/scale-up.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ resource "aws_lambda_function" "scale_up" {
LAUNCH_TEMPLATE_NAME = aws_launch_template.runner.name
LAUNCH_TEMPLATE_VERSION = aws_launch_template.runner.latest_version
SUBNET_IDS = join(",", var.subnet_ids)
SECONDARY_INSTANCE_TYPE = var.secondary_instance_type
}
}

Expand Down
6 changes: 6 additions & 0 deletions modules/runners/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ variable "instance_type" {
default = "m5.large"
}

variable "secondary_instance_type" {
description = "(optional) Secondary instance type to avoid no Spot capacity available error"
type = string
default = "m5.xlarge"
}

variable "ami_filter" {
description = "List of maps used to create the AMI filter for the action runner AMI."
type = map(list(string))
Expand Down