-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from metrico/lambda
AWS Lambda
- Loading branch information
Showing
6 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,7 @@ jobs: | |
- name: Compress Fluxpipe | ||
run: | | ||
strip fluxpipe-server | ||
strip fluxpipe-lambda | ||
upx fluxpipe-server | ||
- id: tag_bump | ||
|
@@ -106,6 +107,7 @@ jobs: | |
with: | ||
release_config: | | ||
fluxpipe-server | ||
fluxpipe-lambda | ||
tag_name: ${{ env.VERSION }} | ||
release_name: fluxpipe_${{ env.VERSION }} | ||
draft: false | ||
|
@@ -120,7 +122,7 @@ jobs: | |
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Docker Build and push | ||
- name: Docker Build and push (server) | ||
if: github.event_name != 'pull_request' | ||
uses: docker/[email protected] | ||
with: | ||
|
@@ -129,3 +131,15 @@ jobs: | |
tags: | | ||
ghcr.io/metrico/fluxpipe:latest | ||
ghcr.io/metrico/fluxpipe:${{ env.VERSION }} | ||
- name: Docker Build and push (lambda) | ||
if: github.event_name != 'pull_request' | ||
uses: docker/[email protected] | ||
with: | ||
context: . | ||
file: Dockerfile.lambda | ||
push: true | ||
tags: | | ||
ghcr.io/metrico/fluxpipe-lambda:latest | ||
ghcr.io/metrico/fluxpipe-lambda:${{ env.VERSION }} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM alpine as builder | ||
RUN apk add --allow-untrusted --update --no-cache curl ca-certificates | ||
WORKDIR / | ||
RUN curl -fsSL github.com/metrico/fluxpipe/releases/latest/download/fluxpipe-lambda -O && chmod +x fluxpipe-lambda | ||
|
||
FROM scratch | ||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
COPY --from=builder /fluxpipe-lambda /fluxpipe-lambda | ||
CMD ["/fluxpipe-lambda"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
"bytes" | ||
|
||
"github.com/influxdata/flux" | ||
"github.com/influxdata/flux/csv" | ||
_ "github.com/influxdata/flux/fluxinit/static" | ||
"github.com/influxdata/flux/lang" | ||
"github.com/influxdata/flux/memory" | ||
"github.com/influxdata/flux/runtime" | ||
|
||
_fluxhttp "github.com/influxdata/flux/dependencies/http" | ||
"github.com/influxdata/flux/dependencies/secret" | ||
"github.com/influxdata/flux/dependencies/url" | ||
|
||
"github.com/aws/aws-lambda-go/lambda" | ||
|
||
) | ||
|
||
var APPNAME = "fluxpipe-lambda" | ||
|
||
func runQuery(ctx context.Context, script string) (flux.Query, error) { | ||
|
||
program, err := lang.Compile(ctx, script, runtime.Default, time.Now()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
q, err := program.Start(ctx, memory.DefaultAllocator) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return q, nil | ||
} | ||
|
||
// NewCustomDependencies produces a Custom set of dependencies including EnvironmentSecretService. | ||
func NewCustomDependencies() flux.Deps { | ||
validator := url.PassValidator{} | ||
return flux.Deps{ | ||
Deps: flux.WrappedDeps{ | ||
HTTPClient: _fluxhttp.NewLimitedDefaultClient(validator), | ||
// Default to having no filesystem, no secrets, and no url validation (always pass). | ||
FilesystemService: nil, | ||
SecretService: secret.EnvironmentSecretService{}, | ||
URLValidator: validator, | ||
}, | ||
} | ||
} | ||
|
||
func exec(inputString string) (string, string) { | ||
|
||
// CustomDeps produces a Custom set of dependencies including EnvironmentSecretService. | ||
customValidator := url.PassValidator{} | ||
customDeps := flux.Deps{ | ||
Deps: flux.WrappedDeps{ | ||
HTTPClient: _fluxhttp.NewLimitedDefaultClient(customValidator), | ||
FilesystemService: nil, | ||
SecretService: secret.EnvironmentSecretService{}, | ||
URLValidator: customValidator, | ||
}, | ||
} | ||
|
||
// ctx := flux.NewDefaultDependencies().Inject(context.Background()) | ||
ctx := customDeps.Inject(context.Background()) | ||
|
||
q, err := runQuery(ctx, inputString) | ||
if err != nil { | ||
fmt.Println("unexpected error while creating query: %s", err) | ||
return "", string(fmt.Sprintf(`{"code":"invalid","message":"%v"}`, err)) | ||
} | ||
|
||
results := flux.NewResultIteratorFromQuery(q) | ||
defer results.Release() | ||
|
||
buf := bytes.NewBuffer(nil) | ||
encoder := csv.NewMultiResultEncoder(csv.DefaultEncoderConfig()) | ||
|
||
if _, err := encoder.Encode(buf, results); err != nil { | ||
panic(err) | ||
} | ||
|
||
q.Done() | ||
|
||
if q.Err() != nil { | ||
fmt.Println("unexpected error from query execution: %s", q.Err()) | ||
return "", string(fmt.Sprintf(`{"code":"invalid","message":"%v"}`, q.Err())) | ||
|
||
} else { | ||
return buf.String(), "" | ||
} | ||
} | ||
|
||
type FluxEvent struct { | ||
Query string `json:"flux"` | ||
} | ||
|
||
func HandleRequest(ctx context.Context, flux FluxEvent) (string, error) { | ||
buf, _ := exec(flux.Query) | ||
return buf, nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(HandleRequest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters