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
+17.8k Golang : Iterate linked list example
+39.1k Golang : How to iterate over a []string(array)
+24.1k Golang : Find biggest/largest number in array
+10.5k Generate Random number with math/rand in Go
+17.7k Golang : [json: cannot unmarshal object into Go value of type]
+26.6k Golang : Encrypt and decrypt data with AES crypto
+12.2k Golang : Detect user location with HTML5 geo-location
+10k Golang : Channels and buffered channels examples
+62.7k Golang : Convert HTTP Response body to string
+15.2k Golang : How to check if IP address is in range
+26.9k Golang : Find files by extension
+7.7k Golang : Command line ticker to show work in progress