title: connecting AWS SAM Local with dynamodb in docker
body: <p>I've set up an api gateway/aws lambda pair using AWS sam local and confirmed I can call it successfully after running</p>
  
  <p><code>sam local start-api</code></p>
  
  <p>I've then added a local dynamodb instance in a docker container and created a table on it using the aws cli</p>
  
  <p>But, having added the code to the lambda to write to the dynamodb instance I receive:</p>
  
  <blockquote>
    <p>2018-02-22T11:13:16.172Z  ed9ab38e-fb54-18a4-0852-db7e5b56c8cd    error:
    could not write to table: {"message":"connect ECONNREFUSED
    0.0.0.0:8000","code":"NetworkingError","errno":"ECONNREFUSED","syscall":"connect","address":"0.0.0.0","port":8000,"region":"eu-west-2","hostname":"0.0.0.0","retryable":true,"time":"2018-02-22T11:13:16.165Z"}
    writing event from command:
    {"name":"test","geolocation":"xyz","type":"createDestination"} END
    RequestId: ed9ab38e-fb54-18a4-0852-db7e5b56c8cd</p>
  </blockquote>
  
  <p>I saw online that you might need to connect to the same docker network so I created a network <code>docker network create lambda-local</code> and have changed my start commands to:</p>
  
  <p><code>sam local start-api --docker-network lambda-local</code></p>
  
  <p>and</p>
  
  <p><code>docker run -v "$PWD":/dynamodb_local_db -p 8000:8000 --network=lambda-local  cnadiminti/dynamodb-local:latest</code></p>
  
  <p>but still receive the same error</p>
  
  <p>sam local is printing out <code>2018/02/22 11:12:51 Connecting container 98b19370ab92f3378ce380e9c840177905a49fc986597fef9ef589e624b4eac3 to network lambda-local</code></p>
  
  <p>I'm creating the dynamodbclient using:</p>
  
  <pre><code>const AWS = require('aws-sdk')
  const dynamodbURL = process.env.dynamodbURL || 'http://0.0.0.0:8000'
  const awsAccessKeyId = process.env.AWS_ACCESS_KEY_ID || '1234567'
  const awsAccessKey = process.env.AWS_SECRET_ACCESS_KEY || '7654321'
  const awsRegion = process.env.AWS_REGION || 'eu-west-2'
  
  console.log(awsRegion, 'initialising dynamodb in region: ')
  
  let dynamoDbClient
  const makeClient = () =&gt; {
    dynamoDbClient = new AWS.DynamoDB.DocumentClient({
      endpoint: dynamodbURL,
      accessKeyId: awsAccessKeyId,
      secretAccessKey: awsAccessKey,
      region: awsRegion
    })
    return dynamoDbClient
  }
  
  module.exports = {
    connect: () =&gt; dynamoDbClient || makeClient()
  }
  </code></pre>
  
  <p>and inspecting the dynamodbclient my code is creating shows</p>
  
  <pre><code>DocumentClient {
    options:
     { endpoint: 'http://0.0.0.0:8000',
       accessKeyId: 'my-key',
       secretAccessKey: 'my-secret',
       region: 'eu-west-2',
       attrValue: 'S8' },
    service:
     Service {
       config:
        Config {
          credentials: [Object],
          credentialProvider: [Object],
          region: 'eu-west-2',
          logger: null,
          apiVersions: {},
          apiVersion: null,
          endpoint: 'http://0.0.0.0:8000',
          httpOptions: [Object],
          maxRetries: undefined,
          maxRedirects: 10,
          paramValidation: true,
          sslEnabled: true,
          s3ForcePathStyle: false,
          s3BucketEndpoint: false,
          s3DisableBodySigning: true,
          computeChecksums: true,
          convertResponseTypes: true,
          correctClockSkew: false,
          customUserAgent: null,
          dynamoDbCrc32: true,
          systemClockOffset: 0,
          signatureVersion: null,
          signatureCache: true,
          retryDelayOptions: {},
          useAccelerateEndpoint: false,
          accessKeyId: 'my-key',
          secretAccessKey: 'my-secret' },
       endpoint:
        Endpoint {
          protocol: 'http:',
          host: '0.0.0.0:8000',
          port: 8000,
          hostname: '0.0.0.0',
          pathname: '/',
          path: '/',
          href: 'http://0.0.0.0:8000/' },
       _clientId: 1 },
    attrValue: 'S8' }
  </code></pre>
  
  <p>Should this setup work? How do I get them talking to each other?</p>
  
  <p>---- edit ----</p>
  
  <p>Based on a twitter conversation it's worth mentioning (maybe) that I can interact with dynamodb at the CLI and in the web shell</p>
  
  <p><a href="https://i.stack.imgur.com/Sxpsw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Sxpsw.png" alt="dynamo db at the CLI"></a></p>
  
  <p><a href="https://i.stack.imgur.com/oXDP6.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oXDP6.png" alt="dynamodb web shell"></a></p>