Golang : Setting up/configure AWS credentials with official aws-sdk-go
Problem :
You need to setup credentials with official AWS SDK and you have couple of questions :
How to read from ~/.aws/credential file?
How to read from environment variables?
How to use the aws_access_key_id
and aws_secret_access_key
inside your code instead from environment variables or file?
Solutions :
Read from ~/.aws/credential file example :
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"
"os"
)
func main() {
// if you have ~/.aws/credentials file, the SDK will find and use the file automagically
// see http://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs
// err... as of May 22nd 2015, it doesn't work for Golang :(
// therefore, you still have to manually tell it where to find and load which profile
// the file location and load default profile
creds := credentials.NewSharedCredentials("/Users/sweetlogic/.aws/credentials", "default")
credValue, err := creds.Get()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Value of credentials : ", credValue)
fmt.Println("----------------------------------------------")
fmt.Println("Raw credentials : ", creds)
// test if the credentials configuration works or not
ec2client := ec2.New(&aws.Config{
Region: "us-west-2", // get from your AWS console, click "Properties"
Credentials: creds,
LogLevel: 1,
})
// Call the DescribeInstances Operation
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))
for _, inst := range resp.Reservations[idx].Instances {
fmt.Println(" - Instance ID: ", *inst.InstanceID)
}
}
}
Read from environment variable example :
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"
"os"
)
func main() {
// grab from the environment variables.
// environment variables names must be uppercase?
// export AWS_ACCESS_KEY_ID=your access key id
// export AWS_SECRET_ACCESS_KEY=your secret key
creds := credentials.NewEnvCredentials()
credValue, err := creds.Get()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Value of credentials : ", credValue)
fmt.Println("----------------------------------------------")
fmt.Println("Raw credentials : ", creds)
// test if the credentials configuration works or not
ec2client := ec2.New(&aws.Config{
Region: "us-west-2", // get from your AWS console, click "Properties"
Credentials: creds,
LogLevel: 1,
})
// Call the DescribeInstances Operation
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))
for _, inst := range resp.Reservations[idx].Instances {
fmt.Println(" - Instance ID: ", *inst.InstanceID)
}
}
}
Read from code example :
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"
"os"
)
func main() {
aws_access_key_id := "your access key id"
aws_secret_access_key := "your secret key"
// 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)
v, err := creds.Get()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Value of credentials : ", v)
fmt.Println("----------------------------------------------")
fmt.Println("Raw credentials : ", creds)
ec2client := ec2.New(&aws.Config{
Region: "us-west-2", // get from your AWS console, click "Properties"
Credentials: creds,
LogLevel: 1,
})
// Call the DescribeInstances Operation
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))
for _, inst := range resp.Reservations[idx].Instances {
fmt.Println(" - Instance ID: ", *inst.InstanceID)
}
}
}
NOTE : AWS Golang SDK is still undergoing radical changes by the time of writing and there are possibilities that these examples might be obsolete in future. Till then happy coding!
References :
http://godoc.org/github.com/aws/aws-sdk-go/aws/credentials#NewEnvCredentials
http://godoc.org/github.com/aws/aws-sdk-go/aws/credentials#NewSharedCredentials
http://godoc.org/github.com/aws/aws-sdk-go/aws/credentials#NewStaticCredentials
See also : Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
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
+35.7k Golang : Save image to PNG, JPEG or GIF format.
+7.9k Android Studio : Rating bar example
+6.2k Golang : Break string into a slice of characters example
+30.1k Get client IP Address in Go
+7.6k Javascript : How to check a browser's Do Not Track status?
+8.5k Android Studio : Image button and button example
+18.6k Golang : Padding data for encryption and un-padding data for decryption
+7.3k Golang : Handling Yes No Quit query input
+6.3k PHP : Shuffle to display different content or advertisement
+5.5k List of Golang XML tutorials
+4.7k Nginx and PageSpeed build from source CentOS example
+9.7k Golang : Translate language with language package example