package com.example.hello_lambda.handler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.example.hello_lambda.HelloLambdaApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import java.util.Map;
public class LambdaHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private static final ApplicationContext context =
SpringApplication.run(HelloLambdaApplication.class);
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context contextLambda) {
String name = request.getQueryStringParameters().getOrDefault("name", "World");
String body = "Hello, " + name + "!";
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody(body);
}
}
HelloLambdaApplicationの修正
HelloLambdaApplicationを以下の通り修正する。
HelloLambdaApplication.java
package com.example.hello_lambda;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloLambdaApplication {
}
build.gradleの修正
以下の通り修正する。
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.4'
id 'io.spring.dependency-management' version '1.1.4'
id 'com.github.johnrengelman.shadow' version '8.1.1'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'com.amazonaws:aws-lambda-java-core:1.2.3'
implementation 'com.amazonaws:aws-lambda-java-events:3.11.0'
}
shadowJar {
archiveBaseName.set("app")
archiveClassifier.set("")
archiveVersion.set("")
}
コメント