Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Created September 4, 2017 00:18
Show Gist options
  • Save Luzifer/817520eb161141d569cf1ec064234793 to your computer and use it in GitHub Desktop.
Save Luzifer/817520eb161141d569cf1ec064234793 to your computer and use it in GitHub Desktop.
Test of eawsy/aws-lambda-go-net serverless Go deployment
handler.so
handler.zip
deploy.out.yml
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"
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(res http.ResponseWriter, r *http.Request) {
res.Header().Set("Content-Type", "text/html")
res.Write([]byte("<strong>Hello, World!</strong>"))
}
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
#
HANDLER ?= handler
PACKAGE ?= $(HANDLER)
ifeq ($(OS),Windows_NT)
GOPATH ?= $(USERPROFILE)/go
GOPATH := /$(subst ;,:/,$(subst \,/,$(subst :,,$(GOPATH))))
CURDIR := /$(subst :,,$(CURDIR))
RM := del /q
else
GOPATH ?= $(HOME)/go
RM := rm -f
endif
MAKEFILE = $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
docker:
docker run --rm -i \
-e HANDLER=$(HANDLER) \
-e PACKAGE=$(PACKAGE) \
-e GOPATH=$(GOPATH) \
-e LDFLAGS='$(LDFLAGS)' \
-v $(CURDIR):$(CURDIR) \
$(foreach GP,$(subst :, ,$(GOPATH)),-v $(GP):$(GP)) \
-w $(CURDIR) \
eawsy/aws-lambda-go-shim:latest make -f $(MAKEFILE) all
.PHONY: docker
all: build pack perm
.PHONY: all
build:
go build -buildmode=plugin -ldflags='-w -s $(LDFLAGS)' -o $(HANDLER).so
.PHONY: build
pack:
pack $(HANDLER) $(HANDLER).so $(PACKAGE).zip
.PHONY: pack
perm:
chown $(shell stat -c '%u:%g' .) $(HANDLER).so $(PACKAGE).zip
.PHONY: perm
clean:
$(RM) $(HANDLER).so $(PACKAGE).zip
.PHONY: clean
### --- ADJUSTED --- ###
STACK_NAME ?= $(notdir $(CURDIR))
SERVERLESS_BUCKET ?= nobucket
deploy: docker
aws cloudformation package \
--template-file deploy.yml \
--output-template-file deploy.out.yml \
--s3-bucket $(SERVERLESS_BUCKET)
aws cloudformation deploy \
--template-file deploy.out.yml \
--capabilities CAPABILITY_IAM \
--stack-name $(STACK_NAME)
aws cloudformation describe-stacks \
--stack-name $(STACK_NAME) \
--query Stacks[0].Outputs[0]
undeploy:
aws cloudformation delete-stack \
--stack-name $(STACK_NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment