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
+6.5k Golang : Reverse by word
+6.3k Elasticsearch : Shutdown a local node
+20.8k Golang : Clean up null characters from input data
+9.1k Mac OSX : Get a process/daemon status information
+8.5k Golang : Heap sort example
+4.6k Fix Google Analytics Redundant Hostnames problem
+5.1k Golang : Return multiple values from function
+10.6k PHP : Convert(cast) bigInt to string
+7.4k Golang : Mapping Iban to Dunging alphabets
+10.3k Golang : ISO8601 Duration Parser example