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
+14.1k Golang : Get query string value on a POST request
+12.8k Golang : Simple word wrap or line breaking example
+6.2k Golang : How to handle file size larger than available memory panic issue
+9.3k Generate Random number with math/rand in Go
+3.6k Adding Skype actions such as call and chat into web page examples
+16.4k Convert JSON to CSV in Golang
+3.9k Javascript : How to get width and height of a div?
+7.6k Golang : Gaussian blur on image and camera video feed examples
+7.7k Golang : Random integer with rand.Seed() within a given range
+5k Golang : Calculate US Dollar Index (DXY)
+8.3k Golang : How to generate Code 39 barcode?
+17.3k Golang : Check whether a network interface is up on your machine