Created
November 5, 2018 22:55
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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