Reputation: 117
Is it possible to use profile from aws config files (.aws and configuration) directly in Go app, without setting any environment variables, like AWS_SDK_LOAD_CONFIG, AWS_PROFILE or any other environment variable containing plain text credentials?
region := "xxxxxx"
profile := "xxxxxx"
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(region),
CredentialsChainVerboseErrors: aws.Bool(true)},
Profile: profile,
})
Response is this:
NoCredentialProviders: no valid providers in chain
caused by: EnvAccessKeyNotFound: failed to find credentials in the environment.
SharedCredsLoad: failed to load profile, xxxxxxxxxx.
EC2RoleRequestError: no EC2 instance role found
caused by: RequestError: send request failed
Upvotes: 2
Views: 7922
Reputation: 9675
AS per the documentation yes, you can provide profile
while creating the session.
Credential and config loading order
By default NewSession will only load credentials from the shared credentials file (~/.aws/credentials). If the AWS_SDK_LOAD_CONFIG environment variable is set to a truthy value the Session will be created from the configuration values from the shared config (~/.aws/config) and shared credentials (~/.aws/credentials) files. Using the NewSessionWithOptions with SharedConfigState set to SharedConfigEnable will create the session as if the AWS_SDK_LOAD_CONFIG environment variable was set.
The Environment variables for credentials will have precedence over shared config even if SharedConfig is enabled. To override this behavior, and use shared config credentials instead specify the session.Options.Profile, (e.g. when using credential_source=Environment to assume a role).
sess, err := session.NewSessionWithOptions(session.Options{
Profile: "myProfile",
})
sample code:
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
region := "eu-central-1"
profile := "myprofile"
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(region),
CredentialsChainVerboseErrors: aws.Bool(true)},
Profile: profile,
})
if err != nil {
fmt.Println(err)
}
svc := s3.New(sess)
_, err = svc.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String("myxplbukcet"),
})
if err != nil {
fmt.Println(err.Error())
return
}
}
Upvotes: 4
Reputation: 1780
It looks like Go is trying to use the shared credentials file but it's not able to find the appropriate profile. Can you check on your credentials file and look at the profiles available?
Upvotes: 0