我已经使用 AWS sam local 设置了一个 api gateway/aws lambda 对,并确认我可以在运行后成功调用它
sam local start-api
然后,我在 docker 容器中添加了一个本地 dynamodb 实例,并使用 aws cli 在其上创建了一个表
但是,将代码添加到 lambda 以写入我收到的 dynamodb 实例:
2018-02-22T11:13:16.172Z ed9ab38e-fb54-18a4-0852-db7e5b56c8cd 错误:无法写入表:{"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"} 从命令写入事件:{"name":"test","geolocation":"xyz"," type":"createDestination"} END RequestId: ed9ab38e-fb54-18a4-0852-db7e5b56c8cd
我在网上看到您可能需要连接到同一个 docker 网络,所以我创建了一个网络docker network create lambda-local
并将启动命令更改为:
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 Connecting container 98b19370ab92f3378ce380e9c840177905a49fc986597fef9ef589e624b4eac3 to network lambda-local
我正在使用以下方法创建 dynamodbclient:
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 节目
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' }
这个设置应该工作吗?我如何让他们互相交谈?
- - 编辑 - -
基于 Twitter 对话,值得一提(也许)我可以在 CLI 和 web shell 中与 dynamodb 交互