Golang : List running EC2 instances and descriptions



Tags : golang ec2 aws-sdk-go

Problem :

You need to list the running EC2 instance and get description for each instances. You also want to know how to access the descriptions of the instance. How to do that in Golang ?

Solution :

Use the EC2.DescribeInstaces() function to get the list of available instances in a particular region. In this example, we use the eu-west-1 region as example.

Here you go :

 package main

 import (
 "fmt"
 "github.com/aws/aws-sdk-go/aws"
 "github.com/aws/aws-sdk-go/aws/credentials"
 "github.com/aws/aws-sdk-go/service/ec2"
 )

 func main() {

 aws_access_key_id := "<replace with yours>"
 aws_secret_access_key := "<replace with yours>"

 // If you're working with temporary security credentials,
 // you can also keep the session token in AWS_SESSION_TOKEN.

 token := ""

 creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)

 ec2client := ec2.New(&aws.Config{
 Region: "eu-west-1", // get from your AWS console, click "Properties"
 Credentials: creds,
 LogLevel: 0,
 })


 resp, err := ec2client.DescribeInstances(nil)

 if err != nil {
 panic(err)
 }

 // resp has all of the response data, pull out instance IDs:
 fmt.Println("> Number of reservation sets: ", len(resp.Reservations))
 for idx, res := range resp.Reservations {
 fmt.Println("  > Number of instances: ", len(res.Instances))

 // see https://godoc.org/github.com/awslabs/aws-sdk-go/service/ec2#Instance

 for _, inst := range resp.Reservations[idx].Instances {
 fmt.Println(" - Instance ID: ", *inst.InstanceID)
 fmt.Println(" - Instance Arch: ", *inst.Architecture)
 fmt.Println(" - Image ID: ", *inst.ImageID)
 fmt.Println(" - Instance Monitoring: ", *inst.Monitoring.State)
 //fmt.Println(" - Instance Platform: ", *inst.Platform) <-- only work for Windows instance. Need to check if inst.Platform is blank, then continue

 fmt.Println(" - Instance Placement: ", *inst.Placement.AvailabilityZone)
 fmt.Println(" - Instance PublicIPAddress: ", *inst.PublicIPAddress)
 fmt.Println(" - Instance State: ", *inst.State.Name)

 for _, tag := range inst.Tags {
 fmt.Println(" - Instance Tag Key: ", *tag.Key)
 fmt.Println(" - Instance Tag Value: ", *tag.Value)
 }
 }
 }

 }

Sample output :

> Number of reservation sets: 1

> Number of instances: 1

- Instance ID: i-aaf61a4c

- Instance Arch: x86_64

- Image ID: ami-47a23a30

- Instance Monitoring: disabled

- Instance Placement: eu-west-1a

- Instance PublicIPAddress: xx.xx.xxx.7

- Instance State: running

- Instance Tag Key: ec2

- Instance Tag Value: golang

References :

https://godoc.org/github.com/aws/aws-sdk-go/service/ec2#EC2.DescribeInstances

https://godoc.org/github.com/aws/aws-sdk-go/service/ec2#EC2.DescribeRegions

Affiliate :

Hire Golang Developers


  See also : Golang : Setting up/configure AWS credentials with official aws-sdk-go



Tags : golang ec2 aws-sdk-go

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement