Skip to content

Commit

Permalink
unify lambda apps
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Dec 13, 2023
1 parent 8fe7d79 commit 4a945d7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 47 deletions.
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ AWS_REGION=us-east-1
SOURCES=$(shell find src/ lib/ -name '*.hs') zureg.cabal

.PHONY: build
build: build/zureg-janitor-lambda.zip
build: build/zureg-lambda.zip

# We need docker to build binaries that run on amazon's linux version, which is
# why this command is a bit more complicated than just `stack install`.
build/zureg-%-lambda/bootstrap: build/image.txt $(SOURCES)
mkdir -p build/zureg-$*-lambda
build/zureg-lambda/bootstrap: build/image.txt $(SOURCES)
mkdir -p build/zureg-lambda
docker run \
-m 4GB \
--user $(shell id -u):$(shell id -g) \
--mount type=bind,source=$(shell pwd)/build/zureg-$*-lambda,target=/dist \
--mount type=bind,source=$(shell pwd)/build/zureg-lambda,target=/dist \
--rm \
$(shell cat build/image.txt) \
cp -r /zureg/bin/zureg-$*-lambda /dist/bootstrap
cp -r /zureg/bin/zureg-lambda /dist/bootstrap

touch $@

# Put all code and dependencies in a zip file we can run on AWS Lambda.
build/zureg-%-lambda.zip: build/zureg-%-lambda/bootstrap
zip $@ -j build/zureg-$*-lambda/*
build/zureg-lambda.zip: build/zureg-lambda/bootstrap
zip $@ -j build/zureg-lambda/*
ls -lh $@

# This is a text file with the name of the docker image. We do things this way
Expand All @@ -47,20 +47,20 @@ build/bucket.txt:
# A text file with the name of the zip file with the lambda's code. Similarly
# to `deploy/bucket.txt` above, we just put the zipfile with a random name and
# then write that to the the file.
build/zureg-%-lambda.txt: build/zureg-%-lambda.zip build/bucket.txt
build/zureg-lambda.txt: build/zureg-lambda.zip build/bucket.txt
mkdir -p build
$(eval ZIP := $(shell od -vAn -N4 -tx4 </dev/random | tr -d ' ').zip)
aws s3api put-object \
--profile $(AWS_PROFILE) \
--region $(AWS_REGION) \
--bucket $(shell cat build/bucket.txt) \
--key $(ZIP) \
--body build/zureg-$*-lambda.zip
--body build/zureg-lambda.zip
echo $(ZIP) >$@

# Deploy (create or update) the cloudformation stack.
.PHONY: deploy
deploy: build/zureg-janitor-lambda.txt build/bucket.txt
deploy: build/zureg-lambda.txt build/bucket.txt
aws cloudformation deploy \
--profile $(AWS_PROFILE) \
--region $(AWS_REGION) \
Expand All @@ -69,7 +69,7 @@ deploy: build/zureg-janitor-lambda.txt build/bucket.txt
--capabilities CAPABILITY_IAM \
--parameter-overrides \
LambdaBucket=$(shell cat build/bucket.txt) \
JanitorLambdaKey=$(shell cat build/zureg-janitor-lambda.txt) \
LambdaKey=$(shell cat build/zureg-lambda.txt) \
Hackathon=$(shell jq -r '.ZUREG_HACKATHON' deploy/env.json) \
Email=$(shell jq -r '.ZUREG_EMAIL' deploy/env.json) \
ScannerSecret=$(shell jq -r '.ZUREG_SCANNER_SECRET' deploy/env.json) \
Expand Down
25 changes: 10 additions & 15 deletions deploy/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Parameters:
LambdaBucket:
Type: 'String'
Description: 'S3 Bucket that the code of the lambdas are stored in.'
JanitorLambdaKey:
LambdaKey:
Type: 'String'
Description: 'S3 Key of the janitor lambda.'
Hackathon:
Expand Down Expand Up @@ -87,11 +87,8 @@ Resources:
ReadCapacityUnits: 1
WriteCapacityUnits: 2

# This is the lambda we deploy. It says Python but it's really just a
# simple wrapper around a Haskell binary.
#
# TODO(jaspervdj): This should be renamed to 'WebLambda'.
ZuregLambda:
# Lambda serving web requests.
WebLambda:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: 'zureg-web'
Expand All @@ -102,7 +99,7 @@ Resources:
MemorySize: 512 # If not, requests to DDB time out...
Code:
S3Bucket: {'Ref': 'LambdaBucket'}
S3Key: {'Ref': 'JanitorLambdaKey'}
S3Key: {'Ref': 'LambdaKey'}
Environment:
Variables:
ZUREG_HACKATHON: {'Ref': 'Hackathon'}
Expand Down Expand Up @@ -149,14 +146,12 @@ Resources:
Resource: {'Fn::Sub': 'arn:aws:ses:${AWS::Region}:${AWS::AccountId}:identity/${Email}'}

# Allow ApiGateway to invoke the function.
#
# TODO(jaspervdj): This should be renamed to 'WebLambdaPermission'.
LambdaPermission:
WebInvokePermission:
Type: 'AWS::Lambda::Permission'
DependsOn: ['ZuregLambda', 'Api']
DependsOn: ['WebLambda', 'Api']
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: {'Fn::GetAtt': ['ZuregLambda', 'Arn']}
FunctionName: {'Fn::GetAtt': ['WebLambda', 'Arn']}
Principal: 'apigateway.amazonaws.com'
SourceArn: {'Fn::Sub': 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${Api}/*'}

Expand All @@ -182,9 +177,9 @@ Resources:
Integration:
Type: 'AWS_PROXY'
IntegrationHttpMethod: 'POST'
Uri: {'Fn::Sub': 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ZuregLambda.Arn}/invocations'}
Uri: {'Fn::Sub': 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${WebLambda.Arn}/invocations'}

MyDeployment:
Deployment:
Type: 'AWS::ApiGateway::Deployment'
DependsOn: ['ProxyMethod']
Properties:
Expand All @@ -202,7 +197,7 @@ Resources:
MemorySize: 512 # If not, requests to DDB time out...
Code:
S3Bucket: {'Ref': 'LambdaBucket'}
S3Key: {'Ref': 'JanitorLambdaKey'}
S3Key: {'Ref': 'LambdaKey'}
Environment:
Variables:
ZUREG_HACKATHON: {'Ref': 'Hackathon'}
Expand Down
11 changes: 7 additions & 4 deletions lib/Zureg/Main/Janitor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Zureg.Main.Janitor
( main
, app
) where

import Control.Monad (guard)
Expand Down Expand Up @@ -33,10 +34,12 @@ isAttending Confirmed = True
isAttending Registered = True
isAttending _ = False

main
:: forall a. (Eq a, A.FromJSON a, A.ToJSON a)
=> Hackathon a -> IO Database.RegistrantsSummary
main hackathon =
main :: (Eq a, A.FromJSON a, A.ToJSON a) => Hackathon a -> IO ()
main hackathon = app hackathon A.Null >>= print

app :: forall a. (Eq a, A.FromJSON a, A.ToJSON a)
=> Hackathon a -> A.Value -> IO Database.RegistrantsSummary
app hackathon _event =
Database.withHandle (Hackathon.databaseConfig hackathon) $ \db -> do
uuids <- Database.getRegistrantUuids db
registrants <- mapM (Database.getRegistrant db) uuids :: IO [Registrant a]
Expand Down
15 changes: 1 addition & 14 deletions src/Janitor.hs
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
{-# LANGUAGE ScopedTypeVariables #-}
import AWS.Lambda.Runtime (mRuntime)
import qualified Data.Aeson as A
import qualified Network.Wai.Handler.Hal as WaiHandler
import System.Environment (lookupEnv)
import qualified Zureg.Hackathon
import qualified Zureg.Main.Janitor
import qualified Zureg.Main.Web as Web

main :: IO ()
main = do
handler <- lookupEnv "_HANDLER"
case handler of
Just "janitor" -> mRuntime $ \(_ :: A.Value) ->
Zureg.Hackathon.withHackathonFromEnv Zureg.Main.Janitor.main
_ -> do
app <- Zureg.Hackathon.withHackathonFromEnv Web.app
mRuntime $ WaiHandler.run app
main = Zureg.Hackathon.withHackathonFromEnv Zureg.Main.Janitor.main
17 changes: 17 additions & 0 deletions src/Lambda.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import AWS.Lambda.Runtime (mRuntime)
import qualified Network.Wai.Handler.Hal as WaiHandler
import System.Environment (lookupEnv)
import qualified Zureg.Hackathon
import qualified Zureg.Main.Janitor as Janitor
import qualified Zureg.Main.Web as Web

main :: IO ()
main = do
handler <- lookupEnv "_HANDLER"
case handler of
Just "janitor" ->
Zureg.Hackathon.withHackathonFromEnv $ mRuntime . Janitor.app
Just "web" -> do
app <- Zureg.Hackathon.withHackathonFromEnv Web.app
mRuntime $ WaiHandler.run app
_ -> fail "_HANDLER not set"
9 changes: 6 additions & 3 deletions zureg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ Executable zureg-badges
Import: exe
Main-is: Badges.hs

Executable zureg-janitor-lambda
Executable zureg-janitor
Import: exe
Main-is: Janitor.hs

Executable zureg-lambda
Import: exe
Main-is: Janitor.hs
Main-is: Lambda.hs
Build-depends:
aeson >= 1.3 && < 1.6,
hal >= 1.0 && < 1.1,
wai-handler-hal >= 0.2 && < 0.3

0 comments on commit 4a945d7

Please sign in to comment.