-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AWS Lambda Function URL Guide (#1365)
* AWS Lambda Function URL Guide ./gradlew mnServerlessFunctionAwsLambdaFunctionUrlBuild * twitter card * Remove unused imports, semicolons and non-groovy/kotlin isms --------- Co-authored-by: Tim Yates <tim.yates@gmail.com>
- Loading branch information
Showing
14 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...ambda-function-url/groovy/src/main/groovy/example/micronaut/FunctionRequestHandler.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.function.aws.MicronautRequestHandler | ||
import io.micronaut.json.JsonMapper | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent | ||
import jakarta.inject.Inject | ||
|
||
class FunctionRequestHandler extends MicronautRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
|
||
@Inject | ||
JsonMapper objectMapper | ||
|
||
@Override | ||
APIGatewayProxyResponseEvent execute(APIGatewayProxyRequestEvent input) { | ||
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() | ||
try { | ||
String json = new String(objectMapper.writeValueAsBytes([message: "Hello World"])) | ||
response.statusCode = 200 | ||
response.body = json | ||
} catch (IOException e) { | ||
response.statusCode = 500 | ||
} | ||
response | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...a-function-url/groovy/src/test/groovy/example/micronaut/FunctionRequestHandlerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package example.micronaut | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class FunctionRequestHandlerSpec extends Specification { | ||
|
||
@AutoCleanup | ||
@Shared | ||
FunctionRequestHandler handler = new FunctionRequestHandler() | ||
|
||
void "test Handler"() { | ||
given: | ||
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent() | ||
request.httpMethod = "GET" | ||
request.path = "/" | ||
|
||
when: | ||
APIGatewayProxyResponseEvent response = handler.execute(request) | ||
|
||
then: | ||
response.getStatusCode().intValue() == 200 | ||
response.body == '{"message":"Hello World"}' | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...-aws-lambda-function-url/java/src/main/java/example/micronaut/FunctionRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.function.aws.MicronautRequestHandler; | ||
import java.io.IOException; | ||
import io.micronaut.json.JsonMapper; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; | ||
import jakarta.inject.Inject; | ||
import java.util.Collections; | ||
|
||
public class FunctionRequestHandler extends MicronautRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
|
||
@Inject | ||
JsonMapper objectMapper; | ||
|
||
@Override | ||
public APIGatewayProxyResponseEvent execute(APIGatewayProxyRequestEvent input) { | ||
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent(); | ||
try { | ||
String json = new String(objectMapper.writeValueAsBytes(Collections.singletonMap("message", "Hello World"))); | ||
response.setStatusCode(200); | ||
response.setBody(json); | ||
} catch (IOException e) { | ||
response.setStatusCode(500); | ||
} | ||
return response; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...-lambda-function-url/java/src/test/java/example/micronaut/FunctionRequestHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package example.micronaut; | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class FunctionRequestHandlerTest { | ||
|
||
private static FunctionRequestHandler handler; | ||
|
||
@BeforeAll | ||
public static void setupServer() { | ||
handler = new FunctionRequestHandler(); | ||
} | ||
|
||
@AfterAll | ||
public static void stopServer() { | ||
if (handler != null) { | ||
handler.getApplicationContext().close(); | ||
} | ||
} | ||
|
||
@Test | ||
void testHandler() { | ||
APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent(); | ||
request.setHttpMethod("GET"); | ||
request.setPath("/"); | ||
APIGatewayProxyResponseEvent response = handler.execute(request); | ||
assertEquals(200, response.getStatusCode().intValue()); | ||
assertEquals("{\"message\":\"Hello World\"}", response.getBody()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ws-lambda-function-url/kotlin/src/main/kotlin/example/micronaut/FunctionRequestHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.function.aws.MicronautRequestHandler | ||
import java.io.IOException | ||
import io.micronaut.json.JsonMapper | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent | ||
import jakarta.inject.Inject | ||
|
||
class FunctionRequestHandler : MicronautRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>() { | ||
|
||
@Inject | ||
lateinit var objectMapper: JsonMapper | ||
|
||
override fun execute(input: APIGatewayProxyRequestEvent): APIGatewayProxyResponseEvent = | ||
APIGatewayProxyResponseEvent().apply { | ||
try { | ||
val json = String(objectMapper.writeValueAsBytes(mapOf("message" to "Hello World"))) | ||
statusCode = 200 | ||
body = json | ||
} catch (e: IOException) { | ||
statusCode = 500 | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ambda-function-url/kotlin/src/test/kotlin/example/micronaut/FunctionRequestHandlerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package example.micronaut | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class FunctionRequestHandlerTest { | ||
|
||
@Test | ||
fun testHandler() { | ||
val handler = FunctionRequestHandler() | ||
val request = APIGatewayProxyRequestEvent() | ||
request.httpMethod = "GET" | ||
request.path = "/" | ||
val response = handler.execute(request) | ||
assertEquals(200, response.statusCode.toInt()) | ||
assertEquals("{\"message\":\"Hello World\"}", response.body) | ||
handler.close() | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
guides/mn-serverless-function-aws-lambda-function-url/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"title": "AWS Lambda Function URL and a Micronaut function", | ||
"intro": "Deploy a Micronaut function to AWS Lambda Java 21 runtime and invoke it with a Lambda function URL.", | ||
"authors": ["Sergio del Amo"], | ||
"tags": ["aws-lambda-function-url"], | ||
"categories": ["AWS Lambda"], | ||
"publicationDate": "2023-10-16", | ||
"apps": [ | ||
{ | ||
"applicationType": "function", | ||
"name": "default", | ||
"features": ["aws-lambda"] | ||
} | ||
] | ||
} |
94 changes: 94 additions & 0 deletions
94
...ion-aws-lambda-function-url/mn-serverless-function-aws-lambda-function-url.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
common:header-top.adoc[] | ||
|
||
In this guide, **we will deploy a Micronaut serverless function to AWS Lambda Java 17 runtime and invoke it with a https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html[Lambda function URL]**. | ||
|
||
common:gettingStarted.adoc[] | ||
|
||
common:requirements.adoc[] | ||
|
||
common:completesolution.adoc[] | ||
|
||
== Writing the Application | ||
|
||
common:cli-or-launch.adoc[] | ||
|
||
[source,bash] | ||
---- | ||
mn create-function-app example.micronaut.micronautguide --features=@features@ --build=@build@ --lang=@lang@ | ||
---- | ||
|
||
common:build-lang-arguments.adoc[] | ||
|
||
If you use https://launch.micronaut.io[Micronaut Launch], select _serverless function_ as application type and add the `aws-lambda` and `aws-lambda-function-url` features. | ||
|
||
common:default-package.adoc[] | ||
|
||
https://guides.micronaut.io/latest/micronaut-intellij-idea-ide-setup.html[Setup IntelliJ IDEA to develop Micronaut Applications]. | ||
|
||
== Handler | ||
|
||
Create a class extending https://micronaut-projects.github.io/micronaut-aws/latest/api/io/micronaut/function/aws/MicronautRequestHandler.html[MicronautRequestHandler] and define input and output types. | ||
|
||
source:FunctionRequestHandler[] | ||
|
||
== Handler Test | ||
|
||
Write a test which verifies the function behaviour: | ||
|
||
test:FunctionRequestHandlerTest[] | ||
|
||
* When you instantiate the Handler, the application context starts. | ||
* Remember to close your application context when you end your test. You can use your handler to obtain it. | ||
* Invoke the `execute` method of the handler. | ||
|
||
common:testApp.adoc[] | ||
|
||
== Lambda | ||
|
||
Create a Lambda Function. As a runtime, select Java 17 (Correto). | ||
|
||
image::create-function.png[] | ||
|
||
Enable **function URLs** and set `Auth Type` as `NONE`. | ||
|
||
image::enable-function-url.png[] | ||
|
||
=== Upload Code | ||
|
||
common:executable-jar.adoc[] | ||
|
||
Upload it: | ||
|
||
image::upload-function-code.png[] | ||
|
||
=== Handler | ||
|
||
As Handler, set: | ||
|
||
`example.micronaut.FunctionRequestHandler` | ||
|
||
image::handler-2.png[] | ||
|
||
=== Obtain the Function URL | ||
|
||
You can obtain the Function URL in the AWS Console: | ||
|
||
image:aws-lambda-function-url.png[] | ||
|
||
Invoke it, via a cURL command: | ||
|
||
[source, bash] | ||
---- | ||
% curl -i https://xxxxxxxx.lambda-url.xxxx.on.aws/ | ||
{"message":"Hello World"} | ||
---- | ||
|
||
common:next.adoc[] | ||
|
||
Read more about: | ||
|
||
* https://micronaut-projects.github.io/micronaut-aws/latest/guide/#lambda[Micronaut AWS Lambda Support] | ||
|
||
* https://aws.amazon.com/lambda/[AWS Lambda] | ||
|
||
common:helpWithMicronaut.adoc[] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+337 KB
src/docs/images/cards/mn-serverless-function-aws-lambda-function-url.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.