diff --git a/README.md b/README.md index 61c8d53..e5bf2aa 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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: @@ -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: @@ -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 @@ -142,7 +143,6 @@ Resources: TargetType: lambda Targets: - Id: !Att Function.Arn - # Configure listener rules of ALB to forward to the LambdaTargetGroup. # ... ``` @@ -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 diff --git a/examples/apigateway-v2/README.md b/examples/apigateway-v2/README.md index d5ed2d9..e483de8 100644 --- a/examples/apigateway-v2/README.md +++ b/examples/apigateway-v2/README.md @@ -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 diff --git a/examples/function-urls/README.md b/examples/function-urls/README.md index c77073f..28a99d8 100644 --- a/examples/function-urls/README.md +++ b/examples/function-urls/README.md @@ -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 @@ -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