nomad icon indicating copy to clipboard operation
nomad copied to clipboard

Add AWS instance tags to client metadata

Open nahsi opened this issue 2 years ago • 7 comments

Since January 2022 AWS Instance tags are available in EC2 metadata. Would it be possible to add Instance tags to Nomad client metadata?

Tags in metadata must be enabled first.

Then tags can be accessed from metadata endpoint:

nahsi@i-0f8c34d628a7cae51:~$ curl http://169.254.169.254/latest/meta-data/tags/instance
Name
aws:autoscaling:groupName
aws:ec2:fleet-id
aws:ec2launchtemplate:id
aws:ec2launchtemplate:version
class
env
role
terraform_managed

Proposal

Pupulate Nomad client metadata with Instance tags collected from AWS metadata endpoint

Use-cases

Ability to constraint jobs based on AWS tags.

nahsi avatar Apr 11 '22 08:04 nahsi

Hi @nahsi

It seems like it should be possible. The AWS SDK mod might need to be updated. It looks like we are using v1.42.27 which came out Jan 4, so I don't know if it includes the new functionality or not. I suspect the changes would need to be made in the AWS fingerprinter. Do you feel like taking a pass at it? Pull requests are always welcome 😄

DerekStrickland avatar Apr 11 '22 16:04 DerekStrickland

@DerekStrickland I will try!

nahsi avatar Apr 12 '22 06:04 nahsi

That's fantastic! Thanks for being willing. I'll look for the incoming PR.

DerekStrickland avatar Apr 12 '22 11:04 DerekStrickland

@nahsi Are you still working on this? If not, I'd like to send a PR for the same. :smile:

mr-karan avatar Oct 07 '22 03:10 mr-karan

@nahsi Are you still working on this? If not, I'd like to send a PR for the same. :smile:

Hi. Not working on this 😅

nahsi avatar Oct 07 '22 08:10 nahsi

@mr-karan Have you implemented PR for this?

EugenKon avatar Apr 05 '24 20:04 EugenKon

Not sure, but it look like it is related: https://github.com/hashicorp/nomad/issues/6744

I also found this question: https://discuss.hashicorp.com/t/list-out-all-platform-variables/1731/7

Probably curl http://169.254.169.254/latest/meta-data/tags/instance/Name could be aliased to $unique.platform.aws.tags.Name, eg. when I refer this name a call to the AWS API is made.

Usage example:

job "exec-job" {
  region = "planitar"
  datacenters = ["dc1"]
  type = "sysbatch"

  group "exec" {
    task "exec-task" {
      driver = "exec"

      config {
        command = "/bin/bash"
        args    = ["local/exec.sh"]
      }

    constraint {
      attribute = "${meta.aws.tag.Name}"
      operator  = "equal"
      value     = "worker"
    }

      template {
        destination = "local/exec.sh"
        data        = <<-EOH
          set -ex
          echo "Hello ${meta.aws.tag.Name}" # As was proposed by ChatGPT )
        EOH
      }
    }
  }
}

EugenKon avatar Apr 05 '24 21:04 EugenKon