Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rberger/6d4c5db1ecb5ae452e1b5412a14c7e6d to your computer and use it in GitHub Desktop.
Save rberger/6d4c5db1ecb5ae452e1b5412a14c7e6d to your computer and use it in GitHub Desktop.
Building and deploying Clojurescript aws lambda with shadow-cljs to fit in lambda@edge

Some excerpts of how we build and deploy two lambdas. In this case these will end up as lambda@edge for a cloudfront distribution for handling viewer-requests and origin-requests.

The main point is how the origin-request lambda in the deploy-lambda section includes the node_modules directory into the lambda zip image. Lambda@edge can't have the image be bigger than 50MB. So we do

yarn install --production

To install only the production parts of the dependencies, Then use modclean to reduce the size of node_modules further.

Had problems doing the direct file upload for aws lambda update-function-code so used the s3 upload option for the origin-request lambda.

ORIGIN_FUNCTION=socialiteOrigin
VIEWER_FUNCTION=socialiteViewer
build:
yarn install --production
npx modclean -r -n default:safe,default:caution
npx shadow-cljs release :lambda-viewer
npx shadow-cljs release :lambda-origin
deploy-lambda:
# Viewer-request lambda
cd dist/lambda-viewer && zip lambda-viewer.zip index.js && \
aws lambda update-function-code --no-cli-pager --publish \
--function-name $(VIEWER_FUNCTION) --zip-file fileb://lambda-viewer.zip
# origin-request lambda
cd dist/lambda-origin && \
ln -f -s ../../node_modules . && \
rm -f lambda-origin.zip && \
pwd && \
zip -q -r lambda-origin.zip index.js node_modules && \
aws s3 cp lambda-origin.zip s3://some-s3-bucket && \
aws lambda update-function-code --no-cli-pager --publish \
--function-name $(ORIGIN_FUNCTION) \
--s3-bucket some-s3-bucket --s3-key lambda-origin.zip
{:deps true
:builds {:lambda-viewer {:target :node-library
:output-to "./dist/lambda-viewer/index.js"
:exports {:handler visx.socialite.viewer/handler}
:compiler-options {:infer-externs :auto}}
:lambda-origin {:target :node-library
:output-to "./dist/lambda-origin/index.js"
:exports {:handler visx.socialite.origin/handler}
:compiler-options {:infer-externs :auto}}
:node {:target :node-script
:output-to "./dist/node/index.js"
:main visx.socialite.main/start
:devtools {:after-load visx.socialite.main/reload}}
:test {:target :node-test
:output-to "out/node-tests.js"
:ns-regexp "-test$"
:autorun true}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment