我已经使用AWS sam本地设置了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 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网络创建lambda-local并将我的start命令更改为:
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 is print out 2018/02/22 11:12:51将容器98b19370ab92f3378ce380e9c840177905a49fc986597fef9ef589e624b4eac3连接到网络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],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,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进行交互
options.endpoint =“http://docker.for.mac.localhost:8000”
或者在较新的docker https://docs.docker.com/docker-for-mac/release-notes/#docker-community-edition-18030-ce-mac59-2018-03-26上安装
options.endpoint =“http://host.docker.internal:8000”
而不是像上面保罗所做的那样做多个命令(但这可能更加平台无关?).