|
1 | | -<!-- |
2 | | -title: 'AWS Simple HTTP Endpoint example in Java' |
3 | | -description: 'This example demonstrates how to setup a simple HTTP GET endpoint using Java. Once you ping it, it will reply with the current time.' |
4 | | -layout: Doc |
5 | | -framework: v1 |
6 | | -platform: AWS |
7 | | -language: Java |
8 | | -authorLink: 'https://github.com/DoWhileGeek' |
9 | | -authorName: 'Joeseph Rodrigues' |
10 | | -authorAvatar: 'https://avatars3.githubusercontent.com/u/1767769?v=4&s=140' |
11 | | ---> |
12 | | -# Simple HTTP Endpoint Example |
13 | 1 |
|
14 | | -This example demonstrates how to setup a simple HTTP GET endpoint using Java. Once you ping it, it will reply with the current time. |
| 2 | +# Simple Cloud Functions handler framework |
15 | 3 |
|
16 | | -[Jackson](https://github.com/FasterXML/jackson) is used to serialize objects to JSON. |
17 | | - |
18 | | -## Use Cases |
19 | | - |
20 | | -- Wrapping an existing internal or external endpoint/service |
21 | | - |
22 | | -## Build |
23 | | - |
24 | | -It is required to build prior to deploying. You can build the deployment artifact using Gradle or Maven. |
25 | | - |
26 | | -### Gradle |
27 | | - |
28 | | -In order to build using Gradle simply run |
29 | | - |
30 | | -```bash |
31 | | -gradle wrapper # to build the gradle wrapper jar |
32 | | -./gradlew build # to build the application jar |
33 | | -``` |
34 | | - |
35 | | -The expected result should be similar to: |
36 | | - |
37 | | -```bash |
38 | | -Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details |
39 | | -:compileJava |
40 | | -:processResources |
41 | | -:classes |
42 | | -:jar |
43 | | -:assemble |
44 | | -:buildZip |
45 | | -:compileTestJava UP-TO-DATE |
46 | | -:processTestResources UP-TO-DATE |
47 | | -:testClasses UP-TO-DATE |
48 | | -:test UP-TO-DATE |
49 | | -:check UP-TO-DATE |
50 | | -:build |
51 | | - |
52 | | -BUILD SUCCESSFUL |
53 | | - |
54 | | -Total time: 8.195 secs |
55 | | -``` |
56 | | - |
57 | | -### Maven |
58 | | - |
59 | | -In order to build using Maven simply run |
60 | | - |
61 | | -```bash |
62 | | -mvn package |
63 | | -``` |
64 | | - |
65 | | -Note: you can install Maven with |
66 | | - |
67 | | -1. [sdkman](http://sdkman.io/) using `sdk install maven` (yes, use as default) |
68 | | -2. `sudo apt-get install mvn` |
69 | | -3. `brew install maven` |
70 | | - |
71 | | -If you use Maven to build, then in `serverless.yml` you have to replace |
72 | | - |
73 | | -```yaml |
74 | | -package: |
75 | | - artifact: build/distributions/aws-java-simple-http-endpoint.zip |
76 | | -``` |
77 | | -with |
78 | | -```yaml |
79 | | -package: |
80 | | - artifact: target/aws-java-simple-http-endpoint.jar |
81 | | -``` |
82 | | -before deploying. |
83 | | -
|
84 | | -## Deploy |
85 | | -
|
86 | | -After having built the deployment artifact using Gradle or Maven as described above you can deploy by simply running |
87 | | -
|
88 | | -```bash |
89 | | -serverless deploy |
90 | | -``` |
91 | | - |
92 | | -The expected result should be similar to: |
93 | | - |
94 | | -```bash |
95 | | -Serverless: Creating Stack... |
96 | | -Serverless: Checking Stack create progress... |
97 | | -..... |
98 | | -Serverless: Stack create finished... |
99 | | -Serverless: Uploading CloudFormation file to S3... |
100 | | -Serverless: Uploading service .zip file to S3... |
101 | | -Serverless: Updating Stack... |
102 | | -Serverless: Checking Stack update progress... |
103 | | -.............................. |
104 | | -Serverless: Stack update finished... |
105 | | -Service Information |
106 | | -service: aws-java-simple-http-endpoint |
107 | | -stage: dev |
108 | | -region: us-east-1 |
109 | | -api keys: |
110 | | - None |
111 | | -endpoints: |
112 | | - GET - https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/ping |
113 | | -functions: |
114 | | - aws-java-simple-http-endpoint-dev-currentTime: arn:aws:lambda:us-east-1:XXXXXXX:function:aws-java-simple-http-endpoint-dev-currentTime |
115 | | - |
116 | | -``` |
117 | | - |
118 | | -## Usage |
119 | | - |
120 | | -You can now invoke the Lambda function directly and even see the resulting log via |
121 | | - |
122 | | -```bash |
123 | | -serverless invoke --function currentTime --log |
124 | | -``` |
125 | | - |
126 | | -The expected result should be similar to: |
127 | | - |
128 | | -```bash |
129 | | -{ |
130 | | - "statusCode": 200, |
131 | | - "body": "{\"message\":\"Hello, the current time is Wed Jan 04 23:44:37 UTC 2017\"}", |
132 | | - "headers": { |
133 | | - "X-Powered-By": "AWS Lambda & Serverless", |
134 | | - "Content-Type": "application/json" |
135 | | - }, |
136 | | - "isBase64Encoded": false |
137 | | -} |
138 | | --------------------------------------------------------------------- |
139 | | -START RequestId: XXXXXXX Version: $LATEST |
140 | | -2004 23:44:37 <XXXXXXX> INFO com.serverless.Handler:18 - received: {} |
141 | | -END RequestId: XXXXXXX |
142 | | -REPORT RequestId: XXXXXXX Duration: 0.51 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 53 MB |
143 | | -``` |
144 | | - |
145 | | -Finally you can send an HTTP request directly to the endpoint using a tool like curl |
146 | | - |
147 | | -```bash |
148 | | -curl https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/ping |
149 | | -``` |
150 | | - |
151 | | -The expected result should be similar to: |
152 | | - |
153 | | -```bash |
154 | | -{"message": "Hello, the current time is Wed Jan 04 23:44:37 UTC 2017"}% |
155 | | -``` |
156 | | - |
157 | | -## Scaling |
158 | | - |
159 | | -By default, AWS Lambda limits the total concurrent executions across all functions within a given region to 100. The default limit is a safety limit that protects you from costs due to potential runaway or recursive functions during initial development and testing. To increase this limit above the default, follow the steps in [To request a limit increase for concurrent executions](http://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html#increase-concurrent-executions-limit). |
0 commit comments