dynamodb-examples icon indicating copy to clipboard operation
dynamodb-examples copied to clipboard

ERROR: NoCredentialProviders: no valid providers in chain

Open johnmackenziediningclubgroup opened this issue 5 years ago • 1 comments

Hey, thanks for these examples but I am having a problem when running dynamo in docker

ERROR: NoCredentialProviders: no valid providers in chain caused by: EnvAccessKeyNotFound: failed to find credentials in the environment. SharedCredsLoad: failed to load profile, . EC2RoleRequestError: no EC2 instance role found caused by: RequestError: send request failed caused by: Get http://169.254.169.254/latest/meta-data/iam/security-credentials/: dial tcp 169.254.169.254:80: connect: connection refused

It looks as though the go sdk is looking for some credentials in ~/.aws/credentials. Is there a way I can bypass this? Since I do not require my code to actually speak to AWS but rather a docker container containing the dynamodb image.

How do you recommend that one sets up these examples within docker?

Any help is greatly appreciated.

My code looks like;

func main() {
	fmt.Println("running...")
	t := true
	// create an aws session
	sess := session.Must(session.NewSession(&aws.Config{
		CredentialsChainVerboseErrors: &t,
		Region:   aws.String("us-east-1"),
		Endpoint: aws.String("dynamodb-app:8000"),
		//EndPoint: aws.String("https://dynamodb.us-east-1.amazonaws.com"),
	}))
	fmt.Println("A")
	// create a dynamodb instance
	db := dynamodb.New(sess)

	// movie data
	movie := types.Movie{
		Year:  2015,
		Title: "The Big New Movie",
		Info: types.MovieInfo{
			Plot:   "Nothing happens at all.",
			Rating: 1.1,
		},
	}
	fmt.Println("B")
	// marshal the movie struct into an aws attribute value
	movieAVMap, err := dynamodbattribute.MarshalMap(movie)
	if err != nil {
		panic("Cannot marshal movie into AttributeValue map")
	}
	fmt.Println("C")
	// create the api params
	params := &dynamodb.PutItemInput{
		TableName: aws.String("Movies"),
		Item:      movieAVMap,
	}

	// put the item
	resp, err := db.PutItem(params)
	if err != nil {
		fmt.Printf("ERROR: %v\n", err.Error())
		return
	}

	// print the response data
	fmt.Println("Success")
	fmt.Println(resp)
}

I found a fix for this, make sure you have both AWS env vars set in your app container.

AWS_SECRET_ACCESS_KEY="AWS_SECRET_ACCESS_KEY"
AWS_ACCESS_KEY="AWS_ACCESS_KEY"

The value does not matter, as long as they are set.

I hope this helps someone