Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gortok/6908999db5fe293eec5c to your computer and use it in GitHub Desktop.
Save gortok/6908999db5fe293eec5c to your computer and use it in GitHub Desktop.
Get EC2 Instance Tags in AWS SDK .NET
using Amazon.EC2;
using Amazon.EC2.Model;
namespace Program
{
public class InstanceInformation
{
public List<string> GetTagsByName(string name)
{
AmazonEc2Client client = new AmazonEc2Client();
List<string> tags = new List<string>();
var request = new DescribeInstancesRequest();
var response = client.DescribeInstances(request);
foreach (var ec2 in response.Reservations)
{
foreach (var instance in ec2.Instances)
{
tags.AddRange(from tag in instance.Tags where tag.Key.Equals("Name", StringComparison.InvariantCultureIgnoreCase) && tag.Value.ToUpper().StartsWith(name.ToUpper()) select tag.Value);
}
}
return tags;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment