1

I can access DynamoDB on AWS with my code. I can access the local DynamoDB with CLI. But I can't get the two to talk to each other.

# deploying dynamodb
docker run \
    --detach \
    --tty \
    --interactive \
    --publish 8000:8000 \
    --name lokal_dynamodb amazon/dynamodb-local

# deploying Go with SAM
sam local start-api \
    --template sam/template.yaml \
    --region eu-central-1 \
    --profile default

Creating a session

sess, err := session.NewSessionWithOptions(session.Options{
    // Provide SDK Config options, such as Region.
    Config: aws.Config{
        Endpoint: aws.String("http://localhost:8000/"),
        Region:   aws.String("eu-central-1"),
    },
})
if err != nil {
    logger.Println(err)
    return nil
}

Error returned

unction 'ShopFunction' timed out after 20 seconds
Function returned an invalid response (must include one of: body, headers, multiValueHeaders or statusCode in the response object). Response received: 
2019-09-21 21:22:03 127.0.0.1 - - [21/Sep/2019 21:22:03] "GET /shop/f10b7ab5-9508-4cfd-acb2-efb2299dd460 HTTP/1.1" 502 -
James Burke
  • 2,229
  • 1
  • 26
  • 34

1 Answers1

0

For some reason SAM lambdas aren't able to connect to localhost DynamoDB, so you need to create a loopback IP address that they reference to access it.

ifconfig lo0 alias 172.16.123.1

Then update your session configuration to:

sess, err := session.NewSessionWithOptions(session.Options{
    // Provide SDK Config options, such as Region.
    Config: aws.Config{
        Endpoint: aws.String("http://172.16.123.1:8000/"),
        Region:   aws.String("eu-central-1"),
    },
})
if err != nil {
    logger.Println(err)
    return nil
}

https://github.com/awslabs/aws-sam-cli/issues/102#issuecomment-326177151

James Burke
  • 2,229
  • 1
  • 26
  • 34