Skip to content

Instantly share code, notes, and snippets.

@lpar
Created November 12, 2020 00:23
Show Gist options
  • Save lpar/f3836238961586b658c9295e86a31c78 to your computer and use it in GitHub Desktop.
Save lpar/f3836238961586b658c9295e86a31c78 to your computer and use it in GitHub Desktop.
How to fetch the tags for the current EC2 instance, from Go, using only official AWS Go SDK calls.
package main
// An example of obtaining the tags for the current EC2 running instance,
// from a Go program running inside the instance, using the official AWS SDK (v1).
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
// Get region and instance of current running instance, via individual metadata calls
func getRegionAndInstance() (string, string, error) {
sess := session.New()
svc := ec2metadata.New(sess)
if !svc.Available() {
return "", "", errors.New("ec2metadata service not available")
}
instance, err := svc.GetMetadata("instance-id")
if err != nil {
return "", "", fmt.Errorf("can't get instance ID: %w", err)
}
region, err := svc.GetMetadata("placement/region")
if err != nil {
return "", "", fmt.Errorf("can't get region: %w", err)
}
return region, instance, nil
}
// Get region and instance of current running instance, via instance identity Document
func getRegionAndInstance2() (string, string, error) {
sess := session.New()
svc := ec2metadata.New(sess)
if !svc.Available() {
return "", "", errors.New("ec2metadata service not available")
}
idd, err := svc.GetInstanceIdentityDocument()
if err != nil {
return "", "", fmt.Errorf("can't get instance identity document: %w", err)
}
return idd.Region, idd.InstanceID, nil
}
// Get tags for instance, given region and instance ID
func getTags(region string, instance string) (map[string]string, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
},
)
svc := ec2.New(sess)
input := &ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-type"),
Values: []*string{aws.String("instance")},
},
{
Name: aws.String("resource-id"),
Values: []*string{aws.String(instance)},
},
},
}
tags := make(map[string]string)
result, err := svc.DescribeTags(input)
if err != nil {
return tags, fmt.Errorf("ec2.DescribeTags failed: %w", err)
}
if result.Tags == nil {
return tags, nil
}
for _, t := range result.Tags {
if t != nil {
k := t.Key
v := t.Value
if k != nil && v != nil {
tags[*k] = *v
}
}
}
return tags, nil
}
func main() {
{
// Method 1: individual metadata calls
region, instance, err := getRegionAndInstance()
if err != nil {
panic(err)
}
fmt.Printf("region = %s instance = %s (method 1)\n", region, instance)
}
{
// Method 2: via instance identity document
region, instance, err := getRegionAndInstance2()
if err != nil {
panic(err)
}
fmt.Printf("region = %s instance = %s (method 2)\n", region, instance)
tags, err := getTags(region, instance)
if err != nil {
panic(err)
}
for k, v := range tags {
fmt.Printf("%s = %s\n", k, v)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment