AWS SDK for Go
Developer Guide

Using Regions and Availability Zones with Amazon EC2

These Go examples show you how to retrieve details about AWS Regions and Availability Zones.

An Amazon EC2 security group acts as a virtual firewall that controls the traffic for one or more instances. You add rules to each security group to allow traffic to or from its associated instances. You can modify the rules for a security group at any time; the new rules are automatically applied to all instances that are associated with the security group.

The code in this example uses the AWS SDK for Go to perform these tasks by using these methods of the Amazon EC2 client class:

You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.


Amazon EC2 is hosted in multiple locations worldwide. These locations are composed of AWS Regions and Availability Zones. Each region is a separate geographic area with multiple, isolated locations known as Availability Zones. Amazon EC2 provides the ability to place instances and data in these multiple locations.

In this example, you use Go code to retrieve details about regions and Availability Zones. The code uses the AWS SDK for Go tomanage instances by using the following methods of the Amazon EC2 client class:


List the Groups

This example describes the security groups by IDs that are passed in to the routine. It takes a space separated list of group IDs as input.

To get started, create a new Go file named regions_and_availability.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

package main import ( "fmt" "" "" )

In the main function, create a session with credentials from the shared credentials file, ~/.aws/credentials, and create a new EC2 client.

func main() { // Load session from shared config sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create new EC2 client svc := ec2.New(sess)

Print out the list of regions that work with Amazon EC2 that are returned by calling DescribeRegions.

resultRegions, err := svc.DescribeRegions(nil) if err != nil { fmt.Println("Error", err) return }

Add a call that retrieves Availability Zones only for the region of the EC2 service object.

resultAvalZones, err := svc.DescribeAvailabilityZones(nil) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", resultAvalZones.AvailabilityZones) }