Skip to content

Commit 8f514b9

Browse files
authored
Merge pull request aws-samples#1637 from msailes/scf-update
2 parents 618954a + 0ab2215 commit 8f514b9

4 files changed

Lines changed: 95 additions & 2 deletions

File tree

apigw-lambda-snapstart/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@ to improve startup performance.
88

99
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-lambda-snapstart.
1010

11+
## Spring Web support in Lambda
12+
13+
This pattern uses the Spring Web ```@Controller``` to support different REST endpoints in the same Lambda function.
14+
There are two supported ways to achieve this with the AWS Serverless Java Container.
15+
16+
### Delegating the handler to Spring
17+
18+
The first is to specify a Spring class as the function handler and reference the SpringBootApplication class as an
19+
environment variable.
20+
21+
```yaml
22+
Type: AWS::Serverless::Function
23+
Properties:
24+
Handler: com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler
25+
...
26+
Environment:
27+
Variables:
28+
MAIN_CLASS: com.unicorn.store.StoreApplication
29+
```
30+
31+
This is useful as you don't need to make any changes to your existing application to run it in Lambda.
32+
33+
### Starting Spring in your own handler
34+
35+
An alternative is to create your own handler and start Spring within it. The class ```StreamLambdaHandler``` is an
36+
example of this.
37+
38+
This is useful when you want control over the handler method, for example to add an annotation.
39+
1140
## Requirements
1241

1342
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.

apigw-lambda-snapstart/UnicornFunction/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.amazonaws.serverless</groupId>
4747
<artifactId>aws-serverless-java-container-springboot3</artifactId>
48-
<version>2.0.0-M1</version>
48+
<version>2.0.0-M2</version>
4949
</dependency>
5050

5151
<dependency>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"title": "Synchronous REST API with Lambda SnapStart",
3+
"description": "This pattern demonstrates how to create a synchronous REST API using API Gateway, AWS Lambda and DynamoDB.",
4+
"language": "Java",
5+
"level": "400",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern is built using [Spring Boot 3](https://spring.io/projects/spring-boot) and leverages the [AWS Serverless Java Container](https://github.com/awslabs/aws-serverless-java-container) for seamless integration with AWS Lambda.",
11+
"This pattern also demonstrates the usage of [SnapStart](https://aws.amazon.com/blogs/compute/reducing-java-cold-starts-on-aws-lambda-functions-with-snapstart/) to improve startup performance."
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-snapstart",
17+
"templateURL": "serverless-patterns/apigw-lambda-snapstart",
18+
"projectFolder": "apigw-lambda-snapstart",
19+
"templateFile": "template.yaml"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Lambda Replatforming Guide",
26+
"link": "https://serverlessland.com/content/service/lambda/paved-path/java-replatforming/introduction"
27+
},
28+
{
29+
"text": "AWS Serverless Java Container",
30+
"link": "https://github.com/awslabs/aws-serverless-java-container/"
31+
},
32+
{
33+
"text": "Lambda SnapStart",
34+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html"
35+
}
36+
]
37+
},
38+
"deploy": {
39+
"text": [
40+
"sam build",
41+
"sam deploy --stack-name unicorn-store --guided"
42+
]
43+
},
44+
"testing": {
45+
"text": [
46+
"See the GitHub repo for detailed testing instructions."
47+
]
48+
},
49+
"cleanup": {
50+
"text": [
51+
"Delete the stack: <code>sam delete</code>."
52+
]
53+
},
54+
"authors": [
55+
{
56+
"name": "Mark Sailes",
57+
"image": "https://serverlessland.com/assets/images/resources/contributors/mark-sailes.jpg",
58+
"bio": "Mark is a specialist solutions architect working at AWS. His primary focus is improving the developer experience of builders using Java on AWS Lambda.",
59+
"linkedin": "mark-sailes",
60+
"twitter": "MarkSailes3"
61+
}
62+
]
63+
}

apigw-lambda-snapstart/template.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ Resources:
1818
UnicornStoreSpringFunction:
1919
Type: AWS::Serverless::Function
2020
Properties:
21-
Handler: com.unicorn.store.StreamLambdaHandler
21+
Handler: com.amazonaws.serverless.proxy.spring.SpringDelegatingLambdaContainerHandler
2222
CodeUri: UnicornFunction
2323
Tracing: Active
2424
Environment:
2525
Variables:
26+
MAIN_CLASS: com.unicorn.store.StoreApplication
2627
AWS_SERVERLESS_JAVA_CONTAINER_INIT_GRACE_TIME: 500
2728
UNICORN_TABLE_NAME: !Ref UnicornTable
2829
POWERTOOLS_LOG_LEVEL: INFO

0 commit comments

Comments
 (0)