Golang : List available AWS regions
Problem :
You need to get ALL the available AWS regions accessible by you. How to do that in Golang ?
Solution :
Use the EC2.DescribeRegions() function to get the list of available AWS regions. You can see a full list of ALL the AWS regions at http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region
Here you go. Happy Coding!
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,
})
regions, err := ec2client.DescribeRegions(&ec2.DescribeRegionsInput{})
if err != nil {
panic(err)
}
// see https://godoc.org/github.com/aws/aws-sdk-go/service/ec2#Region
for _, region := range regions.Regions {
fmt.Println("Region Name : ", *region.RegionName)
fmt.Println("Region Endpoint : ", *region.Endpoint)
}
}
Sample output :
Region Name : eu-central-1
Region Endpoint : ec2.eu-central-1.amazonaws.com
Region Name : sa-east-1
Region Endpoint : ec2.sa-east-1.amazonaws.com
Region Name : ap-northeast-1
Region Endpoint : ec2.ap-northeast-1.amazonaws.com
Region Name : eu-west-1
Region Endpoint : ec2.eu-west-1.amazonaws.com
Region Name : us-east-1
Region Endpoint : ec2.us-east-1.amazonaws.com
Region Name : us-west-1
Region Endpoint : ec2.us-west-1.amazonaws.com
Region Name : us-west-2
Region Endpoint : ec2.us-west-2.amazonaws.com
Region Name : ap-southeast-2
Region Endpoint : ec2.ap-southeast-2.amazonaws.com
Region Name : ap-southeast-1
Region Endpoint : ec2.ap-southeast-1.amazonaws.com
References :
https://godoc.org/github.com/aws/aws-sdk-go/service/ec2#EC2.DescribeRegions
http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region
See also : Golang : List running EC2 instances and descriptions
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
+77.6k Golang : How to return HTTP status code?
+24.1k Golang : GORM read from database example
+19.4k Golang : Measure http.Get() execution time
+21k Curl usage examples with Golang
+19.8k Golang : How to get struct tag and use field name to retrieve data?
+13.2k Golang : Read XML elements data with xml.CharData example
+10.6k Golang : How to transmit update file to client by HTTP request example
+32k Golang : Copy directory - including sub-directories and files
+13.7k Golang : concatenate(combine) strings
+19.4k Golang : Archive directory with tar and gzip
+10.2k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+14.3k Golang : Execute function at intervals or after some delay