'Create Images from EC2 instance based on tags

We have created an environment in AWS and configured all the instances. We have the instances tagged with NAME and ENVIRONMENT tags.

We would like to make images of all the instances tagged with a specific Environment tag. I'm able query the environment with aws ec2 describe-instances --query and pull the tags (ex, Environment=TEST), but can't figure out how to proceed from here so that all instances with the environment tag TEST can be imaged programmatically.

This was the command I used to pull the NAME and ENVIRONMENT tag info:

aws ec2 describe-instances --query "Reservations\[*\].Instances\[*\].{Name:Tags\[?Key=='Name'\],Environment:Tags\[?Key=='Environment'\]|\[0\].Value,Status:State.Name}"  --filters "Name=instance-state-name,Values=running"


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source