Deploy an AWS Lambda function
Automate your Lambda function deployment with a bash script
AWS Lambda is a flexible and cost effective service which allows you to implement back-end functionality in a serverless environment. In this tutorial we will create a script to automate the deployment of Lambda functions. Apart from an AWS account, we will also need
The Lambda function name (ARN) and region. We can find the ARN by selecting our lambda function in the AWS Lambda service. The ARN is shown in the top right of the page:
The AWS cli to run our script
The script we will use is tailored for a
Node.js Lambda function, but it can be easily adjusted for other runtimes.
The executed steps are
- Parse the required AWS parameters (
function name) from a
- Include the files to be uploaded in a .zip file
- Upload the file to AWS
- Remove the zip file
Let's create a
deploy_lambda.sh file with the following contents:
#!/bin/bashset -e. .envZIP_FILE="lambda.zip"# Specify the files you want to include in your lambda functionFILES_TO_INCLUDE="index.js"# Dependency installation, adjust according to your functionecho "Installing dependencies..."npm iecho "Building zip file..."zip -r $ZIP_FILE $FILES_TO_INCLUDEecho "Uploading lambda function..."aws lambda update-function-code \--function-name $AWS_FUNCTION_NAME \--zip-file "fileb://$ZIP_FILE" \--region "$AWS_REGION"rm $ZIP_FILEecho "Done!"
We will also need a
.env file in the same folder as our script. An example:
⚠️ You may need to change the script's permissions first:
chmod 755 deploy.lambda.sh
Finally, we can include this script in our application's CI/CD pipeline to streamline the deployment of Lambda functions.