aws-lambda - localstack 的 Lambda 函数内 DynamoDB 的端点 URL

标签 aws-lambda localstack

我正在使用 localstack 进行本地开发。我有一个名为 readings 的 DynamoDB 表,我想从 lambda 函数插入项目。

我已经在 python 运行时部署了简单的 lambda 函数:

import os
import boto3

def lambda_handler(events, context):
    DYNAMODB_ENDPOINT_URL = os.environ.get("DYNAMODB_ENDPOINT_URL")
    dynamodb = boto3.resource("dynamodb", endpoint_url=DYNAMODB_ENDPOINT_URL)
    readings_table = dynamodb.Table(DYNAMODB_READINGS_TABLE_NAME)

    readings_table.put_item(Item={"reading_id": "10", "other": "test"})

但我收到错误:[ERROR] EndpointConnectionError:无法连接到端点 URL:“http://localstack:4569/”

我尝试了 localhostlocalstack 以及端口:45664569 的组合。他们都失败了。

这是我用来启动 localstack 的 docker-compse 服务

    localstack:
        image: localstack/localstack:0.11.2
        ports:
            - 4566:4566
            - 8080:8080
        environment:
            SERVICES: "dynamodb,sqs,lambda,iam"
            DATA_DIR: "/tmp/localstack/data"
            PORT_WEB_UI: "8080"
            LOCALSTACK_HOSTNAME: localstack
            LAMBDA_EXECUTOR: docker
            AWS_ACCESS_KEY_ID: "test"
            AWS_SECRET_ACCESS_KEY: "test"
            AWS_DEFAULT_REGION: "us-east-1"
        volumes:
            - localstack_volume:/tmp/localstack/data
            - /var/run/docker.sock:/var/run/docker.sock
            # When a container is started for the first time, it will execute files with extensions .sh that are found in /docker-entrypoint-initaws.d. 
            # Files will be executed in alphabetical order. You can easily create aws resources on localstack using `awslocal` (or `aws`) cli tool in the initialization scripts.
            # source: https://github.com/localstack/localstack/pull/1018/files#diff-04c6e90faac2675aa89e2176d2eec7d8R185
            - ./localstack-startup-scripts/:/docker-entrypoint-initaws.d/

我必须在 lambda 中设置的正确端点 URL 是什么,以便我可以将请求发送到 localstack 的 DynamoDB?

最佳答案

根据 the docs LOCALSTACK_HOSTNAME 是一个只读环境变量:

LOCALSTACK_HOSTNAME: Name of the host where LocalStack services are available. Use this hostname as endpoint (e.g., http://${LOCALSTACK_HOSTNAME}:4566) in order to access the services from within your Lambda functions (e.g., to store an item to DynamoDB or S3 from a Lambda).

关于aws-lambda - localstack 的 Lambda 函数内 DynamoDB 的端点 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62196416/

相关文章:

amazon-web-services - 针对 LocalStack 运行 CDK Bootstrap 失败并出现凭据错误

amazon-web-services - 有没有办法查看存储在localstack模拟S3环境中的文件

aws-lambda - 如何使用无服务器更改cloudwatch日志名称?

amazon-web-services - Lambda 返回有效负载 botocore.response.StreamingBody 对象打印但随后在变量中为空

aws-lambda - 警报通知中可用的 AWS Cloudwatch 日志过滤参数

amazon-web-services - 无服务器:根配置错误:无法识别的属性 'stepFunctions'

.net - Docket.DotNet 的 Docker 桌面问题,第二次调用 _dockerClient.Containers.StartContainerAsync() 将失败并出现 OperationCanceledException

c# - AWS Lambda 访问文件

amazon-web-services - 如何保护 AWS API 网关 URL 免受未经授权的访问?

amazon-web-services - Azure 的本地云堆栈类似于 AWS 的 LocalStack?