Giter Site home page Giter Site logo

lifadev / archive_aws-lambda-go-net Goto Github PK

View Code? Open in Web Editor NEW
151.0 12.0 11.0 602 KB

Network I/O interface for AWS Lambda Go runtime.

Home Page: https://github.com/eawsy/aws-lambda-go-net

License: Apache License 2.0

Go 60.08% Makefile 39.92%
aws aws-lambda aws-apigateway apigateway golang go http server serverless

archive_aws-lambda-go-net's Introduction

Powered by Amazon Web Services Created by eawsy

eawsy/aws-lambda-go-net

Amazon API Gateway proxy for AWS Lambda Go runtime.

Api Status License Help Social

AWS Lambda lets you run code without provisioning or managing servers. With eawsy/aws-lambda-go-shim, you can author your Lambda function code in Go. This project provides a seamless proxy layer which translates Amazon API Gateway requests to Go HTTP requests. It allows you for example to port your existing Go HTTP applications to Amazon API Gateway & AWS Lambda, without modification.

Quick Hands-On

For step by step instructions on how to author your AWS Lambda function code in Go, see eawsy/aws-lambda-go-shim. Vanilla Go net/http package is used for the sake of simplicity. You are free to use your favorite Go Web framework.

Dependencies

go get -u -d github.com/eawsy/aws-lambda-go-net/...

Create

package main

import (
	"net/http"

	"github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net"
	"github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy"
)

// Handle is the exported handler called by AWS Lambda.
var Handle apigatewayproxy.Handler

func init() {
	ln := net.Listen()

	// Amazon API Gateway binary media types are supported out of the box.
	// If you don't send or receive binary data, you can safely set it to nil.
	Handle = apigatewayproxy.New(ln, []string{"image/png"}).Handle

	// Any Go framework complying with the Go http.Handler interface can be used.
	// This includes, but is not limited to, Vanilla Go, Gin, Echo, Gorrila, Goa, etc.
	go http.Serve(ln, http.HandlerFunc(handle))
}

func handle(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, World!"))
}

Build

For step by step instructions on how to author your AWS Lambda function code in Go, see eawsy/aws-lambda-go-shim.

make

Deploy

AWS Serverless Application Model (SAM) is used for the sake of simplicity. You are free to use your favorite deployment tool.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  Function:
    Type: AWS::Serverless::Function
    Properties:
      Handler: handler.Handle
      Runtime: python2.7
      CodeUri: ./handler.zip
      Events:
        ApiRoot:
          Type: Api
          Properties:
            Path: /
            Method: ANY
        ApiGreedy:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: ANY
Outputs:
  URL:
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"
aws cloudformation package \
  --template-file example.sam.yaml \
  --output-template-file example.out.yaml \
  --s3-bucket <YOUR BUCKET NAME>

aws cloudformation deploy \
  --template-file example.out.yaml \
  --capabilities CAPABILITY_IAM \
  --stack-name <YOUR STACK NAME>

Invoke

aws cloudformation describe-stacks \
  --stack-name <YOUR STACK NAME> \
  --query Stacks[0].Outputs[0]

# "https://<YOUR API URL>/"

curl https://<YOUR API URL>/

# Hello, World!

If you want to execute your AWS Lambda function from the AWS Lambda console, please pay attention to the provided test event. This project is intended to run seamlessly behind an Amazon API Gateway Proxy.

About

eawsy

This project is maintained and funded by Alsanium, SAS.

We ❤️ AWS and open source software. See our other projects, or hire us to help you build modern applications on AWS.

Contact

We want to make it easy for you, users and contributers, to talk with us and connect with each others, to share ideas, solve problems and make help this project awesome. Here are the main channels we're running currently and we'd love to hear from you on them.

Twitter

eawsyhq

Follow and chat with us on Twitter.

Share stories!

Gitter

eawsy/bavardage

This is for all of you. Users, developers and curious. You can find help, links, questions and answers from all the community including the core team.

Ask questions!

GitHub

pull requests & issues

You are invited to contribute new features, fixes, or updates, large or small; we are always thrilled to receive pull requests, and do our best to process them as fast as we can.

Before you start to code, we recommend discussing your plans through the eawsy/bavardage channel, especially for more ambitious contributions. This gives other contributors a chance to point you in the right direction, give you feedback on your design, and help you find out if someone else is working on the same thing.

Write code!

License

This product is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this product except in compliance with the License. See LICENSE and NOTICE for more information.

Trademark

Alsanium, eawsy, the "Created by eawsy" logo, and the "eawsy" logo are trademarks of Alsanium, SAS. or its affiliates in France and/or other countries.

