From 6667d2715a85a45a88896fa282b72fc7d1f39ecf Mon Sep 17 00:00:00 2001 From: Corey Christous Date: Sat, 29 Mar 2025 11:04:01 -0400 Subject: [PATCH 1/3] add SEMAPHORE_AGENT_ASG_MAX_INSTANCE_LIFETIME --- lib/argument-store.js | 1 + lib/aws-semaphore-agent-stack.js | 1 + test/aws-semaphore-agent.test.js | 12 +++++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/argument-store.js b/lib/argument-store.js index b03bf44..c943aa5 100644 --- a/lib/argument-store.js +++ b/lib/argument-store.js @@ -14,6 +14,7 @@ class ArgumentStore { "SEMAPHORE_AGENT_ASG_MAX_SIZE": "1", "SEMAPHORE_AGENT_ASG_DESIRED": "", "SEMAPHORE_AGENT_ASG_METRICS": "", + "SEMAPHORE_AGENT_ASG_MAX_INSTANCE_LIFETIME": "0", "SEMAPHORE_AGENT_DISCONNECT_AFTER_JOB": "true", "SEMAPHORE_AGENT_DISCONNECT_AFTER_IDLE_TIMEOUT": "300", "SEMAPHORE_AGENT_OS": "ubuntu-focal", diff --git a/lib/aws-semaphore-agent-stack.js b/lib/aws-semaphore-agent-stack.js index 5f17c82..1f0b082 100644 --- a/lib/aws-semaphore-agent-stack.js +++ b/lib/aws-semaphore-agent-stack.js @@ -464,6 +464,7 @@ class AwsSemaphoreAgentStack extends Stack { minSize: this.argumentStore.get("SEMAPHORE_AGENT_ASG_MIN_SIZE"), maxSize: this.argumentStore.get("SEMAPHORE_AGENT_ASG_MAX_SIZE"), cooldown: "60", + maxInstanceLifetime: this.argumentStore.getAsNumber("SEMAPHORE_AGENT_ASG_MAX_INSTANCE_LIFETIME"), tags: [ { key: "application", diff --git a/test/aws-semaphore-agent.test.js b/test/aws-semaphore-agent.test.js index eb642b5..66d5d68 100644 --- a/test/aws-semaphore-agent.test.js +++ b/test/aws-semaphore-agent.test.js @@ -531,7 +531,8 @@ describe("auto scaling group", () => { template.hasResourceProperties("AWS::AutoScaling::AutoScalingGroup", { DesiredCapacity: Match.absent(), MinSize: "0", - MaxSize: "1" + MaxSize: "1", + MaxInstanceLifetime: 0, }); }) @@ -630,6 +631,15 @@ describe("auto scaling group", () => { template.resourceCountIs("AWS::CloudFormation::CustomResource", 1); }) + + test("max instance lifetime is set", () => { + const argumentStore = basicArgumentStore(); + argumentStore.set("SEMAPHORE_AGENT_ASG_MAX_INSTANCE_LIFETIME", 86400); + const template = createTemplate(argumentStore); + template.hasResourceProperties("AWS::AutoScaling::AutoScalingGroup", { + MaxInstanceLifetime: 86400, + }); + }) }) describe("scaler lambda", () => { From d5592f0cce12e315d8721d25def8ed3563ac2595 Mon Sep 17 00:00:00 2001 From: Corey Christous Date: Mon, 31 Mar 2025 16:40:16 -0400 Subject: [PATCH 2/3] conditionally set value in ASG --- lib/aws-semaphore-agent-stack.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/aws-semaphore-agent-stack.js b/lib/aws-semaphore-agent-stack.js index 1f0b082..53e527b 100644 --- a/lib/aws-semaphore-agent-stack.js +++ b/lib/aws-semaphore-agent-stack.js @@ -464,7 +464,6 @@ class AwsSemaphoreAgentStack extends Stack { minSize: this.argumentStore.get("SEMAPHORE_AGENT_ASG_MIN_SIZE"), maxSize: this.argumentStore.get("SEMAPHORE_AGENT_ASG_MAX_SIZE"), cooldown: "60", - maxInstanceLifetime: this.argumentStore.getAsNumber("SEMAPHORE_AGENT_ASG_MAX_INSTANCE_LIFETIME"), tags: [ { key: "application", @@ -474,6 +473,11 @@ class AwsSemaphoreAgentStack extends Stack { ], }); + const maxInstanceLifetime = this.argumentStore.getAsNumber("SEMAPHORE_AGENT_ASG_MAX_INSTANCE_LIFETIME"); + if (maxInstanceLifetime !== 0) { + autoScalingGroup.maxInstanceLifetime = maxInstanceLifetime; + } + // if the desired capacity is not specified, leave it empty. // Setting a default desired capacity leads to Cloudformation resetting it during an in-place update. // That can be an issue if you have your stack configured to automatically scale up/down, so we don't do that here. From 02079e84c2b8027d626002d7ddee307f9176e643 Mon Sep 17 00:00:00 2001 From: Corey Christous Date: Mon, 31 Mar 2025 16:41:20 -0400 Subject: [PATCH 3/3] update test --- test/aws-semaphore-agent.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/aws-semaphore-agent.test.js b/test/aws-semaphore-agent.test.js index 66d5d68..9948972 100644 --- a/test/aws-semaphore-agent.test.js +++ b/test/aws-semaphore-agent.test.js @@ -532,7 +532,6 @@ describe("auto scaling group", () => { DesiredCapacity: Match.absent(), MinSize: "0", MaxSize: "1", - MaxInstanceLifetime: 0, }); })