Skip to content

Commit

Permalink
feat(runner): allow linux starter-runner script to retrieve labels wi…
Browse files Browse the repository at this point in the history
…thout 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 <niek.palm@philips.com>
Co-authored-by: Niek Palm <npalm@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 10, 2023
1 parent e350d15 commit 0d8a74c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
7 changes: 7 additions & 0 deletions examples/multi-runner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ module "multi-runner" {
fifo = true
delay_webhook_event = 0
runner_config = {
# Test retrieving tag information via AWS API (Cli)
runner_metadata_options = {
instance_metadata_tags = "disabled"
http_endpoint = "enabled"
http_tokens = "optional"
http_put_response_hop_limit = 1
}
runner_os = "linux"
runner_architecture = "x64"
create_service_linked_role_spot = true
Expand Down
6 changes: 4 additions & 2 deletions modules/runners/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ resource "aws_launch_template" "runner" {
S3_LOCATION_RUNNER_DISTRIBUTION = local.s3_location_runner_distribution
RUNNER_ARCHITECTURE = var.runner_architecture
})
post_install = var.userdata_post_install
start_runner = templatefile(local.userdata_start_runner[var.runner_os], {})
post_install = var.userdata_post_install
start_runner = templatefile(local.userdata_start_runner[var.runner_os], {
metadata_tags = var.metadata_options != null ? var.metadata_options.instance_metadata_tags : "enabled"
})
ghes_url = var.ghes_url
ghes_ssl_verify = var.ghes_ssl_verify

Expand Down
14 changes: 11 additions & 3 deletions modules/runners/templates/start-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ echo "Retrieved REGION from AWS API ($region)"
instance_id=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/instance-id)
echo "Retrieved INSTANCE_ID from AWS API ($instance_id)"

%{ if metadata_tags == "enabled" }
environment=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/tags/instance/ghr:environment)
echo "Retrieved ghr:environment tag - ($environment)"

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)
echo "Retrieved ghr:ssm_config_path tag - ($ssm_config_path)"

%{ else }
tags=$(aws ec2 describe-tags --region "$region" --filters "Name=resource-id,Values=$instance_id")
echo "Retrieved tags from AWS API ($tags)"

environment=$(echo "$tags" | jq -r '.Tags[] | select(.Key == "ghr:environment") | .Value')
ssm_config_path=$(echo "$tags" | jq -r '.Tags[] | select(.Key == "ghr:ssm_config_path") | .Value')
%{ endif }

echo "Retrieved ghr:environment tag - ($environment)"
echo "Retrieved ghr:ssm_config_path tag - ($ssm_config_path)"

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

0 comments on commit 0d8a74c

Please sign in to comment.