#!/bin/bash -eu # name [arch] r=python3.11 runtime= # runtime l=() layers=() # layers h= handler=lambda_function.lambda_handler # handler . opts name="$1" arch="${2:-arm64}" # Long names for short options runtime="${runtime:-$r}" layers=( "${layers[@]:-"${l[@]}"}" ) handler="${handler:-$h}" # Create a zip file including everything except top-level hidden files. cd "$name" zip="../$name.zip" rm -f "$zip" zip -r "$zip" * # Create and use a new role for the new function: role_name="service-role/$name" aws iam create-role --role-name "$role_name" --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": {"Service": "lambda.amazonaws.com"},"Action": "sts:AssumeRole"}]}' aws iam attach-role-policy --role-name "$role_name" --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole aws iam wait role-exists --role-name "$role_name" # Prepare the list of layers to attach to the function layers_list=() for layer in ${layers[@]}; do layer="arn:aws:lambda:$AWS_REGION:$AWS_ACCOUNT_ID:layer:$layer" layers_list+=("$layer") done layers_option=() if [ "${#layers_list[@]}" -gt 0 ]; then layers_option=(--layers "${layers_list[@]}") fi # Create the function aws lambda create-function \ --function-name "$name" \ --runtime "$runtime" \ --role "arn:aws:iam::$AWS_ACCOUNT_ID:role/$role_name" \ --handler "$handler" \ --zip-file "fileb://$zip" \ --architectures "$arch" \ "${layers_option[@]}" # Notes: # - Undefined env-vars are handled by the -u option to bash, this is sufficient. # - In general I don't want to clutter the code with additional error handling # such as checking if the aws tool is installed. # - I don't want to validate parameters either; if they exist that's good enough.