All Projects → danapsimer → aws-api-to-lambda-shim

danapsimer / aws-api-to-lambda-shim

Licence: other
Using Either http://github.com/aws/aws-lambda-go/ or http://github.com/eawsy/aws-lambda-go/, this project provides a shim for calling go web services from AWS Api Gateway. Read my blog articles: https://goo.gl/PPocho, https://goo.gl/eg9CTQ

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to aws-api-to-lambda-shim

tug
Private Composer registry for private PHP packages on AWS Serverless
Stars: ✭ 33 (+6.45%)
Mutual labels:  aws-api-gateway
serverless-swagger-api
Simplifies the process of generating an API Gateway API from a swagger file.
Stars: ✭ 15 (-51.61%)
Mutual labels:  aws-api-gateway
a-crash-course-on-serverless-with-nodejs
A quick and easy guide of how to hook up a single Serverless service.
Stars: ✭ 28 (-9.68%)
Mutual labels:  aws-api-gateway
ecommerce-shopfront-on-aws
A high availability, API-first reference architecture for ecommerce using AWS services
Stars: ✭ 56 (+80.65%)
Mutual labels:  aws-api-gateway
desktop
A native GUI application that makes it easy to explore and test Serverless Framework applications built on AWS Lambda.
Stars: ✭ 42 (+35.48%)
Mutual labels:  aws-api-gateway
terraform-aws-api-gateway
Terraform module to create Route53 resource on AWS for create api gateway with it's basic elements.
Stars: ✭ 43 (+38.71%)
Mutual labels:  aws-api-gateway
express-sls-app
How to deploy a Node.js application to AWS Lambda using Serverless, a quick start.
Stars: ✭ 20 (-35.48%)
Mutual labels:  aws-api-gateway
resource-x
Resource and domain modeling for quick APIs, CMSs, and applications.
Stars: ✭ 26 (-16.13%)
Mutual labels:  aws-api-gateway

AWS API Gateway to Lambda Shim using http.Handler

This software is based, in part, on the work done by eawsy Their terrific idea was to use the Python 2.7 runtime available in AWS Lambda to run go programs. I was in the midst of creating a new API using goa and it dawned on me that if I could somehow trick goa into responding to AWS Api Gateway requests, I could have a service that could both run standalone and serverless on AWS's API Gateway and Lambda.

So I created this.

Now that AWS Lambda supports GO programs natively there is no reason to use eawsy but I have maintained support for it. I have updated the usage instructions to include examples for both AWS Native and eAWSy.

Usage

AWS Native

For complete details checkout the helloWorld example

The basic steps for usage are as follows:

NOTE: you need to replace the '${AWS_ACCOUNT_ID}' place holder in the swagger.json and the aws commands above with your account id.

  1. separate your configuration of your web service muxer from your call to http.ListenAndServe.

    package hello
    
    import (
        "net/http"
        ...
    )
    
    func InitHandler() (http.Handler, error) {
        mux := http.NewServeMux()
        mux.HandleFunc("/hello/", func(w http.ResponseWriter, req *http.Request) {
          ...
        })
        return mux, nil
    }
  2. Create your main() for your web service:

    package main
    
    import (
    	"github.com/danapsimer/aws-lambda-shim/examples/helloWorld/hello"
    	"log"
    	"net/http"
    )
    
    func main() {
    	handler, _ := hello.InitHandler()
    	log.Fatal(http.ListenAndServe(":8080", handler))
    }
  3. create your main() for your aws:

    package main
    import (
       "github.com/danapsimer/aws-lambda-shim/examples/helloWorld/hello"
       "github.com/danapsimer/aws-lambda-shim/aws"
    )
    func init() {
       shim.NewHttpHandlerShim(hello.InitHandler)
    }
    func main() {
    }
  4. Make your executable:

    build:
    	GOOS=linux go build -o handler
    
    pack:
    	zip handler.zip handler
  5. Create your lambda function in AWS: (in the directory your handler was built)

    aws lambda create-function \
        --function-name hello-world-api \
        --runtime go1.x --handler handler --zip-file fileb://handler.zip \
        --role arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda_basic_execution
  6. Create your API Gateway API:

    aws apigateway import-rest-api \
      --body file://examples/helloWorld/swagger.json --region us-east-1
    aws lambda add-permission --region us-east-1 \
      --function-name hello-world-api --statement-id 5 \
      --principal apigateway.amazonaws.com --action lambda:InvokeFunction \
      --source-arn 'arn:aws:execute-api:us-east-1:${AWS_ACCOUNT_ID}:3l3za8xwnd/*/*/*'

Eawsy

For complete details checkout the helloWorld example

The basic steps for usage are as follows:

NOTE: you need to replace the '${AWS_ACCOUNT_ID}' place holder in the swagger.json and the aws commands above with your account id.

  1. separate your configuration of your web service muxer from your call to http.ListenAndServe.

    package hello
    
    import (
        "net/http"
        ...
    )
    
    func InitHandler() (http.Handler, error) {
        mux := http.NewServeMux()
        mux.HandleFunc("/hello/", func(w http.ResponseWriter, req *http.Request) {
          ...
        })
        return mux, nil
    }
  2. Create your main() for your web service:

    package main
    
    import (
    	"github.com/danapsimer/aws-lambda-shim/examples/helloWorld/hello"
    	"log"
    	"net/http"
    )
    
    func main() {
    	handler, _ := hello.InitHandler()
    	log.Fatal(http.ListenAndServe(":8080", handler))
    }
  3. create your main() for your eawsy:

    package main
    
    import (
        "github.com/danapsimer/aws-lambda-shim/examples/helloWorld/hello"
        "github.com/danapsimer/aws-lambda-shim/eawsy"
    )
    
    func init() {
        shim.NewHttpHandlerShim(hello.InitHandler)
    }
    
    func main() {
    }
  4. Make your eawsy:

    build:
    	go build -buildmode=c-shared -ldflags="-w -s" -o handler.so
    
    pack:
    	zip handler.zip handler.so
  5. Create your eawsy in AWS: (in the directory your eawsy handler was built)

    aws lambda create-function \
        --function-name hello-world-api \
        --runtime python2.7 --handler handler.handle --zip-file fileb://handler.zip \
        --role arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda_basic_execution
  6. Create your API Gateway API:

    aws apigateway import-rest-api \
      --body file://examples/helloWorld/swagger.json --region us-east-1
    aws lambda add-permission --region us-east-1 \
      --function-name hello-world-api --statement-id 5 \
      --principal apigateway.amazonaws.com --action lambda:InvokeFunction \
      --source-arn 'arn:aws:execute-api:us-east-1:${AWS_ACCOUNT_ID}:3l3za8xwnd/*/*/*'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].