Amazon Web Services, the "Powered by Amazon Web Services" logo, and AWS Lambda are trademarks of Amazon.com, Inc. or its affiliates in the United States and/or other countries.

archive_aws-lambda-go-net's People

Contributors

fsenart avatar lion3ls avatar tleyden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

archive_aws-lambda-go-net's Issues

Possibly obvious question about concurrency of the server

Hello, forgive me if my question's answer is obvious but it seems I am missing something.

I saw that the listener's Accept() method is protected by a channel (inc). This implies that only one request can be handled each time, hence one Lambda invocation right?
Why do you have this limitation? Can't we have the net package declare new variables for each request instead of resetting the global vars?

Thanks

Crashes with the latest shim version

I used the latest example code and I'm getting this crash:


23:56:13
START RequestId: d10de6a0-ffa3-11e6-9a01-0108fefdc89d Version: $LATEST

23:56:13
panic: runtime error: invalid memory address or nil pointer dereference

23:56:13
[signal SIGSEGV: segmentation violation code=0x1 addr=0x70 pc=0x7f01b6396db3]

23:56:13
goroutine 17 [running, locked to thread]:

23:56:13
github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy.(*Server).Handle(0xc420014ee0, 0xc420014f30, 0x2, 0x8, 0xc420072280, 0x0, 0xc4200c7940, 0x7f01bcad0e6c)

23:56:13
/go/src/github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy/server.go:87 +0x1c3

23:56:13
github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy.(*Server).Handle-fm(0xc420014f30, 0x2, 0x8, 0xc420072280, 0x0, 0x0, 0x0)

23:56:13
/tmp/handler.go:17 +0x54

23:56:13
reflect.Value.call(0x7f01b66445a0, 0x7f01b67dc440, 0x193, 0x7f01bcb90db8, 0x4, 0xc4200c7de0, 0x2, 0x2, 0xc4200bb680, 0x7f01bce59850, ...)

23:56:13
/usr/local/go/src/reflect/value.go:434 +0x921

23:56:13
reflect.Value.Call(0x7f01b66445a0, 0x7f01b67dc440, 0x193, 0xc4200c7de0, 0x2, 0x2, 0x0, 0x193, 0x13)

23:56:13
/usr/local/go/src/reflect/value.go:302 +0xa6

23:56:13
main.handle(0x7f01be09c054, 0x2161694, 0x20c5a94, 0xc400000008)

23:56:13
/tmp/src/runtime.go:127 +0x5ff

23:56:13
main._cgoexpwrap_7d7ac51857d8_handle(0x7f01be09c054, 0x2161694, 0x20c5a94, 0x0)

23:56:13
_/tmp/src/_obj/_cgo_gotypes.go:125 +0x74

23:56:13
END RequestId: d10de6a0-ffa3-11e6-9a01-0108fefdc89d

23:56:13
REPORT RequestId: d10de6a0-ffa3-11e6-9a01-0108fefdc89d	Duration: 30.44 ms	Billed Duration: 100 ms Memory Size: 1024 MB	Max Memory Used: 17 MB

23:56:13
RequestId: d10de6a0-ffa3-11e6-9a01-0108fefdc89d Process exited before completing request

redirect not working?

Hi,

Am I correct that aws-lambda-go-net is not passing on a redirect properly to the client?

I think it gets stuck in this component and doing a redirect locally ...

Cheers
Gerd

'Process exited before completing request'

I get this error when I invoke the function ?

It happens in my test with the basic example code:
https://github.com/eawsy/aws-lambda-go-net/tree/master/_example/basic

If I invoke the function I get the error. It does not matter if I invoke from the commandline or using the API Gateway. Both give the same error.

Below the log of one test request, from both the API Gateway and AWS Lambda.

Any ideas what could go wrong ?

