Skip to content

Commit 169f52e

Browse files
committed
some cleanup
1 parent bdf0d27 commit 169f52e

File tree

4 files changed

+44
-54
lines changed

4 files changed

+44
-54
lines changed

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

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

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

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,49 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import java.io.IOException;
56
import java.util.Map;
67
import okhttp3.OkHttpClient;
78
import okhttp3.Request;
89
import okhttp3.Response;
910
import org.json.JSONObject;
10-
import software.amazon.awssdk.services.s3.S3Client;
11-
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
1211

1312
public class LambdaHandler implements RequestHandler<Object, Map<String, Object>> {
14-
private final OkHttpClient httpClient;
15-
private final S3Client s3Client;
1613

17-
public LambdaHandler() {
18-
this.httpClient = new OkHttpClient();
19-
this.s3Client = S3Client.create();
20-
}
14+
private final OkHttpClient client = new OkHttpClient();
2115

2216
@Override
23-
public Map<String, Object> handleRequest(Object o, Context context) {
24-
makeRemoteCall();
25-
listS3Buckets();
17+
public Map<String, Object> handleRequest(Object input, Context context) {
18+
System.out.println("Executing LambdaHandler");
2619

27-
// Get the trace id from system property
2820
// https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
29-
String traceId = System.getProperty("com.amazonaws.xray.traceHeader");
21+
// try and get the trace id from environment variable _X_AMZN_TRACE_ID. If it's not present
22+
// there
23+
// then try the system property.
24+
String traceId =
25+
System.getenv("_X_AMZN_TRACE_ID") != null
26+
? System.getenv("_X_AMZN_TRACE_ID")
27+
: System.getProperty("com.amazonaws.xray.traceHeader");
28+
29+
System.out.println("Trace ID: " + traceId);
30+
31+
// Make a remote call using OkHttp
32+
System.out.println("Making a remote call using OkHttp");
33+
String url = "https://www.amazon.com";
34+
Request request = new Request.Builder().url(url).build();
3035

31-
// Construct the response body
3236
JSONObject responseBody = new JSONObject();
33-
responseBody.put("message", "Request successful");
3437
responseBody.put("traceId", traceId);
3538

36-
// Return the API Gateway-compatible response
39+
try (Response response = client.newCall(request).execute()) {
40+
responseBody.put("httpRequest", "Request successful");
41+
} catch (IOException e) {
42+
context.getLogger().log("Error: " + e.getMessage());
43+
responseBody.put("httpRequest", "Request failed");
44+
}
45+
System.out.println("Remote call done");
46+
47+
// return a response in the ApiGateway proxy format
3748
return Map.of(
3849
"isBase64Encoded",
3950
false,
@@ -44,22 +55,4 @@ public Map<String, Object> handleRequest(Object o, Context context) {
4455
"headers",
4556
Map.of("Content-Type", "application/json"));
4657
}
47-
48-
private void makeRemoteCall() {
49-
try {
50-
Request request = new Request.Builder().url("https://aws.amazon.com/").build();
51-
Response response = httpClient.newCall(request).execute();
52-
response.close();
53-
} catch (Exception e) {
54-
e.printStackTrace();
55-
}
56-
}
57-
58-
private void listS3Buckets() {
59-
ListBucketsResponse response = s3Client.listBuckets();
60-
int bucketCount = response.buckets().size();
61-
62-
// Print bucket count
63-
System.out.println("Number of S3 buckets: " + bucketCount);
64-
}
6558
}

sample-apps/apigateway-lambda/terraform/main.tf

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "aws_iam_policy" "s3_access" {
2828
})
2929
}
3030

31-
resource "aws_iam_role_policy_attachment" "attachBasicExecutionRolePolicy" {
31+
resource "aws_iam_role_policy_attachment" "attach_execution_role_policy" {
3232
role = aws_iam_role.lambda_role.name
3333
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
3434
}
@@ -51,9 +51,17 @@ resource "aws_lambda_function" "sampleLambdaFunction" {
5151
role = aws_iam_role.lambda_role.arn
5252
filename = "${path.module}/../build/distributions/lambda-function.zip"
5353
source_code_hash = filebase64sha256("${path.module}/../build/distributions/lambda-function.zip")
54+
architectures = [var.architecture]
55+
memory_size = 512
5456
tracing_config {
5557
mode = var.lambda_tracing_mode
5658
}
59+
layers = var.adot_layer_arn != null && var.adot_layer_arn != "" ? [var.adot_layer_arn] : []
60+
environment {
61+
variables = var.adot_layer_arn != null && var.adot_layer_arn != "" ? {
62+
AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-instrument"
63+
} : {}
64+
}
5765
}
5866

5967
### API Gateway proxy to Lambda function
@@ -83,23 +91,6 @@ resource "aws_api_gateway_integration" "apigw_lambda_integration" {
8391
uri = aws_lambda_function.sampleLambdaFunction.invoke_arn
8492
}
8593

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-
10394
resource "aws_api_gateway_deployment" "apigw_lambda_deployment" {
10495
depends_on = [
10596
aws_api_gateway_integration.apigw_lambda_integration

sample-apps/apigateway-lambda/terraform/variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
## Lambda function related configurations
12
variable "function_name" {
23
type = string
34
description = "Name of sample app function"
@@ -22,6 +23,13 @@ variable "lambda_tracing_mode" {
2223
default = "Active"
2324
}
2425

26+
variable "adot_layer_arn" {
27+
type = string
28+
description = "ARN of the ADOT JAVA layer"
29+
default = null
30+
}
31+
32+
## API Gateway related configurations
2533
variable "api_gateway_name" {
2634
type = string
2735
description = "Name of API gateway to create"

0 commit comments

Comments
 (0)