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.6k Golang : Find smallest number in array
+7k Nginx : Password protect a directory/folder
+36.4k Golang : Convert(cast) int64 to string
+14k Golang : How to determine if a year is leap year?
+8.3k Golang : Check if integer is power of four example
+16.3k Golang : How to extract links from web page ?
+25.4k Golang : Get current file path of a file or executable
+10.1k Golang : Read file and convert content to string
+15.7k Golang : Get checkbox or extract multipart form data value example
+18.6k Golang : Generate thumbnails from images
+16.3k Golang : Loop each day of the current month example
+8.8k Golang : Heap sort example