Amazon Web Services (AWS) - Add tags to an EC2 instance using the AWS CLI

by
Jeremy Canfield |
Updated: April 18 2023
| Amazon Web Services (AWS) articles
This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.
The aws ec2 describe-instances command can be used to list your EC2 instances.
aws ec2 describe-instances
--filter can be used to only return the JSON for an specific instance.
aws ec2 describe-instances --filters "Name=tag:Name,Values=my-instance"
Something like this should be returned (this is just a small subset of the output that would be returned). Notice in this example that the EC2 ID is i-09112345cf1abcdd2 and currently has 1 tag (Name = my-instance).
{
"Reservations": [
{
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-09d123474b6cabcda",
"InstanceId": "i-09112345cf1abcdd2",
"InstanceType": "t2.micro",
"KeyName": "aws",
"LaunchTime": "2022-11-06T09:23:52+00:00",
"Tags": [
{
"Key": "Name",
"Value": "my-instance"
}
]
}
]
}
]
}
The aws ec2 create-tags command can be used to add new tags, or replace existing tags.
aws ec2 create-tags --resources i-0d4b6d80b3763f894 --tags Key=foo,Value=bar
Did you find this article helpful?
If so, consider buying me a coffee over at