Skip to content

Instantly share code, notes, and snippets.

@msailes
Created June 14, 2023 11:29
Show Gist options
  • Save msailes/528be800973feb42f7d61608590555b9 to your computer and use it in GitHub Desktop.
Save msailes/528be800973feb42f7d61608590555b9 to your computer and use it in GitHub Desktop.
Local end to end testing for Lambda handlers. This is useful to test how your events will be serialized by the managed runtime.
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
package helloworld;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.lambda.LambdaClient;
import software.amazon.awssdk.services.lambda.model.CreateFunctionRequest;
import software.amazon.awssdk.services.lambda.model.FunctionCode;
import software.amazon.awssdk.services.lambda.model.InvokeRequest;
import software.amazon.awssdk.services.lambda.model.InvokeResponse;
import software.amazon.awssdk.services.lambda.model.Runtime;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Testcontainers
public class LocalEndToEndTest {
@Container
private static final LocalStackContainer localStack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:2.1.0"))
.withEnv("LAMBDA_SYNCHRONOUS_CREATE", "1");
private static final String FUNCTION_NAME = "vehicles";
private static final Region region = Region.of(localStack.getRegion());
private static LambdaClient lambdaClient;
@BeforeAll
public static void beforeAll() throws IOException {
lambdaClient = LambdaClient.builder()
.endpointOverride(localStack.getEndpointOverride(LocalStackContainer.Service.LAMBDA))
.region(region)
.build();
String zipFilePath = "target/HelloWorld-1.0.jar";
byte[] zipFileBytes = Files.readAllBytes(Paths.get(zipFilePath));
CreateFunctionRequest createFunctionRequest = CreateFunctionRequest.builder()
.functionName(FUNCTION_NAME)
.runtime(Runtime.JAVA17)
.handler(App.class.getName())
.code(FunctionCode.builder().zipFile(SdkBytes.fromByteArray(zipFileBytes)).build())
.role("arn:aws:iam::123456789012:role/irrelevant")
.timeout(60)
.memorySize(512)
.build();
lambdaClient.createFunction(createFunctionRequest);
}
@Test
public void testLambdaCustomSerializer() {
String jsonEventPayload = """
{
"vehicle-type": "car",
"vehicleID": 123
}
""";
InvokeRequest invokeRequest = InvokeRequest.builder()
.functionName(FUNCTION_NAME)
.payload(SdkBytes.fromString(jsonEventPayload, StandardCharsets.UTF_8))
.build();
InvokeResponse response = lambdaClient.invoke(invokeRequest);
assertEquals(HTTP_OK, response.statusCode());
assertEquals("""
{"body":"123,car","statusCode":200}""", response.payload().asUtf8String());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment