AWS SAM中Lambda无法连接本地DynamoDB问题求助
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:
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-netStart DynamoDB with
docker-compose up -d.Launch SAM Local with the shared network:
sam local start-api --docker-network sam-shared-netUpdate 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 tohttp://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):
Find your host's LAN IP:
- On macOS/Linux: Run
ifconfigorip addrand look for an IP like192.168.x.x - On Windows: Run
ipconfigand find the IPv4 address of your active network adapter
- On macOS/Linux: Run
Update your template.yaml to use this IP as the DynamoDB endpoint, e.g.:
Environment: Variables: DYNAMODB_ENDPOINT: http://192.168.1.100:8000/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:
- Find the Lambda container ID with
docker ps - Run:
You should get a response from DynamoDB (even if it's an error about invalid request syntax, that means the connection succeeded).docker exec -it <container-id> curl http://dynamodb-local:8000/ # or your host IP
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

