Skip to content

Instantly share code, notes, and snippets.

@aerostitch
Created September 18, 2017 22:17
Show Gist options
  • Save aerostitch/f535458d8f465673a6d57c154701312b to your computer and use it in GitHub Desktop.
Save aerostitch/f535458d8f465673a6d57c154701312b to your computer and use it in GitHub Desktop.
This script lists the ec2 instances, their status and tags
package main
// This script lists the ec2 instances, their status and tags
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"log"
)
func computeTags(tagsInput []*ec2.Tag) *string {
res := ""
for _, tag := range tagsInput {
res += *tag.Key + " " + *tag.Value + ";"
}
return &res
}
func main() {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := ec2.New(sess)
filters := []*ec2.Filter{
&ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("running"), aws.String("pending"), aws.String("stopped")},
},
}
result, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{Filters: filters})
if err != nil {
log.Fatalf("Unable to get descriptions for security groups, %v\n", err)
}
for _, reservation := range result.Reservations {
for _, instance := range reservation.Instances {
fmt.Printf("%s,%s,%s\n", *instance.InstanceId, *instance.State.Name, *computeTags(instance.Tags))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment