lifadev / archive_aws-lambda-go-shim

Author your AWS Lambda functions in Go, effectively.
https://github.com/eawsy/aws-lambda-go-shim
Apache License 2.0
789 stars 66 forks source link

Running exec.Command within Lambda #24

Closed smashedtoatoms closed 7 years ago

smashedtoatoms commented 7 years ago

Is it possible to package and run executables using this? I can't seem to get it to work. I have scenarios where I need to package and run an executable as part of a lambda call (git for instance) and I can't seem to get it to find my executable.

fsenart commented 7 years ago

@smashedtoatoms you can write a handler like this:

package main

func Handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
// ...
}

func main() {
// do whatever you need
// call Handle(...)
// etc
}

You need to make some kind of conditional build, like when you want to deploy to Lambda you build with the docker image which produced a .so file, and when you need a self contained executable you build the program like always with go build.

There is this project https://github.com/cristim/autospotting by @cristim which adopts this kind of approach.

PS: When deploying to Lambda, anything in the main function is stripped by the compiler and there is no overhead.

fsenart commented 7 years ago

Mmm I think I've read your question too fast. You want to run something from the Lambda? So yes using exec.Command, etc should do the job. Have you any problem doing this kind of stuff or do you need an example? If the problem comes from the packaging process, you need to customize the provided Makefile to add your third-party executable to the package:

#...

pack:
    @pack $(HANDLER) $(HANDLER).so $(PACKAGE).zip
    @zip -ru $(PACKAGE).zip any_third_party_dir/ or_file1 or_file2
#...
smashedtoatoms commented 7 years ago

It's pebcak. It's the way I was calling it. I was botching the pathing. It works perfectly. I thought that maybe there was something where lambda was limiting go's ability to fork exec, but as you were typing your response, I got it working.

This library is incredible, by the way. I know it doesn't necessarily mean much, but this: Duration: 0.87 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB happens sometimes. It's so fast, and so light.

smashedtoatoms commented 7 years ago

Great work all around, and thanks for taking the time to reply.

fsenart commented 7 years ago

You're welcome :wink: and do not hesitate if you encounter any other problem.