0

I'm trying to use a local shell processor script at the end of our Packer build that gets the latest AMI ID and then pushes this to a specific SSM param.

So far I can get the latest AMI without issue doing

AWS_PROFILE=myprofile AWS_DEFAULT_REGION=us-east-1 aws ec2 describe-images --owners self --filters "Name=name,Values=my-eks-*" --query 'reverse(sort_by(Images,&CreationDate))[:1].{id:ImageId,date:CreationDate}' --output json | grep "ami-"

The above code successfully prints the latest AMI I want to push to SSM. I'm just having trouble thinking of a way to get this output successfully to SSM within the same script. Any ideas?

Time Keeper
  • 51
  • 1
  • 3

1 Answers1

1

Generally you would store the amiid in variable which you would use in other aws cli command. Eg.

ami_id=$(AWS_PROFILE=myprofile AWS_DEFAULT_REGION=us-east-1 aws ec2 describe-images --owners self --filters "Name=name,Values=my-eks-*" --query 'reverse(sort_by(Images,&CreationDate))[:1].{id:ImageId,date:CreationDate}' --output json | grep "ami-")

and then in some other command (just some example how to reference ami_id):

aws cli --image="${ami_id}"
Rafael
  • 7,605
  • 13
  • 31
  • 46
Marcin
  • 215,873
  • 14
  • 235
  • 294