I am trying to send emails through Amazon SES in Golang. Previously it was working on a development server (without SSL) with following code:
sess, err := session.NewSession(&aws.Config{
Region:aws.String(AwsRegion)},
)
/* Create an SES client in the session */
svc := ses.New(sess)
/* Assemble the email */
input := &ses.SendEmailInput{
Destination: &ses.Destination{
CcAddresses: []*string{
},
ToAddresses: []*string{
aws.String(request.EmailTo),
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(body),
},
},
Subject: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(subject),
},
},
Source: aws.String("test<test@example.com>"),
}
/* Attempt to send the email */
result, err := svc.SendEmail(input)
Recently I started working on the production server (with SSL) and the same code started giving me following error:
NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
I have tried adding credentials to the code like this:
creds := credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{})
/* updated session block with credentials in aws config */
sess, err := session.NewSession(&aws.Config{
Region: aws.String(AwsRegion),
Credentials: creds})
But this code is not working (not even compiling) and gives me following error:
/main.go:63:42: undefined: ec2rolecreds
However I have already included credentials package in code.
I am not getting how to add credentials to this code so that it works on production server too. Any kind of help will be appreciated.
The first error (NoCredentialProviders) isn't an issue with code but that it can't find any credentials. According to this page it automatically checks in the following order:
If it worked in one environment and not another I'm guessing you had an IAM role with the necessary permissions associated with an EC2 instance via an Instance Profile.
To set credentials explicitly, try this:
staticCreds = credentials.NewStaticCredentials("<key-id>", "<secret>", "<session token>")
session, err = session.NewSession(&aws.Config{Credentials: staticCreds})