Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 0d8a74c

Browse files
MGSousanpalm
andauthored
feat(runner): allow linux starter-runner script to retrieve labels without with IMDSv2 tags option (#2764)
* Add metadata_tags as a var to default script * Check for metadata tags service on linux * fix metadata_options var * Move metadata_tags var to start_runner template * fix for reading config via SDK - Fix start script to read ssm config tag via SDK - Update multi runner example, one runner is now using SDK instead of meta data tags --------- Co-authored-by: Niek Palm <[email protected]> Co-authored-by: Niek Palm <[email protected]>
1 parent e350d15 commit 0d8a74c

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

Diff for: examples/multi-runner/main.tf

+7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ module "multi-runner" {
120120
fifo = true
121121
delay_webhook_event = 0
122122
runner_config = {
123+
# Test retrieving tag information via AWS API (Cli)
124+
runner_metadata_options = {
125+
instance_metadata_tags = "disabled"
126+
http_endpoint = "enabled"
127+
http_tokens = "optional"
128+
http_put_response_hop_limit = 1
129+
}
123130
runner_os = "linux"
124131
runner_architecture = "x64"
125132
create_service_linked_role_spot = true

Diff for: modules/runners/main.tf

+4-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ resource "aws_launch_template" "runner" {
144144
S3_LOCATION_RUNNER_DISTRIBUTION = local.s3_location_runner_distribution
145145
RUNNER_ARCHITECTURE = var.runner_architecture
146146
})
147-
post_install = var.userdata_post_install
148-
start_runner = templatefile(local.userdata_start_runner[var.runner_os], {})
147+
post_install = var.userdata_post_install
148+
start_runner = templatefile(local.userdata_start_runner[var.runner_os], {
149+
metadata_tags = var.metadata_options != null ? var.metadata_options.instance_metadata_tags : "enabled"
150+
})
149151
ghes_url = var.ghes_url
150152
ghes_ssl_verify = var.ghes_ssl_verify
151153

Diff for: modules/runners/templates/start-runner.sh

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ echo "Retrieved REGION from AWS API ($region)"
1313
instance_id=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/instance-id)
1414
echo "Retrieved INSTANCE_ID from AWS API ($instance_id)"
1515

16+
%{ if metadata_tags == "enabled" }
1617
environment=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/tags/instance/ghr:environment)
17-
echo "Retrieved ghr:environment tag - ($environment)"
18-
1918
ssm_config_path=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/tags/instance/ghr:ssm_config_path)
20-
echo "Retrieved ghr:ssm_config_path tag - ($ssm_config_path)"
2119

20+
%{ else }
21+
tags=$(aws ec2 describe-tags --region "$region" --filters "Name=resource-id,Values=$instance_id")
22+
echo "Retrieved tags from AWS API ($tags)"
23+
24+
environment=$(echo "$tags" | jq -r '.Tags[] | select(.Key == "ghr:environment") | .Value')
25+
ssm_config_path=$(echo "$tags" | jq -r '.Tags[] | select(.Key == "ghr:ssm_config_path") | .Value')
26+
%{ endif }
27+
28+
echo "Retrieved ghr:environment tag - ($environment)"
29+
echo "Retrieved ghr:ssm_config_path tag - ($ssm_config_path)"
2230

2331
parameters=$(aws ssm get-parameters-by-path --path "$ssm_config_path" --region "$region" --query "Parameters[*].{Name:Name,Value:Value}")
2432
echo "Retrieved parameters from AWS SSM ($parameters)"

0 commit comments

Comments
 (0)