Skip to content

Lambda: Docker Containers not removed #8616

@manuelwaltschek

Description

@manuelwaltschek

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I am using localstack image 2.1.0 with testcontainers in version 1.18.0 in my spring boot integration tests.

I implemented the localstack container with a singleton pattern.

    public static LocalStackContainer getContainer() {
        if (localStackContainer == null) {
            DockerImageName image = DockerImageName.parse("localstack/localstack:2.1.0");
            localStackContainer =
                    new LocalStackContainer(image)
                            .withServices(SNS, SQS)
                            .withServices(LAMBDA)
                            .withEnv("DEBUG", "1")
                            .withEnv("LAMBDA_REMOVE_CONTAINERS", "1")
                            .withLogConsumer(new Slf4jLogConsumer(log));
        }
        return localStackContainer;
    }

Note the LAMBDA_REMOVE_CONTAINERS option

When I run my tests the lambda containers are not discarded when they finish. How so? Could not find anything meaningful in the logs.

Expected Behavior

Containers are removed when the JVM exits (when the test is finished), or when the lambda function is deleted.

How are you starting LocalStack?

Custom (please describe below)

Steps To Reproduce

How are you starting localstack (e.g., bin/localstack command, arguments, or docker-compose.yml)

public class LocalStackITHelper implements InitializingBean {
...
       private static void ensureContainerStarted() {
            LocalStackContainer container = getContainer();
            if (!container.isRunning()) {
                container.start();
            }
        }
 ...       
}

Client commands (e.g., AWS SDK code snippet, or sequence of "awslocal" commands)

I have the following methods in LambdaService to create and delete functions:

@SneakyThrows
    public String createLambdaFunction(
            String functionName,
            @NonNull Resource resource,
            String role) {
        LambdaWaiter waiter = awsLambda.waiter();
        SdkBytes fileToUpload = SdkBytes.fromInputStream(resource.getInputStream());

        FunctionCode code = FunctionCode.builder()
                .zipFile(fileToUpload)
                .build();

        String handler = functionName + ".handler";
        CreateFunctionRequest functionRequest = CreateFunctionRequest.builder()
                .functionName(functionName)
                .description("Created by the Lambda Java API")
                .code(code)
                .handler(handler)
                .runtime(Runtime.NODEJS16_X)
                .role(role)
                .build();

        // Create a Lambda function using a waiter.
        CreateFunctionResponse functionResponse = awsLambda.createFunction(functionRequest);
        GetFunctionRequest getFunctionRequest = GetFunctionRequest.builder()
                .functionName(functionName)
                .build();
        WaiterResponse<GetFunctionResponse> waiterResponse = waiter.waitUntilFunctionExists(getFunctionRequest);
        waiterResponse.matched().response().ifPresent(response -> log.info(response.toString()));
        return functionResponse.functionArn();

    }

    public boolean deleteLambdaFunction(String functionName) {
        DeleteFunctionRequest request = DeleteFunctionRequest.builder()
                .functionName(functionName)
                .build();

        LambdaWaiter waiter = awsLambda.waiter();
        DeleteFunctionResponse response = awsLambda.deleteFunction(request);

        log.info("The " + functionName + " function was deleted");
        return response.sdkHttpResponse().isSuccessful();
    }

Environment

- OS: MacOS Monterey 12.4
- LocalStack: 2.1.0

Edit: I added the configuration

.withEnv("LAMBDA_KEEPALIVE_MS", "10000")

This seems to stop and then discard the containers after 10 seconds.

I have some new questions:
What effect does "deleting" lambdas have? I assumed it will also delete the running containers.
If not, is there any way to associate the lambda container with the parent localstack container so that I can clean up by myself on shutdown?

Metadata

Metadata

Assignees

Labels

aws:lambdaAWS Lambdastatus: backlogTriaged but not yet being worked ontype: featureNew feature, or improvement to an existing feature

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions