eawsy / aws-lambda-go
- вторник, 25 октября 2016 г. в 03:14:41
Go
Run standard Go code on the AWS Lambda platform.
AWS Lambda™ lets you run code without provisioning or managing servers. This project allows you to run vanilla Go code on the AWS Lambda platform.
package main
import (
"encoding/json"
"github.com/eawsy/aws-lambda-go/service/lambda/runtime"
)
func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
return "Hello, World!", nil
}
func init() {
runtime.HandleFunc(handle)
}
func main() {}
Before continuing, ensure that you have the GCC compiler and header files and libraries for Python development installed on your system. See Advanced Building section for more details.
go get -u -d github.com/eawsy/aws-lambda-go/...
go build -buildmode=c-shared -o handler.so
zip handler.zip handler.so
Python 2.7
as the runtime.handler.handle
as the handler.There are also some examples to play with
🎉 .
// ...
func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
// ...
log.Println("Hello, World!")
// ...
}
// ...
Logging is as simple as using the Go log package. You have
access to all the functions of the log package like
Print*
, Fatal*
or even Panic*
, and all the logs are available in the Lambda
function's CloudWatch log group and log stream with the usual format of the AWS
Lambda logs.
// ...
func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
// ...
return "Hello, World!", nil
}
// ...
You can return anything you want and that can be marshaled by the Go json package.
// ...
func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
// ...
return nil, errors.New("Oh, Snap!")
}
// ...
You can return any Go error you want.
You can access any runtime information of the
runtime.Context object.
You can access any exposed function of the
runtime.Context object, like
RemainingTimeInMillis
.
// ...
func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
// ...
select {
case <-time.After(500 * time.Millisecond):
log.Printf("Remaining time in ms: %d\n", ctx.RemainingTimeInMillis())
}
// ...
}
// ...
This project uses cgo and Python C extension to provide a seamless intergration between AWS Lambda Python 2.7 runtime and Go code. This is how we managed to create one and only one binary to deploy on the AWS Lambda platform.
Important: The output filename MUST be
handler.so
.
# Normal build
go build -buildmode=c-shared -o handler.so
# Size optimized build (~30% gain)
go build -buildmode=c-shared -ldflags="-w -s" -o handler.so
Even if it is not visible in the above command line, Go uses the GCC compiler and header files and libraries for Python development. Also, you need to have these dependencies installed on your system.
# For Debian families
sudo apt-get install build-essentials pkg-config python-dev
# For Redhat families
sudo dnf groupinstall 'Development Tools'
sudo dnf install pkgconfig python-devel
For those who have not yet a Linux OS or who do not want to install these dependencies, we also provide a ready to use Docker image along with its Dockerfile.
Do not forget to take a tour in the examples to see how it works.
This project is maintained and funded by Alsanium, SAS.
We
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.
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.