2

I'm using localstack for local development. I have a DynamoDB table named readings and I'd like to insert items from a lambda function.

I have deployed simple lambda function in python runtime:

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"})

But I'm getting error: [ERROR] EndpointConnectionError: Could not connect to the endpoint URL: "http://localstack:4569/"

I've tried combinations of localhost and localstack along with ports: 4566 and 4569. All of them fail.

Here's my docker-compse service that I use to start localstack

    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/

What would be the correct endpoint url that I have to set inside in my lambda so that I can send requests to localstack's DynamoDB?

3 Answers 3

3

According to the docs LOCALSTACK_HOSTNAME is a read-only env var:

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).

Sign up to request clarification or add additional context in comments.

Comments

0
 try with 
 ports:      
    - "0.0.0.0:4566-4599:4566-4599"  

Hope it helps

2 Comments

Unfortunately no effect, either with endpoint url = localhost:4566 and localstack:4566 - I still get EndpointConnectionError
Did you find any solution for the same ?
0

By following Robert Taylor's answer, I was able to get the following working in my Java Lambda (this should really be a comment in that answer, but code formatting for big snippet is not adequate in comment, so I'm sharing this as a separate answer to make people's life easier):

var url = System.getenv("LOCALSTACK_HOSTNAME");
var credentials = new BasicAWSCredentials("mock_access_key", "mock_secret_key");
var ep = new AwsClientBuilder.EndpointConfiguration(String.format("http://%s:4566",url), "us-east-1");
s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(ep)
        .withPathStyleAccessEnabled(true)
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .build();
dynamoDBMapper = new DynamoDBMapper(
        AmazonDynamoDBClientBuilder.standard()
        .withEndpointConfiguration(ep)
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .build()
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.