Skip to content

bug: Multiple Lambdas, AWS region not set for 2nd #5788

Closed as not planned
Closed as not planned
@ShedPlant

Description

@ShedPlant

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When deploying more than one (python zip) lambda to localstack, that both create boto3 SQS clients, the first one is fine but deploying the second one fails with

botocore.exceptions.ClientError: An error occurred (InternalFailure) when calling the CreateFunction operation: Unable to get handler function from lambda code: You must specify a region.

Expected Behavior

Both lambdas to deploy fine, with AWS region set by localstack

How are you starting LocalStack?

With a docker-compose file

Steps To Reproduce

My setup (simplified)

docker-compose.yml

version: '3.8'

services:

  localstack:
    privileged: true
    image: localstack/localstack
    ports:
      - "4566:4566"
    environment:
      - LOCALSTACK_SERVICES=sqs,lambda
      - LOCALSTACK_HOSTNAME=localhost
      - DEFAULT_REGION=eu-west-1

Define two simple Python lambda handlers which connect to SQS with boto3:

import os
import boto3

AWS_ENDPOINT_URL = os.environ.get("AWS_ENDPOINT_URL")
sqs = boto3.resource("sqs", endpoint_url=AWS_ENDPOINT_URL)

def lambda_handler(event, context):
    return "first lambda ok"
import os
import boto3

AWS_ENDPOINT_URL = os.environ.get("AWS_ENDPOINT_URL")
sqs = boto3.resource("sqs", endpoint_url=AWS_ENDPOINT_URL)

def lambda_handler(event, context):
    return "second lambda ok"

Script to deploy two Lambdas to localstack:

    import boto3

    localstack_url = f"http://127.0.0.1:{EnvConfig.LOCALSTACK_PORT}"

    # zip already created either manually or in other script, irrelevant for this bug
    with open(_ZIP_FILENAME, "rb") as f:
        zipped_code = f.read()

    lambda_client = boto3.client("lambda", endpoint_url=localstack_url)
    lambda_client.create_function(
        FunctionName="first_lambda",
        Runtime="python3.9",
        Role="not_applicable",
        Handler="first_lambda.lambda_handler.lambda_handler",
        Code={"ZipFile": zipped_code},
        PackageType="Zip",
        Publish=True,
        Environment={
            "Variables": {
                "AWS_ENDPOINT_URL": localstack_url,
            }
        },
    )

    print("first lambda deployed ok")

    lambda_client.create_function(
        FunctionName="second_lambda",
        Runtime="python3.9",
        Role="not_applicable",
        Handler="second_lambda.lambda_handler.lambda_handler",
        Code={"ZipFile": zipped_code},
        PackageType="Zip",
        Publish=True,
        Environment={
            "Variables": {
                "AWS_ENDPOINT_URL": localstack_url,
            }
        },
    )

    # execution never gets this far
    print("second lambda deployed ok")

Environment

- OS: MacOS Monterey 12.2.1
- LocalStack: Latest

Anything else?

As a workaround, I can set AWS_DEFAULT_REGION environment variable explicitly in all my lambdas:

        Environment={
            "Variables": {
                "AWS_ENDPOINT_URL": localstack_url,
                "AWS_DEFAULT_REGION": "eu-west-1",
            }

But this shouldn't be necessary:

  • localstack already has a region set
  • it managed to set it for the 1st lambda
  • I wouldn't need to explicitly set AWS_DEFAULT_REGION env var when running the lambda in real AWS.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions