728x90
웹사이트 html Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Session Assignment from Lambda</title>
</head>
<body>
<h2>Welcome to Our Website</h2>
<p id="sessionInfo">Loading...</p>
<script>
window.onload = function () {
fetch('YOUR_API_URL')
.then(response => response.json())
.then(data => {
const sessionId = data.sessionId; // 세션 ID 추출
console.log("Received session ID:", sessionId);
document.getElementById('sessionInfo').textContent = "세션 ID가 부여되었습니다. 세션 ID: " + sessionId;
})
.catch(error => console.error('Error:', error));
};
</script>
</body>
</html>
Lambda Roles
Lambda Code
javaSE1.8과 maven빌드 eclipse툴 사용
.java
package aa;
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 java.util.HashMap;
import java.util.UUID;
public class SessionIdHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
// UUID를 사용하여 고유한 세션 ID 생성
String sessionId = UUID.randomUUID().toString();
context.getLogger().log("Generated Session ID: " + sessionId);
// CORS 문제를 방지하기 위해 적절한 헤더 설정
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Access-Control-Allow-Origin", "*");
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
response.setStatusCode(200);
response.setHeaders(headers);
response.setBody("{\"sessionId\": \"" + sessionId + "\"}");
return response;
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>say</groupId>
<artifactId>sessionid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hi</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-log4j2</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
API Gateway(REST)
API Gateway 구조
CORS - 활성화 후 Lambda에서 트리거로 연결
RESULT
done.
728x90
'Amazon Web Services' 카테고리의 다른 글
AWS 네트워크 인터페이스에서 IP 추가와 재부팅 후 유지 여부 비교 (0) | 2025.01.12 |
---|---|
AWS Serverless Application Model (0) | 2024.03.07 |
람다람쥐썬더 (0) | 2024.03.02 |
Lambda SES 이메일 보내기 (0) | 2024.02.26 |
Lambda hello world~ .jar (0) | 2024.02.21 |