将 AWS SAM Local 与 docker 中的 dynamodb 连接起来 [英] connecting AWS SAM Local with dynamodb in docker

查看:27
本文介绍了将 AWS SAM Local 与 docker 中的 dynamodb 连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 AWS sam local 设置了一个 api 网关/aws lambda 对,并确认我可以在运行后成功调用它

I've set up an api gateway/aws lambda pair using AWS sam local and confirmed I can call it successfully after running

sam local start-api

然后我在 docker 容器中添加了一个本地 dynamodb 实例,并使用 aws cli 在其上创建了一个表

I've then added a local dynamodb instance in a docker container and created a table on it using the aws cli

但是,将代码添加到 lambda 以写入我收到的 dynamodb 实例:

But, having added the code to the lambda to write to the dynamodb instance I receive:

2018-02-22T11:13:16.172Z ed9ab38e-fb54-18a4-0852-db7e5b56c8cd 错误:无法写入表:{"message":"connect ECONNREFUSED0.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","re​​tryable":true,"time":"2018-02-22T11:13:16.165Z"}从命令写入事件:{"name":"test","geolocation":"xyz","type":"createDestination"} END请求 ID:ed9ab38e-fb54-18a4-0852-db7e5b56c8cd

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

我在网上看到你可能需要连接到同一个 docker 网络,所以我创建了一个网络 docker network create lambda-local 并将我的启动命令更改为:

I saw online that you might need to connect to the same docker network so I created a network docker network create lambda-local and have changed my start commands to:

sam local start-api --docker-network lambda-local

docker run -v "$PWD":/dynamodb_local_db -p 8000:8000 --network=lambda-local cnadiminti/dynamodb-local:latest

但仍然收到相同的错误

sam local 正在打印 2018/02/22 11:12:51 将容器 98b19370ab92f3378ce380e9c840177905a49fc986597fef9ef589e624b4eac3 连接到网络

sam local is printing out 2018/02/22 11:12:51 Connecting container 98b19370ab92f3378ce380e9c840177905a49fc986597fef9ef589e624b4eac3 to network lambda-local

我正在使用以下方法创建 dynamodbclient:

I'm creating the dynamodbclient using:

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 = () => {
  dynamoDbClient = new AWS.DynamoDB.DocumentClient({
    endpoint: dynamodbURL,
    accessKeyId: awsAccessKeyId,
    secretAccessKey: awsAccessKey,
    region: awsRegion
  })
  return dynamoDbClient
}

module.exports = {
  connect: () => dynamoDbClient || makeClient()
}

并检查我的代码正在创建的 dynamodbclient

and inspecting the dynamodbclient my code is creating shows

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' }

这个设置应该有效吗?我如何让他们互相交谈?

Should this setup work? How do I get them talking to each other?

---- 编辑----

---- edit ----

基于 Twitter 对话,值得一提的是(也许)我可以在 CLI 和 web shell 中与 dynamodb 进行交互

Based on a twitter conversation it's worth mentioning (maybe) that I can interact with dynamodb at the CLI and in the web shell

推荐答案

非常感谢 Heitor Lessa 回答我在 Twitter 上有一个示例存储库

Many thanks to Heitor Lessa who answered me on Twitter with an example repo

这给了我答案...

  • dynamodb 的 docker 容器在我的上下文中位于 127.0.0.1机器(这就是我可以与之交互的原因)

  • dynamodb's docker container is on 127.0.0.1 from the context of my machine (which is why I could interact with it)

SAM 本地的 docker 容器在我的上下文中位于 127.0.0.1机器

SAM local's docker container is on 127.0.0.1 from the context of my machine

但是他们不在彼此的上下文中的 127.0.0.1

But they aren't on 127.0.0.1 from each other's context

所以:https://github.com/heitorlessa/sam-local-python-hot-reloading/blob/master/users/users.py#L14

指出我将连接代码更改为:

Pointed me at changing my connection code to:

const AWS = require('aws-sdk')
const awsRegion = process.env.AWS_REGION || 'eu-west-2'

let dynamoDbClient
const makeClient = () => {
  const options = {
    region: awsRegion
  }
  if(process.env.AWS_SAM_LOCAL) {
    options.endpoint = 'http://dynamodb:8000'
  }
  dynamoDbClient = new AWS.DynamoDB.DocumentClient(options)
  return dynamoDbClient
}

module.exports = {
  connect: () => dynamoDbClient || makeClient()
}

重要的几行是:

if(process.env.AWS_SAM_LOCAL) {
  options.endpoint = 'http://dynamodb:8000'
}

从 SAM 本地 docker 容器的上下文中,dynamodb 容器通过其名称公开

我的两个启动命令最终为:

My two startup commands ended up as:

docker run -d -v "$PWD":/dynamodb_local_db -p 8000:8000 --network lambda-local --name dynamodb cnadiminti/dynamodb-local

AWS_REGION=eu-west-2 sam local start-api --docker-network lambda-local

这里唯一的变化是为 dynamodb 容器命名

with the only change here being to give the dynamodb container a name

这篇关于将 AWS SAM Local 与 docker 中的 dynamodb 连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