Skip to content

Commit bdf0d27

Browse files
committed
manage app through terraform
1 parent 040d63e commit bdf0d27

File tree

4 files changed

+187
-6
lines changed

4 files changed

+187
-6
lines changed

sample-apps/apigateway-lambda/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ dependencies {
1717
implementation("com.amazonaws:aws-lambda-java-core:1.2.2")
1818
implementation("com.squareup.okhttp3:okhttp:4.11.0")
1919
implementation("software.amazon.awssdk:s3:2.20.0")
20+
implementation("org.slf4j:jcl-over-slf4j:2.0.16")
21+
implementation("org.json:json:20240303")
2022
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
2123
}
2224

sample-apps/apigateway-lambda/src/main/java/com/amazon/sampleapp/LambdaHandler.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import java.util.Map;
56
import okhttp3.OkHttpClient;
67
import okhttp3.Request;
78
import okhttp3.Response;
9+
import org.json.JSONObject;
810
import software.amazon.awssdk.services.s3.S3Client;
911
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
1012

11-
public class LambdaHandler implements RequestHandler<Object, String> {
13+
public class LambdaHandler implements RequestHandler<Object, Map<String, Object>> {
1214
private final OkHttpClient httpClient;
1315
private final S3Client s3Client;
1416

@@ -18,15 +20,29 @@ public LambdaHandler() {
1820
}
1921

2022
@Override
21-
public String handleRequest(Object o, Context context) {
23+
public Map<String, Object> handleRequest(Object o, Context context) {
2224
makeRemoteCall();
2325
listS3Buckets();
2426

25-
// Get the _X_AMZN_TRACE_ID environment variable
26-
String traceId = System.getenv("_X_AMZN_TRACE_ID");
27+
// Get the trace id from system property
28+
// https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
29+
String traceId = System.getProperty("com.amazonaws.xray.traceHeader");
2730

28-
// Construct the response string
29-
return "Trace ID: " + traceId;
31+
// Construct the response body
32+
JSONObject responseBody = new JSONObject();
33+
responseBody.put("message", "Request successful");
34+
responseBody.put("traceId", traceId);
35+
36+
// Return the API Gateway-compatible response
37+
return Map.of(
38+
"isBase64Encoded",
39+
false,
40+
"statusCode",
41+
200,
42+
"body",
43+
responseBody.toString(),
44+
"headers",
45+
Map.of("Content-Type", "application/json"));
3046
}
3147

3248
private void makeRemoteCall() {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
### Lambda function
2+
locals {
3+
architecture = var.architecture == "x86_64" ? "amd64" : "arm64"
4+
}
5+
6+
resource "aws_iam_role" "lambda_role" {
7+
name = "lambda_execution_role"
8+
assume_role_policy = jsonencode({
9+
Version = "2012-10-17",
10+
Statement = [{
11+
Action = "sts:AssumeRole",
12+
Effect = "Allow",
13+
Principal = { Service = "lambda.amazonaws.com" }
14+
}]
15+
})
16+
}
17+
18+
resource "aws_iam_policy" "s3_access" {
19+
name = "S3ListBucketsPolicy"
20+
description = "Allow Lambda to list S3 buckets"
21+
policy = jsonencode({
22+
Version = "2012-10-17",
23+
Statement = [{
24+
Effect = "Allow",
25+
Action = ["s3:ListAllMyBuckets"],
26+
Resource = "*"
27+
}]
28+
})
29+
}
30+
31+
resource "aws_iam_role_policy_attachment" "attachBasicExecutionRolePolicy" {
32+
role = aws_iam_role.lambda_role.name
33+
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
34+
}
35+
36+
resource "aws_iam_role_policy_attachment" "attach_s3_policy" {
37+
role = aws_iam_role.lambda_role.name
38+
policy_arn = aws_iam_policy.s3_access.arn
39+
}
40+
41+
resource "aws_iam_role_policy_attachment" "attach_xray_policy" {
42+
role = aws_iam_role.lambda_role.name
43+
policy_arn = "arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess"
44+
}
45+
46+
resource "aws_lambda_function" "sampleLambdaFunction" {
47+
function_name = var.function_name
48+
runtime = var.runtime
49+
timeout = 300
50+
handler = "com.amazon.sampleapp.LambdaHandler::handleRequest"
51+
role = aws_iam_role.lambda_role.arn
52+
filename = "${path.module}/../build/distributions/lambda-function.zip"
53+
source_code_hash = filebase64sha256("${path.module}/../build/distributions/lambda-function.zip")
54+
tracing_config {
55+
mode = var.lambda_tracing_mode
56+
}
57+
}
58+
59+
### API Gateway proxy to Lambda function
60+
resource "aws_api_gateway_rest_api" "apigw_lambda_api" {
61+
name = var.api_gateway_name
62+
}
63+
64+
resource "aws_api_gateway_resource" "apigw_lambda_resource" {
65+
rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id
66+
parent_id = aws_api_gateway_rest_api.apigw_lambda_api.root_resource_id
67+
path_part = "lambda"
68+
}
69+
70+
resource "aws_api_gateway_method" "apigw_lambda_method" {
71+
rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id
72+
resource_id = aws_api_gateway_resource.apigw_lambda_resource.id
73+
http_method = "ANY"
74+
authorization = "NONE"
75+
}
76+
77+
resource "aws_api_gateway_integration" "apigw_lambda_integration" {
78+
rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id
79+
resource_id = aws_api_gateway_resource.apigw_lambda_resource.id
80+
http_method = aws_api_gateway_method.apigw_lambda_method.http_method
81+
integration_http_method = "POST"
82+
type = "AWS_PROXY"
83+
uri = aws_lambda_function.sampleLambdaFunction.invoke_arn
84+
}
85+
86+
# resource "aws_api_gateway_method" "lambda_api_proxy_root_nodejs" {
87+
# rest_api_id = aws_api_gateway_rest_api.lambda_api_proxy.id
88+
# resource_id = aws_api_gateway_rest_api.lambda_api_proxy.root_resource_id
89+
# http_method = "ANY"
90+
# authorization = "NONE"
91+
# }
92+
#
93+
# resource "aws_api_gateway_integration" "lambda_api_root_nodejs" {
94+
# rest_api_id = aws_api_gateway_rest_api.lambda_api_proxy.id
95+
# resource_id = aws_api_gateway_method.lambda_api_proxy_root_nodejs.resource_id
96+
# http_method = aws_api_gateway_method.lambda_api_proxy_root_nodejs.http_method
97+
#
98+
# integration_http_method = "POST"
99+
# type = "AWS_PROXY"
100+
# uri = var.function_invoke_arn
101+
# }
102+
103+
resource "aws_api_gateway_deployment" "apigw_lambda_deployment" {
104+
depends_on = [
105+
aws_api_gateway_integration.apigw_lambda_integration
106+
]
107+
rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id
108+
}
109+
110+
resource "aws_api_gateway_stage" "test" {
111+
stage_name = "default"
112+
rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id
113+
deployment_id = aws_api_gateway_deployment.apigw_lambda_deployment.id
114+
xray_tracing_enabled = var.apigw_tracing_enabled
115+
}
116+
117+
resource "aws_lambda_permission" "apigw_lambda_invoke" {
118+
statement_id = "AllowAPIGatewayInvoke"
119+
action = "lambda:InvokeFunction"
120+
function_name = aws_lambda_function.sampleLambdaFunction.function_name
121+
principal = "apigateway.amazonaws.com"
122+
source_arn = "${aws_api_gateway_rest_api.apigw_lambda_api.execution_arn}/*/*"
123+
}
124+
125+
# Output the API Gateway URL
126+
output "invoke_url" {
127+
value = "${aws_api_gateway_stage.test.invoke_url}/lambda"
128+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
variable "function_name" {
2+
type = string
3+
description = "Name of sample app function"
4+
default = "aws-opentelemetry-distro-java"
5+
}
6+
7+
variable "architecture" {
8+
type = string
9+
description = "Lambda function architecture, either arm64 or x86_64"
10+
default = "x86_64"
11+
}
12+
13+
variable "runtime" {
14+
type = string
15+
description = "Java runtime version used for Lambda Function"
16+
default = "java17"
17+
}
18+
19+
variable "lambda_tracing_mode" {
20+
type = string
21+
description = "Lambda function tracing mode"
22+
default = "Active"
23+
}
24+
25+
variable "api_gateway_name" {
26+
type = string
27+
description = "Name of API gateway to create"
28+
default = "aws-opentelemetry-distro-java"
29+
}
30+
31+
variable "apigw_tracing_enabled" {
32+
type = string
33+
description = "API Gateway REST API tracing enabled or not"
34+
default = "true"
35+
}

0 commit comments

Comments
 (0)