Skip to content

Commit b31edb8

Browse files
authored
Fix lambda container is dropped by ryuk (#848) (#922)
Co-authored-by: julienboulay <[email protected]>
1 parent 7f8f97c commit b31edb8

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

packages/modules/localstack/src/localstack-container.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { LOCALSTACK_PORT, LocalstackContainer } from "./localstack-container";
2-
import { HeadBucketCommand, S3Client, CreateBucketCommand } from "@aws-sdk/client-s3";
1+
import { CreateBucketCommand, HeadBucketCommand, S3Client } from "@aws-sdk/client-s3";
32
import { GenericContainer, log, Network, StartedTestContainer } from "testcontainers";
3+
import { LABEL_TESTCONTAINERS_SESSION_ID } from "testcontainers/src/utils/labels";
4+
import { LocalstackContainer, LOCALSTACK_PORT } from "./localstack-container";
45

56
const runAwsCliAgainstDockerNetworkContainer = async (
67
command: string,
@@ -103,4 +104,13 @@ describe("LocalStackContainer", () => {
103104

104105
await container.stop();
105106
});
107+
108+
it("should add LAMBDA_DOCKER_FLAGS with sessionId label", async () => {
109+
const container = await new LocalstackContainer().start();
110+
const sessionId = container.getLabels()[LABEL_TESTCONTAINERS_SESSION_ID];
111+
112+
const { output, exitCode } = await container.exec(["printenv", "LAMBDA_DOCKER_FLAGS"]);
113+
expect(exitCode).toBe(0);
114+
expect(output).toContain(`${LABEL_TESTCONTAINERS_SESSION_ID}=${sessionId}`);
115+
});
106116
});

packages/modules/localstack/src/localstack-container.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import { AbstractStartedContainer, GenericContainer, log, StartedTestContainer, Wait } from "testcontainers";
1+
import {
2+
AbstractStartedContainer,
3+
GenericContainer,
4+
getContainerRuntimeClient,
5+
log,
6+
StartedTestContainer,
7+
Wait,
8+
} from "testcontainers";
9+
import { getReaper } from "testcontainers/src/reaper/reaper";
10+
import { LABEL_TESTCONTAINERS_SESSION_ID } from "testcontainers/src/utils/labels";
211

312
export const LOCALSTACK_PORT = 4566;
413

514
export class LocalstackContainer extends GenericContainer {
615
constructor(image = "localstack/localstack:2.2.0") {
716
super(image);
17+
818
this.withExposedPorts(LOCALSTACK_PORT).withWaitStrategy(Wait.forLogMessage("Ready", 1)).withStartupTimeout(120_000);
919
}
1020

@@ -25,8 +35,19 @@ export class LocalstackContainer extends GenericContainer {
2535
log.info(`${envVar} environment variable set to "${this.environment[envVar]}" (${hostnameExternalReason})`);
2636
}
2737

38+
/**
39+
* Configure the LocalStack container to add sessionId when starting lambda container.
40+
* This allows the lambda container to be identified by the {@link Reaper} and cleaned up on exit.
41+
*/
42+
private async flagLambdaSessionId(): Promise<void> {
43+
const client = await getContainerRuntimeClient();
44+
const reaper = await getReaper(client);
45+
this.withEnvironment({ LAMBDA_DOCKER_FLAGS: `-l ${LABEL_TESTCONTAINERS_SESSION_ID}=${reaper.sessionId}` });
46+
}
47+
2848
protected override async beforeContainerCreated(): Promise<void> {
2949
this.resolveHostname();
50+
await this.flagLambdaSessionId();
3051
}
3152

3253
public override async start(): Promise<StartedLocalStackContainer> {

0 commit comments

Comments
 (0)