Execution log for request test-request
Fri Aug 04 18:55:13 UTC 2017 : Starting execution for request: test-invoke-request
Fri Aug 04 18:55:13 UTC 2017 : HTTP Method: GET, Resource Path: /
Fri Aug 04 18:55:13 UTC 2017 : Method request path: {}
Fri Aug 04 18:55:13 UTC 2017 : Method request query string: {}
Fri Aug 04 18:55:13 UTC 2017 : Method request headers: {}
Fri Aug 04 18:55:13 UTC 2017 : Method request body before transformations:
Fri Aug 04 18:55:13 UTC 2017 : Endpoint request URI: https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:061029286405:function:golang-preview/invocations
Fri Aug 04 18:55:13 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=*********************************************************************************************************************************************************************************************************************************************************************************************************dbd72d, X-Amz-Date=20170804T185513Z, x-amzn-apigateway-api-id=c49ynl0n82, X-Amz-Source-Arn=arn:aws:execute-api:eu-central-1:061029286405:c49ynl0n82/null/GET/, Accept=application/json, User-Agent=AmazonAPIGateway_c49ynl0n82, X-Amz-Security-Token=[TRUNCATED]
Fri Aug 04 18:55:13 UTC 2017 : Endpoint request body after transformations:
Fri Aug 04 18:55:13 UTC 2017 : Sending request to https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:061029286405:function:golang-preview/invocations
Fri Aug 04 18:55:13 UTC 2017 : Received response. Integration latency: 112 ms
Fri Aug 04 18:55:13 UTC 2017 : Endpoint response body before transformations: {"errorMessage":"RequestId: 72a77d10-7946-11e7-b375-efd5be3a9346 Process exited before completing request"}
Fri Aug 04 18:55:13 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=72a77d10-7946-11e7-b375-efd5be3a9346, Connection=keep-alive, Content-Length=107, X-Amz-Function-Error=Unhandled, Date=Fri, 04 Aug 2017 18:55:13 GMT, X-Amzn-Trace-Id=root=1-5984c311-5177729f4c5c6d7167442086;sampled=0, Content-Type=application/json}
Fri Aug 04 18:55:13 UTC 2017 : Method response body after transformations: {"errorMessage":"RequestId: 72a77d10-7946-11e7-b375-efd5be3a9346 Process exited before completing request"}
Fri Aug 04 18:55:13 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-5984c311-5177729f4c5c6d7167442086, Content-Type=application/json}
Fri Aug 04 18:55:13 UTC 2017 : Successfully completed execution
Fri Aug 04 18:55:13 UTC 2017 : Method completed with status: 200

AWS Lambda:

START RequestId: 72a77d10-7946-11e7-b375-efd5be3a9346 Version: $LATEST
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x70 pc=0x7f0f111bccc0]
goroutine 17 [running, locked to thread]:
github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy.(*Server).Handle(0xc420114460, 0xc42008edc0, 0x2, 0x8, 0xc4200ca200, 0x0, 0xc4200e7958, 0x7f0f137c6d0c)
/go/src/github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy/server.go:115 +0xef0
github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy.(*Server).Handle-fm(0xc42008edc0, 0x2, 0x8, 0xc4200ca200, 0x0, 0x0, 0x0)
/go/src/bitbucket.org/greulen/lambda-hello/main.go:15 +0x54
reflect.Value.call(0x7f0f1146aac0, 0x7f0f11604440, 0x193, 0x7f0f13887078, 0x4, 0xc4200e7e10, 0x2, 0x2, 0x7f0f13ab99c0, 0x7f0f13ae1368, ...)
/usr/local/go/src/reflect/value.go:434 +0x921
reflect.Value.Call(0x7f0f1146aac0, 0x7f0f11604440, 0x193, 0xc4200e7e10, 0x2, 0x2, 0xc4200ca278, 0x193, 0x7f0f137ddb48)
/usr/local/go/src/reflect/value.go:302 +0xa6
main.handle(0x7f0f13d91cd4, 0x7f0f17607254, 0xf15c04, 0xc4200e7eb0, 0xc4200e7eb8, 0x0)
/build/src/runtime.go:140 +0x4e1
main._cgoexpwrap_dd780bcacc70_handle(0x7f0f13d91cd4, 0x7f0f17607254, 0xf15c04, 0x0, 0x0, 0x0)
_/build/src/_obj/_cgo_gotypes.go:127 +0x9a
END RequestId: 72a77d10-7946-11e7-b375-efd5be3a9346
REPORT RequestId: 72a77d10-7946-11e7-b375-efd5be3a9346 Duration: 39.40 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 25 MB
RequestId: 72a77d10-7946-11e7-b375-efd5be3a9346 Process exited before completing request

Why init not main?

All the examples start the http server from inside init() func instead of the usually used main() func. Is there a reason behind this? And if yes, could this be documented?

Cut a release?

Hi, can you cut a release so I don't have to deploy master?

Thanks!

Why listen and serve instead of handling requests in-memory?

Hi, I may be missing something here so correct me if I'm wrong. What's the reason behind proxying requests and responses over an additional TCP socket when both apigatewayproxy.Handle and a developer's http.Handler are operating in the same process?

Can't we just build an http.ResponseWriter, hand it off to a registered http.Handler and transform it into an *apigatewayproxy.Response? Even better, couldn't apigatewayproxy.Response implement http.ResponseWriter? There wouldn't be any additional networking, in either case.

Looking forward to your clarification! 😄

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.