Skip to content

Commit

Permalink
Merge pull request #59 from shogo82148/add-streaming-to-readme
Browse files Browse the repository at this point in the history
add Lambda function URLs with a Response Streaming Enabled Function s…
  • Loading branch information
shogo82148 authored Aug 6, 2023
2 parents 196c1ee + 8201c49 commit e8d085c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 38 deletions.
146 changes: 113 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[![GoDoc](https://godoc.org/github.com/shogo82148/ridgenative?status.svg)](https://godoc.org/github.com/shogo82148/ridgenative)

# ridgenative

AWS Lambda HTTP Proxy integration event bridge to Go net/http.
[fujiwara/ridge](https://github.com/fujiwara/ridge) is a prior work, but it depends on [Apex](http://apex.run/).
I want same one that only depends on [aws/aws-lambda-go](https://github.com/aws/aws-lambda-go).
Expand Down Expand Up @@ -49,8 +50,8 @@ Here is an example of [AWS Serverless Application Model template](https://github
See [the example directory](https://github.com/shogo82148/ridgenative/tree/main/example) to how to deploy it.

```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: example of shogo82148/ridgenative
Resources:
ExampleApi:
Expand All @@ -73,8 +74,8 @@ Resources:
You can also run it as an [Amazon API Gateway HTTP API](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api.html).
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: example of shogo82148/ridgenative
Resources:
ExampleApi:
Expand Down Expand Up @@ -111,23 +112,23 @@ Resources:
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: CloudWatchLogs
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: CloudWatchLogs
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"

LambdaPermission:
Type: AWS::Lambda::Permission
Expand All @@ -142,7 +143,6 @@ Resources:
TargetType: lambda
Targets:
- Id: !Att Function.Arn

# Configure listener rules of ALB to forward to the LambdaTargetGroup.
# ...
```
Expand Down Expand Up @@ -175,23 +175,103 @@ Resources:
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: CloudWatchLogs
PolicyDocument:
Statement:
- PolicyName: CloudWatchLogs
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
```
### Lambda function URLs with a Response Streaming Enabled Function
The ridgenative also works with [a response streaming enabled function](https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html#config-rs-invoke-furls).
To enable response streaming, set the `RIDGENATIVE_INVOKE_MODE` environment value to `RESPONSE_STREAM`.

```yaml
AWSTemplateFormatVersion: "2010-09-09"
Resources:
Function:
Type: AWS::Lambda::Function
Properties:
Code: dist
Handler: example
Role: !GetAtt ExecutionRole.Arn
Runtime: provided.al2
Timeout: 30
# configure environment values to enable response streaming
Environment:
Variables:
RIDGENATIVE_INVOKE_MODE: RESPONSE_STREAM
LambdaUrls:
Type: AWS::Lambda::Url
Properties:
AuthType: NONE
TargetFunctionArn: !GetAtt Function.Arn
InvokeMode: RESPONSE_STREAM # enables response streaming
ExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: CloudWatchLogs
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
```

With a response streaming enabled function, the ResponseWriter implements `http.Flusher`.

```go
package main
import (
"fmt"
"net/http"
"github.com/shogo82148/ridgenative"
)
func main() {
http.HandleFunc("/hello", handleRoot)
ridgenative.ListenAndServe(":8080", nil)
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Hello World")
f := w.(http.Flusher)
f.Flush()
}
```

## RELATED WORKS
Expand Down
2 changes: 1 addition & 1 deletion examples/apigateway-v2/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# API Gateway V2 example of ridgenative

It is a saying hello example on API Gateway V1.
It is a saying hello example on API Gateway V2.

```
$ curl -F name=shogo https://xxxxxxx.execute-api.ap-northeast-1.amazonaws.com/hello
Expand Down
8 changes: 4 additions & 4 deletions examples/function-urls/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# API Gateway V2 example of ridgenative
# Lambda Function URLs example of ridgenative

It is a saying hello example on API Gateway V1.
It is a saying hello example on Lambda Function URLs.

```
$ curl -F name=shogo https://xxxxxxx.lambda-url.ap-northeast-1.on.aws/hello
Expand All @@ -17,10 +17,10 @@ $ curl -F name=shogo http://localhost:8080/hello
Hello shogo
```

## Run on AWS Lambda with API Gateway
## Run on AWS Lambda

[template.yaml](template.yaml) is [AWS Serverless Application Model template](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md) file.
You can run this sample on AWS with API gateway proxy integration.
You can run this sample on AWS with Lambda Function URLs.

```
$ ./deploy.sh $YOUR_S3_BUCKET_NAME $YOUR_STACK_NAME
Expand Down

0 comments on commit e8d085c

Please sign in to comment.