Skip to content

Instantly share code, notes, and snippets.

@nicc777
Last active December 31, 2023 09:26
Show Gist options
  • Save nicc777/2b7a988a1920eca910ac3243d56bdd80 to your computer and use it in GitHub Desktop.
Save nicc777/2b7a988a1920eca910ac3243d56bdd80 to your computer and use it in GitHub Desktop.
Bash script for quick Python based AWS Lambda Function Packaging

AWS Python Lambda Function Packaging

The script will create a ZIP file according to the AWS Documented process.

The resulting ZIP file can then be uploaded to S3 from where you can create the Lambda function, either by the AWS CLI, the AWS Console or by AWS CloudFormation. Just supply the S3 ZIP file URL and your Lambda function will be created.

Basic Workflow

  1. Create a directory for your Python script (Lambda function) and create the initial script. If you need additional packages, specify then in a requirements.txt file in the same directory as the Python script
  2. Code... coffee... code... coffee... etc. (repeat as required)
  3. When you are done and need a ZIP file, run the packaging script. Each time you run it, a new package directory and ZIP file will be created.

Usually, boto3 will already be included in the Python Lambda environment, so no need to specify it in the requirements.txt file.

#!/usr/bin/env bash
help_message () {
script_name=${0##*/}
echo "Usage: ${script_name} -f STRING -p STRING [-h]"
echo
echo " -f STRING - Path to the Python script to package as AWS Lambda function"
echo " -p STRING - Package name (without any extensions)"
echo " -h - Display help"
echo
exit
}
display_help=0
pyhton_script_path=""
package_name="python_lambda_function"
while getopts hf:p: flag
do
case "${flag}" in
h) display_help=1;;
f) pyhton_script_path=${OPTARG};;
p) package_name=${OPTARG};;
esac
done
if [ "$display_help" -gt "0" ]; then
help_message
fi
if [ -z "$pyhton_script_path" ]
then
echo "ERROR: When supplying the -f parameter, a STRING value must be provided. Use the -h parameter for help."
exit
fi
if ! test -f $pyhton_script_path; then
echo "ERROR: File does not exist."
exit
fi
echo "Starting to package AWS Lamda Function (Python)"
package_dir="/tmp/package_`cat /proc/sys/kernel/random/uuid | sed 's/[-]//g'`"
echo " Creating temporary packaging directory: ${package_dir}"
mkdir -p $package_dir
echo " Copy Python lambda function to package directory"
cp $pyhton_script_path $package_dir
requirements_file="`dirname ${pyhton_script_path}`/requirements.txt"
echo " Checking for the presence of requirements.txt in the same directory as the script: ${requirements_file}"
if test -f $requirements_file; then
echo " Adding requirements..."
cp $requirements_file $package_dir
echo " Creating Python virtual environment"
cd $package_dir
python3 -m venv venv
. venv/bin/activate
echo " Adding required packages"
mkdir package
pip3 install --target ./package -r requirements.txt
cd package
zip -r ../${package_name}.zip .
cd ..
zip ${package_name}.zip `basename ${pyhton_script_path}`
else
cd $package_dir
zip ${package_name}.zip `basename ${pyhton_script_path}`
fi
echo "DONE"
echo
echo " Package Directory : ${package_dir}"
echo " Pakage File : ${package_dir}/${package_name}.zip"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment