You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS SAM中Lambda无法连接本地DynamoDB问题求助

Fixing Lambda-DynamoDB Connection Refusal in Local AWS SAM Environment

I've run into this exact issue before when working with local SAM and DynamoDB—let's break down what's happening and how to fix it.

The error message makes it clear: your Lambda function is trying to connect to http://127.0.0.1:8000/ and getting a connection refused. Here's why that's failing, and the steps to resolve it:

Core Problem: Network Isolation Between Lambda Container and Local DynamoDB

When you run sam local, SAM spins up your Lambda function in a separate Docker container. The 127.0.0.1 inside that container refers to the container itself, not your host machine. Even though you can access DynamoDB from your host, the Lambda container can't reach it via localhost unless you configure the network correctly.

Solution 1: Use a Shared Docker Network (For Dockerized DynamoDB)

If you're running DynamoDB via Docker Compose, the easiest fix is to put both DynamoDB and your SAM Lambda containers on the same custom network:

  1. Update your Docker Compose file to define a shared network:

    version: '3.8'
    networks:
      sam-shared-net:
        driver: bridge
    
    services:
      dynamodb-local:
        image: amazon/dynamodb-local:latest
        ports:
          - "8000:8000"
        networks:
          - sam-shared-net
    
  2. Start DynamoDB with docker-compose up -d.

  3. Launch SAM Local with the shared network:

    sam local start-api --docker-network sam-shared-net
    
  4. Update your template.yaml to use the DynamoDB container name as the endpoint instead of 127.0.0.1:
    Set the environment variable for DynamoDB endpoint to http://dynamodb-local:8000/ (match the service name in your Compose file).

Solution 2: Use Your Host Machine's LAN IP (For JAR-Based DynamoDB)

If you're running DynamoDB via the standalone JAR on your host, you need to point the Lambda function to your host's actual LAN IP address (not localhost):

  1. Find your host's LAN IP:

    • On macOS/Linux: Run ifconfig or ip addr and look for an IP like 192.168.x.x
    • On Windows: Run ipconfig and find the IPv4 address of your active network adapter
  2. Update your template.yaml to use this IP as the DynamoDB endpoint, e.g.:

    Environment:
      Variables:
        DYNAMODB_ENDPOINT: http://192.168.1.100:8000/
    
  3. Ensure your host firewall allows incoming connections on port 8000 from Docker containers.

Verify the Fix

To confirm connectivity, you can exec into the running Lambda container and test access to DynamoDB:

  1. Find the Lambda container ID with docker ps
  2. Run:
    docker exec -it <container-id> curl http://dynamodb-local:8000/ # or your host IP
    
    You should get a response from DynamoDB (even if it's an error about invalid request syntax, that means the connection succeeded).

Additional Check: AWS SDK v2 Configuration

Make sure your Go code is correctly using the endpoint from the environment variable. With aws-sdk-go-v2, you can load the endpoint via the AWS_ENDPOINT_URL_DYNAMODB environment variable automatically, or explicitly set it in your config:

import (
  "context"
  "os"

  "github.com/aws/aws-sdk-go-v2/aws"
  "github.com/aws/aws-sdk-go-v2/config"
)

func getDynamoDBConfig() (aws.Config, error) {
  endpoint := os.Getenv("DYNAMODB_ENDPOINT")
  cfg, err := config.LoadDefaultConfig(context.TODO(),
    config.WithRegion("us-east-1"),
    config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(
      func(service, region string, options ...interface{}) (aws.Endpoint, error) {
        if endpoint != "" {
          return aws.Endpoint{URL: endpoint}, nil
        }
        // Fallback to default endpoint if not set
        return aws.Endpoint{}, nil
      })),
  )
  return cfg, err
}

Once you make these changes, re-run make sam-local and make curl-get-website-abc—the connection should work, and your Lambda should be able to fetch data from DynamoDB.

内容的提问来源于stack exchange,提问作者Peter Aba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 21:32:36