Skip to content

Instantly share code, notes, and snippets.

@brydavis
Created November 5, 2018 22:55
Show Gist options
  • Save brydavis/5c4d090812a81f712c6048a4e006f689 to your computer and use it in GitHub Desktop.
Save brydavis/5c4d090812a81f712c6048a4e006f689 to your computer and use it in GitHub Desktop.
Get the account ID for an AWS account (assuming that credentials file is present)
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)
func main() {
svc := sts.New(session.New())
input := &sts.GetCallerIdentityInput{}
result, err := svc.GetCallerIdentity(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
account := *result.Account
fmt.Println(account)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment