Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 886c940

Browse files
author
TechPrimers
committed
AWS Lambda API Example
0 parents commit 886c940

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

‎.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
target
3+
*.iml

‎README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# AWS Lambda Java API Example
2+
3+
This example code is used for running a Java Lambda using API Gateway trigger.
4+
5+
It uses AWS Lambda Core SDK for `RequestHandler` implementation which is called by the trigger

‎pom.xml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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.techprimers.aws</groupId>
8+
<artifactId>lambda-java-api-example</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>lambda-java-api-example</name>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>com.amazonaws</groupId>
23+
<artifactId>aws-lambda-java-core</artifactId>
24+
<version>1.1.0</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>4.11</version>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
37+
<plugins>
38+
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-shade-plugin</artifactId>
42+
<version>2.3</version>
43+
<configuration>
44+
<createDependencyReducedPom>false</createDependencyReducedPom>
45+
</configuration>
46+
<executions>
47+
<execution>
48+
<phase>package</phase>
49+
<goals>
50+
<goal>shade</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<version>3.8.0</version>
60+
<configuration>
61+
<source>1.8</source>
62+
<target>1.8</target>
63+
</configuration>
64+
</plugin>
65+
66+
</plugins>
67+
</pluginManagement>
68+
</build>
69+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.techprimers.aws;
2+
3+
import java.util.Map;
4+
5+
public class GatewayResponse {
6+
7+
private String body;
8+
private Integer statusCode;
9+
private Map<String, String> headers;
10+
private boolean isBase64Encoded;
11+
12+
public GatewayResponse(String body, Integer statusCode, Map<String, String> headers, boolean isBase64Encoded) {
13+
this.body = body;
14+
this.statusCode = statusCode;
15+
this.headers = headers;
16+
this.isBase64Encoded = isBase64Encoded;
17+
}
18+
19+
public String getBody() {
20+
return body;
21+
}
22+
23+
public void setBody(String body) {
24+
this.body = body;
25+
}
26+
27+
public Integer getStatusCode() {
28+
return statusCode;
29+
}
30+
31+
public void setStatusCode(Integer statusCode) {
32+
this.statusCode = statusCode;
33+
}
34+
35+
public Map<String, String> getHeaders() {
36+
return headers;
37+
}
38+
39+
public void setHeaders(Map<String, String> headers) {
40+
this.headers = headers;
41+
}
42+
43+
public boolean isBase64Encoded() {
44+
return isBase64Encoded;
45+
}
46+
47+
public void setBase64Encoded(boolean base64Encoded) {
48+
isBase64Encoded = base64Encoded;
49+
}
50+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.techprimers.aws;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
6+
import java.util.Collections;
7+
8+
public class LambdaJavaAPI implements RequestHandler<Object, GatewayResponse> {
9+
10+
@Override
11+
public GatewayResponse handleRequest(Object object, Context context) {
12+
13+
String message = "Hello from TechPrimers";
14+
System.out.println(message);
15+
16+
GatewayResponse response = new GatewayResponse(
17+
message,
18+
200,
19+
Collections.singletonMap("X-Powered-By", "TechPrimers"),
20+
false
21+
);
22+
return response;
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /