Skip to content

Commit

Permalink
Package awsgateway lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
Traun Leyden committed Feb 25, 2017
1 parent 3e2f908 commit a1ffe99
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

## Deploy to AWS Lambda

Package the lambda function

```
$ wget -O Makefile https://github.com/eawsy/aws-lambda-go-shim/raw/master/src/Makefile.example
$ make
```

Deploy cloudformation with lambda function

```
$ aws cloudformation package \
--template-file aws_serverless_application_model.yaml \
--output-template-file aws_serverless_application_model.out.yaml \
--s3-bucket cf-templates-1m70kn8ou9eql-us-east-1
$ aws cloudformation deploy \
--template-file aws_serverless_application_model.out.yaml \
--capabilities CAPABILITY_IAM \
--stack-name CBBootstrapExperiment \
--region us-east-1
```

Get REST API endpoint

```
$ aws cloudformation describe-stacks \
--stack-name CBBootstrapExperiment \
--region us-east-1 | grep -i OutputValue
"OutputValue": "https://5e61vqxs5f.execute-api.us-east-1.amazonaws.com/Prod"
```

Test endpoint

```
$ curl https://5e61vqxs5f.execute-api.us-east-1.amazonaws.com/Prod
```

## REST API Definition

Endpoints

- POST /cluster/add_or_create

This creates or updates a cluster object in the system. If it’s the first node in this cluster (defined by cluster_id), then

Request

{
“cluster_id”: “safdasdf3234",
“node_ip_addr_or_hostname”: “ip-233.11.2.5"
}

Response

{
“cluster_already_initialized”: true | false, // if false, then this node becomes the initial node that other nodes try to join
“initial_node_ip_addr_or_hostname”: “ip-233.11.2.5”, // empty if cluster_already_initialized
"all_known_nodes": [
{
“node_ip_addr_or_hostname”: “ip-233.11.2.5",
"last_seen": <date>
},
{
“node_ip_addr_or_hostname”: “ip-233.11.2.18",
"last_seen": <date>
},
]

}


23 changes: 23 additions & 0 deletions aws_serverless_application_model.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
Function:
Type: AWS::Serverless::Function
Properties:
Handler: handler.Handle
Runtime: python2.7
CodeUri: ./package.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"
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package main

// /* Required by eawsy/aws-lambda-go-net */
import "C"

import (
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws"
"log"
"fmt"
"github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net"
"github.com/eawsy/aws-lambda-go-net/service/lambda/runtime/net/apigatewayproxy"
"net/http"
)

/*
Expand Down Expand Up @@ -103,4 +109,24 @@ func main() {

log.Printf("done")

}
}


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

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

// Amazon API Gateway Binary support out of the box.
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, etc.
go http.Serve(ln, http.HandlerFunc(handle))
}

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

0 comments on commit a1ffe99

Please sign in to comment.