Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MissingRegion: could not find region configuration in golang and aws sns

I am new to Golang and AWS. I am trying to send SMS using AWS SNS. I have set Environment variable First then try send SMS.

export AWS_ACCESS_KEY_ID=AKIAIOSFODN..
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEM..
export AWS_DEFAULT_REGION=us-west-2

I tried to debug where i am getting wrong But always getting error MissingRegion: could not find region configuration

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/sns"
)

func main() {
    fmt.Println("creating session")
    sess := session.Must(session.NewSession())
    fmt.Println("session created")
    svc := sns.New(sess)
    fmt.Println("service created")
    params := &sns.PublishInput{
        Message: aws.String("testing 123"),         
        PhoneNumber: aws.String("+14445556666"),    
    }
    resp, err := svc.Publish(params)

    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Println(resp)
}

I am trying to do this since last 2 days.Please help me where i am going wrong.

like image 560
Amit Upa Avatar asked May 14 '19 17:05

Amit Upa


2 Answers

You have to configure the SDK. To set just the region you would do something like

sess, err := session.NewSession(&aws.Config{
    Region: aws.String("us-west-2")},
)

You can see full details on config here: https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html

like image 186
noisewaterphd Avatar answered Nov 15 '22 08:11

noisewaterphd


The Go SDK does not recognize the env variable AWS_DEFAULT_REGION
The region can be specified using the AWS_REGION name as below

export AWS_REGION="us-west-2"

The other way is to explicitly pass the region while creating the session as per the answer by noisewaterphd

like image 8
Pratham Avatar answered Nov 15 '22 08:11

Pratham