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
+11.7k Golang : Sort and reverse sort a slice of runes
+6.5k How to let Facebook Login button redirect to a particular URL ?
+5.1k Golang : Reclaim memory occupied by make() example
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+10.4k Golang : Flip coin example
+13.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+34.9k Golang : Strip slashes from string example
+8.7k Golang : Inject/embed Javascript before sending out to browser example
+9.2k Golang : How to extract video or image files from html source code
+18.2k Golang : Example for RSA package functions
+6.5k Golang : Calculate pivot points for a cross
+14.9k Golang : How to add color to string?