Skip to content

Commit 88d2e2a

Browse files
authored
SpringBoot Response Streaming using function url (#237)
* Adding a new example that uses springboot for streaming * Updated readme and added the sample output. * Updated readme and added the sample output. * .DS_Store banished! * .DS_Store banished! * Updated readme and added the sample output. * Optimized imports * Optimized imports
1 parent 9a88d2b commit 88d2e2a

File tree

14 files changed

+857
-0
lines changed

14 files changed

+857
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
HELP.md
2+
target/
3+
!app/.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
.DS_Store
16+
17+
### IntelliJ IDEA ###
18+
.idea
19+
*.iws
20+
*.iml
21+
*.ipr
22+
23+
### NetBeans ###
24+
/nbproject/private/
25+
/nbbuild/
26+
/dist/
27+
/nbdist/
28+
/.nb-gradle/
29+
build/
30+
!**/src/main/**/build/
31+
!**/src/test/**/build/
32+
33+
### VS Code ###
34+
.vscode/
35+
36+
.mvn
37+
.aws-sam
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11.0.14
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Spring Boot 2 Zip example
2+
3+
A basic file streaming application written with the Spring Boot 2 framework. You can build and test it locally as a typical Spring Boot 2 application.
4+
5+
The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yaml` file in the root folder contains the application definition.
6+
7+
To run the application we are using the run.sh script located in the resources folder:
8+
9+
```shell
10+
#!/bin/sh
11+
12+
exec java -cp "./:lib/*" "com.amazonaws.demo.stream.Application"
13+
```
14+
15+
In the configuration we have to specify the AWS Lambda adapter as a layer and configure the script as handler:
16+
17+
```yaml
18+
Properties:
19+
MemorySize: 512
20+
Handler: run.sh
21+
CodeUri: app/
22+
Runtime: java11
23+
AutoPublishAlias: live
24+
SnapStart:
25+
ApplyOn: PublishedVersions
26+
Environment:
27+
Variables:
28+
RUST_LOG: info
29+
READINESS_CHECK_PATH: /healthz
30+
REMOVE_BASE_PATH: /v1
31+
AWS_LAMBDA_EXEC_WRAPPER: /opt/bootstrap
32+
AWS_LWA_INVOKE_MODE: response_stream
33+
Layers:
34+
- !Sub arn:aws:lambda:${AWS::Region}:753240598075:layer:LambdaAdapterLayerX86:16
35+
```
36+
In this template, we enable SnapStart for this function. SnapStart drastically reduces cold start time for Java functions using Firecracker MicroVM snapshotting technology. Read more about SnapStart [here](https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html).
37+
38+
### Remove the base path
39+
40+
The file stream application is deployed under /v1/{proxy+}. But the application does not know that. So in the SAM template file, we configured environment variable `REMOVE_BASE_PATH=/v1`.
41+
This configuration tells the Adapter to remove `/v1` from http request path, so that the pet store application works without changing code.
42+
43+
44+
## Pre-requisites
45+
46+
The following tools should be installed and configured.
47+
48+
* [AWS CLI](https://aws.amazon.com/cli/)
49+
* [SAM CLI](https://github.com/awslabs/aws-sam-cli)
50+
* [Maven](https://maven.apache.org/)
51+
* [Docker](https://www.docker.com/products/docker-desktop)
52+
53+
## Deploy to Lambda
54+
Navigate to the sample's folder and use the SAM CLI to build the application:
55+
56+
```shell
57+
$ sam build
58+
```
59+
60+
This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.
61+
62+
To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen
63+
64+
```shell
65+
$ sam deploy --guided
66+
```
67+
68+
Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new application URL. You can use `curl` or a web browser to make a call to the URL
69+
70+
```shell
71+
...
72+
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
73+
Outputs
74+
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
75+
Key StreamFilesFunctionUrl
76+
Description Function URL for StreamFiles function to stream a dummy 15mb file
77+
Value https://abcdxxxxxxxxxxxx.lambda-url.us-west-2.on.aws/stream-dummy?size=15
78+
79+
Key StreamFilesFunction
80+
Description StreamFilesFunction Lambda Function ARN
81+
Value arn:aws:lambda:us-west-2:111111111111:function:spring-wa-StreamFilesFunction-abcdxxxxxxxxxxxx
82+
...
83+
84+
$ curl https://abcdxxxxxxxxxxxx.lambda-url.us-west-2.on.aws/stream-dummy?size=15
85+
86+
To stream a preexisting mp4 file
87+
88+
$ curl https://abcdxxxxxxxxxxxx.lambda-url.us-west-2.on.aws/stream
89+
```
90+
91+
## Clean up
92+
93+
This example use provisioned concurrency to reduce cold start time. It incurs additional cost. You can remove the whole example with the following command.
94+
95+
```shell
96+
sam delete
97+
```

0 commit comments

Comments
 (0)