Skip to content

Commit 9767603

Browse files
authored
fix: Remove runner agents if provider assumed a role (#401)
* Integrate the removal script into Terraform * Reference variables from trigger only * Format code * Always read the token
1 parent 3f79054 commit 9767603

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

bin/remove-runner.sh

-5
This file was deleted.

main.tf

+12-5
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,26 @@ resource "aws_ssm_parameter" "runner_registration_token" {
2121
}
2222
}
2323

24+
# to read the current token for the null_resource. aws_ssm_parameter.runner_registration_token.value is never updated!
25+
data "aws_ssm_parameter" "current_runner_registration_token" {
26+
depends_on = [aws_ssm_parameter.runner_registration_token]
27+
28+
name = local.secure_parameter_store_runner_token_key
29+
}
30+
2431
resource "null_resource" "remove_runner" {
2532
depends_on = [aws_ssm_parameter.runner_registration_token]
33+
2634
triggers = {
27-
script = "${path.module}/bin/remove-runner.sh"
28-
aws_region = var.aws_region
29-
runners_gitlab_url = var.runners_gitlab_url
30-
secure_parameter_store_runner_token_key = local.secure_parameter_store_runner_token_key
35+
aws_region = var.aws_region
36+
runners_gitlab_url = var.runners_gitlab_url
37+
runner_registration_token = data.aws_ssm_parameter.current_runner_registration_token.value
3138
}
3239

3340
provisioner "local-exec" {
3441
when = destroy
3542
on_failure = continue
36-
command = "${self.triggers.script} ${self.triggers.aws_region} ${self.triggers.runners_gitlab_url} ${self.triggers.secure_parameter_store_runner_token_key}"
43+
command = "curl -sS --request DELETE \"${self.triggers.runners_gitlab_url}/api/v4/runners\" --form \"token=${self.triggers.runner_registration_token}\""
3744
}
3845
}
3946

versions.tf

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
terraform {
22
required_version = ">= 0.13.0"
3+
34
required_providers {
45
aws = {
56
version = ">= 3.35.0"
67
source = "hashicorp/aws"
78
}
9+
810
null = {
911
source = "hashicorp/null"
1012
}

0 commit comments

Comments
 (0)