imageproxy icon indicating copy to clipboard operation
imageproxy copied to clipboard

[Feature Request] AWS Lambda deployment?

Open nexus-uw opened this issue 5 years ago • 5 comments
trafficstars

Is it possible to run imageproxy in an AWS Lambda (or other serverless function)?

nexus-uw avatar Nov 25 '19 05:11 nexus-uw

I don't see why not, though I've never actually tried it myself. Inside your function, you'd want to create an imageproxy.Proxy object, configured to your liking, and then call its ServeHTTP method.

Well, at least that would work pretty simply for Google Cloud Functions, since your function is simply passed the http request and response. I've never used Lambda, but it looks like it's passed an event rather than http request and response? A quick Google search found this post about porting an http.Handler to use Lambda's API.

willnorris avatar Nov 25 '19 18:11 willnorris

For what it's worth, I use imageproxy with Google Cloud Run, which I guess still fits into the "serverless" style of services, but it's a level up from function-based, since it allows you define the entire docker image to be run, which works really well for imageproxy. Though it is auto-scaling (down to zero) similar to function-based services like Lambda.

willnorris avatar Nov 25 '19 18:11 willnorris

Okay, turns out it is incredibly simple on Google Cloud Functions. I just used the inline editor with the following:

function.go

package p

import (
	"net/http"

	"willnorris.com/go/imageproxy"
)

func ImageProxy(w http.ResponseWriter, r *http.Request) {
	p := imageproxy.NewProxy(nil, nil)
	p.ServeHTTP(w, r)
}

go.mod

module cloudfunction

require willnorris.com/go/imageproxy v0.9.0

And that's live at https://us-central1-wjn730.cloudfunctions.net/imageproxy/200x/https:/willnorris.com/2013/12/small-things.jpg (I'll probably take this down later).

And of course, you'd want to configure the imageproxy.Proxy instance to do whatever caching you want, limit origin hosts, require signatures, etc.

willnorris avatar Nov 25 '19 18:11 willnorris

Amazing how simple it is to get the first image through the proxy - very nice!

But now I try to connect GCS to it an the compiler complains about the import from files/function.go:8:2: use of internal package willnorris.com/go/imageproxy/internal/gcscache not allowed. Am I missing a non-internal gcscache module or would the cache need to be moved out of the internal scope?

elmarburke avatar Dec 15 '19 13:12 elmarburke

hah, no you're not missing anything. I hadn't really expected those packages to be used outside the context of the main package, but this is a really good example of where that's needed.

I'm not sure I want to make these part of the exported API for imageproxy, so I'll probably move those packages out to a separate repo. In the meantime, you could just copy the gcscache package and include it in your function.

I'll follow-up once I've moved it out to a separate repo.

willnorris avatar Dec 15 '19 18:12 willnorris