github

eawsy / aws-lambda-go

  • вторник, 25 октября 2016 г. в 03:14:41
https://github.com/eawsy/aws-lambda-go

Go
Run standard Go code on the AWS Lambda platform.



Powered by Amazon Web Services Created by eawsy

AWS Lambda - Go

Runtime Api Chat Status License Hire us

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.

Usage

Hello, World!

Authoring

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() {}

Building

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

Packaging

zip handler.zip handler.so

Deploying

  1. Create a Lambda function.
  2. Use Python 2.7 as the runtime.
  3. Use handler.handle as the handler.
  4. Voilà c'est eawsy!

There are also some examples to play with 🎉.

Advanced

Logging

// ...
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.

CloudWatch logs screenshot

Execution

Success

// ...
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.

Successful function execution screenshot

Failure

// ...
func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
  // ...
  return nil, errors.New("Oh, Snap!")
}
// ...

You can return any Go error you want.

Failed function execution screenshot

Context

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())
  }
  // ...
}
// ...

Building

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 :trollface: 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.

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.

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.