Skip to content

Instantly share code, notes, and snippets.

@ruffyleaf
Last active October 13, 2017 08:52
Show Gist options
  • Save ruffyleaf/67e3e305cb012cbd8d5b8fc54f445b3f to your computer and use it in GitHub Desktop.
Save ruffyleaf/67e3e305cb012cbd8d5b8fc54f445b3f to your computer and use it in GitHub Desktop.
AWS Lambda Makefile for Scheduled Jobs
PROJECT = myProject
FUNCTION = $(PROJECT)
REGION = ap-southeast-1
ROLE = arn:aws:iam::123456789012:role/service-role/myLambdaRole
createFunction:
aws lambda create-function \
--region $(REGION) \
--function-name $(PROJECT) \
--zip-file fileb://$(FUNCTION).zip \
--role $(ROLE) \
--environment Variables="{HOST='0.0.0.0',PORT=22,USERNAME='user',PASSWORD='password',REMOTEFILE='to/remote/file'}" \
--handler $(PROJECT).handler \
--runtime python2.7 \
--profile <adminuser> \
--timeout 10 \
--memory-size 1024
packageDependencies:
zip -r9 $(FUNCTION).zip $(PROJECT).py
mkdir -p site-packages
virtualenv $(FUNCTION)
. $(FUNCTION)/bin/activate; pip install -r requirements.txt;\
cd site-packages; cp -r $$VIRTUAL_ENV/lib/python2.7/site-packages/ ./;\
cp -r $$VIRTUAL_ENV/lib64/python2.7/site-packages/ ./;\
cd site-packages; zip -r9 ../../$(FUNCTION).zip .
updateFunction:
aws lambda update-function-code \
--region $(REGION) \
--function-name $(PROJECT) \
--zip-file fileb://$(FUNCTION).zip \
--publish\
--profile <adminuser>
manualInvoke:
aws lambda invoke \
--invocation-type RequestResponse \
--function-name $(FUNCTION) \
--region $(REGION) \
--profile <adminuser> \
outputfile.txt
deleteFunction:
aws lambda delete-function \
--function-name $(FUNCTION) \
--region $(REGION) \
--profile <adminuser>
# Schedule the Lambda Function
# Step 1:
createRule:
aws events put-rule \
--region $(REGION) \
--name $(FUNCTION)-rule \
--schedule-expression 'cron(0/5 8-17 ? * MON-FRI *)' \
--profile <adminuser>
# $(ARNRULE) is taken from the put-rule target output and parsed in
# ARNRULE=arn:aws:events:.....
# Step 2:
addPermission:
aws lambda add-permission \
--region $(REGION) \
--function-name $(FUNCTION) \
--statement-id $(FUNCTION)-event \
--action 'lambda:InvokeFunction' \
--principal events.amazonaws.com \
--source-arn $(ARNRULE) \
--profile <adminuser>
putTargets:
aws events put-targets \
--region $(REGION) \
--rule $(FUNCTION)-rule \
--targets file://targets.json \
--profile <adminuser>
clean:
rm -r $(FUNCTION)
rm -r $(FUNCTION).zip
rm -r site-packages
rm -r outputfile.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment