22
33import com .amazonaws .services .lambda .runtime .Context ;
44import com .amazonaws .services .lambda .runtime .RequestHandler ;
5+ import java .io .IOException ;
56import java .util .Map ;
67import okhttp3 .OkHttpClient ;
78import okhttp3 .Request ;
89import okhttp3 .Response ;
910import org .json .JSONObject ;
10- import software .amazon .awssdk .services .s3 .S3Client ;
11- import software .amazon .awssdk .services .s3 .model .ListBucketsResponse ;
1211
1312public 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}
0 commit comments