Issue
I am trying to use the describeInstances function in amazon ec2 to get details about my instance using my tag id. In the documentation it mentions use the filter,
tag:key=value - The key/value combination of a tag assigned to the resource, where tag:key is the tag's key.
I tried it in the following way:
var params1 = {
Filters : [
{
Tags : [ {
Key : key_name,
Value : key_value
} ]
}
]
};
ec2.describeInstances(params1, function(data, err) {
})
, but I get an error: Unexpected Token at Tags : What is the correct way to use this api?
Solution
The documentation is a little confusing, but you need to construct a filter name that includes the tag: prefix and your tag name. Here's a working example:
var AWS = require('aws-sdk');
var ec2 = new AWS.EC2({
region: 'eu-west-1'
});
var params = {
Filters: [
{
Name: 'tag:Project',
Values: ['foo']
}
]
};
ec2.describeInstances(params, function (err, data) {
if (err) return console.error(err.message);
console.log(data);
});
This returns all instances that have the tag Project set to the value foo.
Answered By - Robin Murphy Answer Checked By - Marie Seifert (WPSolving Admin)