Lambda Layers

Script to create boto3 lambda layer with latest version of boto3 and push to AWS.

See here for a Lambda based Lambda Layer generator.

How to use a Lambda Layer.

#!/bin/bash
printf "Build script for AWS boto3 Lambda Layer. \n"
printf "=======================================. \n"
 
printf "Updating system. \n"
sudo yum update -y
 
printf "\n"
printf "Update python3 pip. \n"
python3 -m pip install --upgrade pip
 
printf "\n"
printf "aws cli version is:-  "
aws --version
 
printf "\n"
LATESTRELEASE=`curl -L -s "https://pypi.org/pypi/boto3/json" | jq  -r '.releases | keys | .[]' | sort -Vr | head -n 1`
printf "Latest boto3 release:- %s \n" ${LATESTRELEASE}
 
export LIB_DIR=boto3-${LATESTRELEASE}
printf "\n"
printf "Creating directory and installing boto3 to ${LIB_DIR}. \n"
 
# remove any existing directories if they match the current release.
if [ -d "$LIB_DIR" ];  then rm -rf ${LIB_DIR}; fi
mkdir -p ${LIB_DIR}
 
printf "\n"
pip3 install boto3 -t ${LIB_DIR}
 
printf "\n"
printf "Directory structure:- \n"
tree -L 1 ${LIB_DIR}
 
printf "\n"
printf "Zip up the package and check the destination directory:- \n"
cd ${LIB_DIR}
 
zip -r /tmp/${LIB_DIR}.zip .  2>&1 > /dev/null
 
ls -l /tmp/boto*
 
printf "Publishing layer to AWS. \n\n"
 
printf "These credentials are not stored anywhere. \n"
read -p "AWS region [default is:- eu-central-1]:-" AWS_REGION
AWS_REGION=${AWS_REGION:-eu-central-1}
 
read -p "AWS Access Key:- "  AWS_ACCESS_KEY_ID
read -p "AWS Secret Access key:- " AWS_SECRET_ACCESS_KEY
read -p "AWS Session Token:-" AWS_SESSION_TOKEN
 
export AWS_DEFAULT_REGION=${AWS_REGION}
export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
export AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}
 
 
printf "\n"
printf "Check access to AWS:- \n"
aws sts get-caller-identity
 
# (arn:[a-zA-Z0-9-]+:lambda:[a-zA-Z0-9-]+:\d{12}:layer:[a-zA-Z0-9-_]+)|[a-zA-Z0-9-_]+
# Need to replace '.' with '_' to satisfy AWS requirements.
 
# backslash escape '.' to treat as literal not regex character
LATESTRELEASE=${LATESTRELEASE//\./_}
 
 
printf "\n"
printf "Push layer to AWS:- \n"
aws lambda publish-layer-version --layer-name boto3-${LATESTRELEASE} \
        --compatible-runtimes python3.6 python3.7 python3.8 python3.9 \
        --description "Lambda Layer for S3 Additional Checksum." \
        --zip-file fileb:///tmp/${LIB_DIR}.zip
 
exit 0
 
aws/lambda_layers.txt · Last modified: 09/03/2023 11:52 by andrew