我试图使用localStack运行lambada并查看日志.
所以你跑起来就像:
public class LambdaLoader implements RequestHandler<Object, String> {
@Override
public String handleRequest(Object input, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("\"started\"");
return "Complete";
}我在运行它
public class LambdaLoaderIT {
@Test
void handleRequest() throws InterruptedException, IOException {
AwsClientBuilder.EndpointConfiguration endpointConfiguration =
new AwsClientBuilder.EndpointConfiguration(
"http://localhost:4566", Regions.US_EAST_1.getName());
AWSLambda lambdaClient = createLambdaClient(endpointConfiguration);
createLambda(lambdaClient);
}
private AWSLambda createLambdaClient(
AwsClientBuilder.EndpointConfiguration endpointConfiguration) {
return AWSLambdaClientBuilder.standard()
.withEndpointConfiguration(endpointConfiguration)
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("dummyAccessKey", "dummySecretKey")))
.build();
}
private void createLambda(AWSLambda clientLambda) throws IOException {
CreateFunctionRequest functionRequest = new CreateFunctionRequest();
functionRequest.setHandler("com.ssp.coreTeam.LambdaLoader::handleRequest");
functionRequest.setFunctionName("handleRequest");
functionRequest.setTimeout(900);
functionRequest.setRuntime("java11");
functionRequest.setRole("arn:aws:lambda:us-east-1:000000000000:function:handleRequest");
FunctionCode code = new FunctionCode();
File file = new File("target/my-lambda-0.0.0-SNAPSHOT.jar");
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = IoUtils.toByteArray(fileInputStream);
code.setZipFile(ByteBuffer.wrap(bytes));
functionRequest.setCode(code);
Environment environment = new Environment();
environment.setVariables(Map.of("LAMBDA_ENV","dev"));
functionRequest.setEnvironment(environment);
CreateFunctionResult function = clientLambda.createFunction(functionRequest);
System.out.println(function);
}此外,我是如何在docker-compose文件中配置lambada的(注意,LAMBDA_EXECUTOR=local ):
localstack:
image: 'localstack/localstack'
ports:
- '4566:4566'
environment:
- SERVICES=lambda,ssm
- DEBUG=1
- DATA_DIR=${DATA_DIR- }
- PORT_WEB_UI=${PORT_WEB_UI- }
- LAMBDA_EXECUTOR=local
- KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
- DOCKER_HOST=unix:///var/run/docker.sock
- HOST_TMP_FOLDER=${TMPDIR}
volumes:
- "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"我怎么能看到原木和那里发生了什么?
发布于 2022-01-30 23:19:52
您已经将DEBUG设置为1,因此日志就在那里。
要阅读它们,请使用标准码头为原木组装设施。在你的例子中,应该是这样的:
docker-compose logs localstack我还建议您使用一个小型库在您的测试中注入AWS客户机,名为aws-junit5。这将大大简化您的测试。它支持Lambda客户端的AWSJavaSDK1.x和2.x。使用非常简单:
@ExtendWith(Lambda.class)
class AmazonDynamoDBInjectionTest {
@AWSClient(endpoint = Endpoint.class) // Endpoint configuration
private AWSLambda client;
@Test
void test() {
CreateFunctionRequest functionRequest = new CreateFunctionRequest();
functionRequest.setHandler("com.ssp.coreTeam.LambdaLoader::handleRequest");
functionRequest.setFunctionName("handleRequest");
functionRequest.setTimeout(900);
functionRequest.setRuntime("java11");
functionRequest.setRole("arn:aws:lambda:us-east-1:000000000000:function:handleRequest");
FunctionCode code = new FunctionCode();
File file = new File("target/my-lambda-0.0.0-SNAPSHOT.jar");
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = IoUtils.toByteArray(fileInputStream);
code.setZipFile(ByteBuffer.wrap(bytes));
functionRequest.setCode(code);
Environment environment = new Environment();
environment.setVariables(Map.of("LAMBDA_ENV","dev"));
functionRequest.setEnvironment(environment);
// Just use client here, it will be auto-injected!
CreateFunctionResult function = client.createFunction(functionRequest);
// Rest of your test
System.out.println(function);
}
}甚至还有用GitHub实现CI/CD的一个实例,它非常类似于您正在做的事情。
https://stackoverflow.com/questions/69512807
复制相似问题