Skip to content

Commit

Permalink
build for hal runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Dec 12, 2023
1 parent ac3172d commit 9051230
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
33 changes: 15 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@ AWS_REGION=us-east-1
SOURCES=$(shell find src/ lib/ -name '*.hs') zureg.cabal

.PHONY: build
build: build/zureg-lambda.zip
build: build/zureg-janitor-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/bin/zureg-web: build/image.txt $(SOURCES)
mkdir -p build/bin
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),target=/dist \
--mount type=bind,source=$(shell pwd)/build/zureg-$*-lambda,target=/dist \
--rm \
$(shell cat build/image.txt) \
cp -r /zureg/bin /dist/build
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/bin/zureg-web deploy/main.py deploy/env.json
mkdir -p build/zureg-lambda
ln -fs $(PWD)/build/bin/zureg-janitor build/zureg-lambda/zureg-janitor
ln -fs $(PWD)/build/bin/zureg-web build/zureg-lambda/zureg-web
ln -fs $(PWD)/deploy/main.py build/zureg-lambda/main.py
ln -fs $(PWD)/deploy/env.json build/zureg-lambda/env.json
zip $@ -j build/zureg-lambda/*
build/zureg-%-lambda/config.json: deploy/env.json
cp $^ $@
build/zureg-%-lambda.zip: build/zureg-%-lambda/bootstrap build/zureg-%-lambda/config.json
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 @@ -52,29 +49,29 @@ 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/zip.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
echo $(ZIP) >build/zip.txt
--body build/zureg-$*-lambda.zip
echo $(ZIP) >$@

# Deploy (create or update) the cloudformation stack.
.PHONY: deploy
deploy: build/zip.txt build/bucket.txt
deploy: build/zureg-janitor-lambda.txt build/bucket.txt
aws cloudformation deploy \
--profile $(AWS_PROFILE) \
--region $(AWS_REGION) \
--stack-name zureg-stack \
--template-file deploy/template.yaml \
--capabilities CAPABILITY_IAM \
--parameter-overrides \
SourceS3Bucket=$(shell cat build/bucket.txt) \
SourceS3Key=$(shell cat build/zip.txt) \
LambdaBucket=$(shell cat build/bucket.txt) \
JanitorLambdaKey=$(shell cat build/zureg-janitor-lambda.txt) \
EmailAddress=$(shell jq -r '.ZUREG_EMAIL' deploy/env.json)

# Undo the deployment.
Expand Down
18 changes: 9 additions & 9 deletions deploy/template.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
SourceS3Bucket:
LambdaBucket:
Type: 'String'
Description: 'S3 Bucket that the code of the lambdas are stored in.'
SourceS3Key:
JanitorLambdaKey:
Type: 'String'
Description: 'S3 Key of the web lambda.'
Description: 'S3 Key of the janitor lambda.'
EmailAddress:
Type: 'String'
Description: 'Email address that the registration bot sends email from.'
Expand Down Expand Up @@ -83,8 +83,8 @@ Resources:
Timeout: 10
MemorySize: 512 # If not, requests to DDB time out...
Code:
S3Bucket: {'Ref': 'SourceS3Bucket'}
S3Key: {'Ref': 'SourceS3Key'}
S3Bucket: {'Ref': 'LambdaBucket'}
S3Key: {'Ref': 'JanitorLambdaKey'}

# This is the role of the lambda: the permissions it needs. We need access to
# logs, and to the database.
Expand Down Expand Up @@ -168,14 +168,14 @@ Resources:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: 'zureg-janitor'
Handler: 'main.janitor_handler'
Handler: 'NOT_USED'
Role: {'Fn::GetAtt': ['LambdaExecutionRole', 'Arn']}
Runtime: 'python3.9'
Runtime: 'provided'
Timeout: 10
MemorySize: 512 # If not, requests to DDB time out...
Code:
S3Bucket: {'Ref': 'SourceS3Bucket'}
S3Key: {'Ref': 'SourceS3Key'}
S3Bucket: {'Ref': 'LambdaBucket'}
S3Key: {'Ref': 'JanitorLambdaKey'}

JanitorRule:
Type: 'AWS::Events::Rule'
Expand Down
6 changes: 5 additions & 1 deletion src/Janitor.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{-# LANGUAGE ScopedTypeVariables #-}
import AWS.Lambda.Runtime (mRuntime)
import qualified Data.Aeson as A
import qualified Zureg.Hackathon
import qualified Zureg.Main.Janitor

main :: IO ()
main = Zureg.Hackathon.withHackathonFromEnv Zureg.Main.Janitor.main
main = mRuntime $ \(_ :: A.Value) ->
Zureg.Hackathon.withHackathonFromEnv Zureg.Main.Janitor.main
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extra-deps:
- 'digestive-functors-0.8.4.2'
- 'digestive-functors-blaze-0.6.2.0'
- 'eventful-dynamodb-0.2.0'
- 'hal-1.0.0.1'
- 'unliftio-core-0.1.2.0'
- 'urlencoded-0.5.0.0'
nix:
Expand Down
7 changes: 7 additions & 0 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ packages:
sha256: 6e9cf49ee8117536f28933ef2a6027da691bb6ab00f1ab5d786b207398611460
original:
hackage: eventful-dynamodb-0.2.0
- completed:
hackage: hal-1.0.0.1@sha256:0d7706c8c9341aeca7b50dc60d66ebb32f28259d8afc3f555a7dc3a651f08f3d,4761
pantry-tree:
size: 2449
sha256: 39cedd2a95379083b02934b19899da8d598dbdf12114785516d4bf38feda42e4
original:
hackage: hal-1.0.0.1
- completed:
hackage: unliftio-core-0.1.2.0@sha256:b0a7652ffce2284a6cebe05c99eb60573a8fb6631163f34b0b30a80b4a78cb23,1081
pantry-tree:
Expand Down
10 changes: 6 additions & 4 deletions zureg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ Library
Zureg.Main.Email
Zureg.Main.Export
Zureg.Main.Janitor
Zureg.Main.Web
Zureg.Main.PopWaitlist
Zureg.Main.Web
Zureg.Model
Zureg.Model.Csv
Zureg.Captcha
Expand Down Expand Up @@ -94,6 +94,7 @@ Library
eventful-dynamodb >= 0.2 && < 0.3,
file-embed >= 0.0 && < 0.1,
filepath >= 1.4 && < 1.5,
hal >= 1.0 && < 1.1,
http-client >= 0.5 && < 0.7,
http-client-tls >= 0.3 && < 0.4,
lens >= 4.16 && < 4.20,
Expand Down Expand Up @@ -142,6 +143,7 @@ Executable zureg-badges
Import: exe
Main-is: Badges.hs

Executable zureg-janitor
Import: exe
Main-is: Janitor.hs
Executable zureg-janitor-lambda
Import: exe
Main-is: Janitor.hs
Build-depends: aeson, hal

0 comments on commit 9051230

Please sign in to comment.