Skip to content

Commit

Permalink
feat: dummy instance of notifier lambda function
Browse files Browse the repository at this point in the history
  • Loading branch information
Cupprum committed Jun 16, 2024
1 parent 002f283 commit dd92306
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/dagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ jobs:
workdir: cicd/dagger
args: >-
run
--logic="../../logic/seatchecker"
--seatchecker="../../logic/seatchecker"
--notifier="../../logic/notifier"
--infra="../../infra"
--access_key="env:AWS_ACCESS_KEY_ID"
--secret_key="env:AWS_SECRET_ACCESS_KEY"
Expand Down
38 changes: 28 additions & 10 deletions cicd/dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Cicd struct{}

func Logic(src *Directory) *Directory {
func PackageGoLambda(src *Directory, module string, test bool) *Directory {
base := dag.Container().From("golang:latest")
// Install packages required to package Lambda.
base = base.
Expand All @@ -24,29 +24,47 @@ func Logic(src *Directory) *Directory {
WithEnvVariable("GOARCH", "arm64")
// Add cache for Go.
base = base.
WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod-logic")).
WithMountedCache("/go/pkg/mod", dag.CacheVolume(fmt.Sprintf("go-mod-%s", module))).
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", dag.CacheVolume("go-build-logic")).
WithMountedCache("/go/build-cache", dag.CacheVolume(fmt.Sprintf("go-build-%s", module))).
WithEnvVariable("GOCACHE", "/go/build-cache")
base = base.WithExec([]string{"go", "install"})

base.WithExec([]string{"go", "test"})
// TODO: do i need a feature flag here?
if test {
base.WithExec([]string{"go", "test"})
}

build := base.WithExec([]string{"go", "build",
"-tags", "lambda.norpc", // Do not include RPC part of library.
"-o", "seatchecker",
"-o", module,
"-ldflags", "-w", // Reduce size of output binary.
"seatchecker.go"})
fmt.Sprintf("%s.go", module)})

out := build.
WithExec([]string{"mkdir", "/out"}).
WithExec([]string{"mv", "seatchecker", "/out/bootstrap"}). // Lambda requires it to be called bootstrap.
WithExec([]string{"mv", module, "/out/bootstrap"}). // Lambda requires it to be called bootstrap.
WithWorkdir("/out").
WithExec([]string{"zip", "seatchecker.zip", "bootstrap"})
WithExec([]string{"zip", fmt.Sprintf("%s.zip", module), "bootstrap"})

return out.Directory("/out")
}

func Logic(seatchecker *Directory, notifier *Directory) *Directory {
sc := PackageGoLambda(seatchecker, "seatchecker", true)
nt := PackageGoLambda(notifier, "notifier", false)

combined := dag.Container().From("golang:latest").
WithDirectory("/in/seatchecker", sc).
WithDirectory("/in/notifier", nt).
WithExec([]string{"mkdir", "/out"}).
WithExec([]string{"cp", "-R", "/in/seatchecker/*", "/out"}).
WithExec([]string{"cp", "-R", "/in/notifier/*", "/out"}).
Directory("/out")

return combined
}

func Infra(out *Directory, infra *Directory,
ak *Secret, sk *Secret,
) *Container {
Expand Down Expand Up @@ -77,10 +95,10 @@ func Infra(out *Directory, infra *Directory,
}

func (m *Cicd) Run(ctx context.Context,
logic *Directory, infra *Directory,
seatchecker *Directory, notifier *Directory, infra *Directory,
access_key *Secret, secret_key *Secret,
) (string, error) {
out := Logic(logic)
out := Logic(seatchecker, notifier)
i := Infra(out, infra, access_key, secret_key)

return i.Stdout(ctx)
Expand Down
3 changes: 2 additions & 1 deletion cicd/dagger/run.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash

dagger call run \
--logic="../../logic/seatchecker" \
--seatchecker="../../logic/seatchecker" \
--notifier="../../logic/notifier" \
--infra="../../infra" \
--access_key="env:GREENMO_AWS_ACCESS_KEY_ID" \
--secret_key="env:GREENMO_AWS_SECRET_ACCESS_KEY"
Expand Down
13 changes: 13 additions & 0 deletions infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ resource "aws_lambda_function" "test_lambda" {
}
}
}


resource "aws_lambda_function" "test_lambda" {
# If the file is not in the current working directory you will need to include a
# path.module in the filename.
filename = "/out/notifier.zip"
function_name = "notifier"
role = aws_iam_role.iam_for_lambda.arn
handler = "bootstrap"
architectures = [ "arm64" ]

runtime = "provided.al2023"
}
12 changes: 12 additions & 0 deletions logic/notifier/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# build binary
GOOS=linux GOARCH=arm64 go build -tags lambda.norpc -o bootstrap notifier.go

# package binary
zip notifier.zip bootstrap

# cleanup binary
rm bootstrap

mv notifier.zip ../../out/notifier.zip
5 changes: 5 additions & 0 deletions logic/notifier/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module example.com/m/v2

go 1.22.4

require github.com/aws/aws-lambda-go v1.47.0 // indirect
2 changes: 2 additions & 0 deletions logic/notifier/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
53 changes: 53 additions & 0 deletions logic/notifier/notifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"log"

"github.com/aws/aws-lambda-go/lambda"
)

// Event defines your lambda input and output data structure,
// and of course you can have different input and output data structure
type InEvent struct {
Sk string `json:"sampleKey1"`
K string `json:"key3"`
}

type OutEvent struct {
Status int `json:"status"`
}

func handler(request InEvent) (OutEvent, error) {
log.Println("Program started.")

log.Println(request)

// email := os.Getenv("SEATCHECKER_EMAIL")
// if email == "" {
// fmt.Fprintf(os.Stderr, "env var 'SEATCHECKER_EMAIL' is not configured")
// os.Exit(1)
// }

// log.Println("Configuration successful.")

// catchErr := func(err error) {
// if err != nil {
// fmt.Fprintf(os.Stderr, "error: %v\n", err)
// os.Exit(1)
// }
// }

// log.Printf("Start account login for user: %s.\n", email)
// cAuth, err := client.accountLogin(email, password)
// catchErr(err) // TODO: probably i will have to rethink exceptions -> Why?
// log.Println("Account login finished successfully.")

log.Println("Program finished successfully.")
return OutEvent{Status: 200}, nil
}

func main() {
lambda.Start(handler)
// resp := handler(InEvent{Sk: "test1", K: "test2"})
// log.Println(resp)
}

0 comments on commit dd92306

Please sign in to comment.