Skip to content

Instantly share code, notes, and snippets.

@quangv
Last active July 3, 2018 15:21
Show Gist options
  • Save quangv/675838e13521d709056382ee049bb683 to your computer and use it in GitHub Desktop.
Save quangv/675838e13521d709056382ee049bb683 to your computer and use it in GitHub Desktop.
AWS Lambda shell scripts
# Environment varialbes goes here
SAY='hello'
# make an .env.local file
AWS_LAMBDA_ROLE='arn of lambda goes here'
Copyright 2018 Quang Van
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#!/bin/bash
function create() {
. .env.local
mkdir tmp
cd $1
zip ../tmp/$1.zip -r *
cd ../tmp
aws lambda create-function --function-name $1 --runtime nodejs8.10 --role $AWS_LAMBDA_ROLE --handler index.handler --zip-file fileb://$1.zip --environment Variables="{$(getEnv)}"
}
function update() {
. .env.local
mkdir tmp
cd $1
zip ../tmp/$1.zip -r *
cd ../tmp
aws lambda update-function-code --function-name $1 --zip-file fileb://$1.zip > /dev/null
aws lambda update-function-configuration --function-name $1 --environment Variables="{$(getEnv)}"
}
# alias update
function upload() {
update $1
}
# alias update
function push() {
update $1
}
function delete() {
aws lambda delete-function --function-name $1
rm -r $1
}
function invoke() {
aws lambda invoke --function-name $1 tmp/result
echo
cat tmp/result
echo
}
function localInvoke() {
set -a # export envars
source .env
set +a
# Handles promises, console.log result
node -e "var result = require('./js/$1').handler(null, {}, console.log); if(result && result.then){result.then(function(r){console.log(r)}, function(e){console.error(e)})} else{result && console.log(result)}"
}
function list() {
aws lambda list-functions | grep FunctionName
}
function copy() {
cp -r $1 $2
create $2
}
function getZipUrl() {
aws lambda get-function --function-name $1 | jq .Code.Location
}
function getEnv() {
# removes comments from .env
grep -o '^[^#]*' $CWD/.env | tr '\n' ','
}
function pull() {
eval wget $(getZipUrl $1) -O tmp/$1.zip
unzip tmp/$1.zip -d $1
}
CWD=$PWD
eval "$@"
# ./run list
# ./run create hello
# ./run update hello
# ./run delete hello
#
# ./run invoke hello
#
# ./run copy hello hello2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment