Skip to content

Instantly share code, notes, and snippets.

@AnalyzePlatypus
Last active May 12, 2021 03:21
Show Gist options
  • Save AnalyzePlatypus/2916f8427124da1e0b1dea065a2d4625 to your computer and use it in GitHub Desktop.
Save AnalyzePlatypus/2916f8427124da1e0b1dea065a2d4625 to your computer and use it in GitHub Desktop.
Deploy a Node.js Lambda function using the AWS CLI

.env file

LAMBDA_FUNCTION_NAME=MyFunction
AWS_UPLOAD_CODE_PROFILE=MY_CLI_PROFILE
SLACK_DEV_NOTIFICATIONS_WEBHOOK_URL=https://hooks.slack.com/services/****/****/****

deploy.sh file

# Read the .env file
export $(egrep -v '^#' .env | xargs)


echo $LAMBDA_FUNCTION_NAME # Print function name
rm function.zip # Delete the old ZIP file from the last deploy

# Install all dependencies, in case we've missed something
npm install

# Build the ZIP file
zip -r function.zip \
  app.js \
  parseJotFormPayload.js \
  node_modules \ # This is a directory with all our packages
  package.json

echo "ℹ️ Built ZIP" 
ls -lh function.zip # Print out the stats of the file, file size etc.

# Upload to AWS
echo "🌀 Uploading..."
aws lambda update-function-code \
  --cli-connect-timeout 0 \ # Prevent upload from timing out if you have really slow Wi-Fi (cough, cough)
  --function-name=$LAMBDA_FUNCTION_NAME \
  --zip-file=fileb://function.zip \
  --profile=$AWS_UPLOAD_CODE_PROFILE \
  --region='us-east-1'
  
# Announce the new deploy in a Slack channel
curl -X POST -H 'Content-type: application/json' --data '{"text":"🚀 Lambda "'$LAMBDA_FUNCTION_NAME'" redeployed"}' $SLACK_DEV_NOTIFICATIONS_WEBHOOK_URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment