Golang : List running EC2 instances and descriptions
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
See also : Golang : Setting up/configure AWS credentials with official 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
Tutorials
+17.8k Golang : Read data from config file and assign to variables
+3.8k Java : Random alphabets, alpha-numeric or numbers only string generator
+11.7k SSL : The certificate is not trusted because no issuer chain was provided
+11.1k Golang : Replace a parameter's value inside a configuration file example
+33.7k Golang : How to check if slice or array is empty?
+20.4k Golang : How to get own program name during runtime ?
+7.1k Golang : Find the shortest line of text example
+15.5k Golang : invalid character ',' looking for beginning of value
+7.3k CloudFlare : Another way to get visitor's real IP address
+39.2k Golang : How to iterate over a []string(array)
+12.1k Golang : Decompress zlib file example
+9.7k Javascript : Read/parse JSON data from HTTP response