Skip to content

Commit 847abd8

Browse files
committed
First commit
1 parent da7add5 commit 847abd8

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

awslambdahttprequest/README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# AWS Lambda Function Http Request Java example
2+
3+
This folder contains an AWS Lambda Function example in Java on AWS (Amazon Web Services).
4+
5+
It handles an AWS Lambda function that it is invoked by an http request. It shows the parameters of the request and responds a message including the parameters.
6+
7+
## Requirements
8+
9+
* You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account.
10+
11+
* The code was written for:
12+
13+
* Java 8
14+
* Apache Maven 3
15+
* AWS Java Lambda Support Libraries:
16+
* AWS Lambda Java Core Library
17+
* AWS Lambda Java Events Library
18+
19+
## Using the code
20+
21+
* Access the AWS console.
22+
23+
* Create an AWS lambda function:
24+
* Name: `<LAMBDA_NAME>`
25+
* Runtime: `Java 8`
26+
* Handler: `example.HttpRequestHandler::handleRequest`
27+
* Role: `lambda-basic-execution`
28+
* Runtime Settings for the lambda function:
29+
* Memory (MB): `512`
30+
* Timeout: `15 sec`
31+
* The resources that the function's role has access to:
32+
* `Amazon CloudWatch Logs`
33+
* The triggers:
34+
* `API Gateway`
35+
* Details below.
36+
37+
* Upload the Java JAR file.
38+
39+
Artifact:
40+
41+
```bash
42+
awslambdahttprequest.jar
43+
```
44+
45+
* Save the Lambda function.
46+
47+
It deploys the Lambda function.
48+
49+
* Create an `API Gateway` trigger.
50+
51+
This allows to call the lambda function using an HTTP API.
52+
53+
* Name: `<LAMBDA_NAME>-API`
54+
* API: `Create an API`
55+
* API type: `HTTP API`
56+
* Security: `Open`
57+
58+
You will get an API endpoint, which can be copied and run in your browser's address bar.
59+
60+
It looks like the following URL:
61+
62+
```bash
63+
https://<API_ID>.execute-api.<REGION>.amazonaws.com/<STAGE_NAME>/<LAMBDA_NAME>
64+
```
65+
66+
For example:
67+
68+
```bash
69+
https://abcdefg5jk.execute-api.eu-west-1.amazonaws.com/default/HttpRequestJava`
70+
```
71+
72+
* Run the code.
73+
74+
To run the code, you need to use 2 parameters:
75+
76+
* `firstname`
77+
* `lastname`
78+
79+
You call the API endpoint with this format:
80+
81+
```bash
82+
https://<API_ID>.execute-api.<REGION>.amazonaws.com/<STAGE_NAME>/<LAMBDA_NAME>?firstname=<FIRST_NAME>&lastname=<LAST_NAME>
83+
```
84+
85+
For example:
86+
87+
```bash
88+
https://abcdefg5jk.execute-api.eu-west-1.amazonaws.com/default/HttpRequestPython?firstname=Peter&lastname=Parker
89+
```
90+
91+
* Test the AWS Lambda function.
92+
93+
Go to the URL of API endpoint that you have got: `https://<API_ID>.execute-api.<REGION>.amazonaws.com/<STAGE_NAME>/<LAMBDA_NAME>?firstname=Peter&lastname=Parker` using a browser.
94+
95+
You should see the next response if you have added the right paramenters:
96+
97+
```bash
98+
"Hello Peter Parker!"
99+
```
100+
101+
You should see the next response if you have not added any paramenter:
102+
103+
```bash
104+
"Who are you?"
105+
```
106+

awslambdahttprequest/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.alfonsof.awsexamples</groupId>
8+
<artifactId>aws-lambda-http-request</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<jdk.version>8</jdk.version>
14+
<maven.compiler.source>${jdk.version}</maven.compiler.source>
15+
<maven.compiler.target>${jdk.version}</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.amazonaws</groupId>
21+
<artifactId>aws-lambda-java-core</artifactId>
22+
<version>1.2.1</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.amazonaws</groupId>
26+
<artifactId>aws-lambda-java-events</artifactId>
27+
<version>3.9.0</version>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.8.1</version>
37+
<configuration>
38+
<source>${jdk.version}</source>
39+
<target>${jdk.version}</target>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-shade-plugin</artifactId>
45+
<version>3.2.4</version>
46+
<configuration>
47+
<createDependencyReducedPom>false</createDependencyReducedPom>
48+
</configuration>
49+
<executions>
50+
<execution>
51+
<phase>package</phase>
52+
<goals>
53+
<goal>shade</goal>
54+
</goals>
55+
</execution>
56+
</executions>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: example.HttpRequestHandler
3+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* AWS Lambda Function Http Request Java example.
3+
* Handle an AWS Lambda function that it is invoked by an http request.
4+
* It shows the parameters of the request and responds a message including the parameters.
5+
*/
6+
7+
package example;
8+
9+
import java.util.Map;
10+
import java.util.HashMap;
11+
import com.amazonaws.services.lambda.runtime.Context;
12+
import com.amazonaws.services.lambda.runtime.RequestHandler;
13+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
14+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
15+
16+
public class HttpRequestHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>{
17+
18+
@Override
19+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
20+
context.getLogger().log("Event: " + event);
21+
Map queryStringParameters = event.getQueryStringParameters();
22+
String responseString;
23+
if (queryStringParameters == null) {
24+
responseString = "Who are you?";
25+
} else {
26+
context.getLogger().log("firstname: " + queryStringParameters.get("firstname"));
27+
context.getLogger().log("lastname: " + queryStringParameters.get("lastname"));
28+
responseString = "Hello " +
29+
queryStringParameters.get("firstname") + " " +
30+
queryStringParameters.get("lastname") +"!";
31+
}
32+
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
33+
response.setIsBase64Encoded(false);
34+
response.setStatusCode(200);
35+
HashMap<String, String> headers = new HashMap<String, String>();
36+
headers.put("Content-Type", "text/html");
37+
response.setHeaders(headers);
38+
response.setBody("<!DOCTYPE html><html><head><title>AWS Lambda example</title></head><body>"+
39+
responseString + "</p>" +
40+
"</body></html>");
41+
42+
return response;
43+
}
44+
}

0 commit comments

Comments
 (0)