script to update python runtime from 3.6 to 3.9 in lambda functions

1

i have multiple lambda functions. is there any script to update python runtime from 3.6 to 3.9 in lambda function automatically. thanks.

asked 2 years ago6452 views
2 Answers
4
Accepted Answer

you need to change the region in below code , it will automatically update python runtime 3.6 to 3.9 in all lambda functions.

#!/bin/bash

# listing all functions in csv format using Regular expressions
lambda_fun=$(
  aws lambda list-functions \
  --function-version ALL \
  --region=us-east-2 \
  --output text \
  --query "Functions[?Runtime=='python3.6'].FunctionName" | sed -E 's/	+/\\n/g')

# adding header to csv and redirecting to file [file.csv]
echo -e "lambda_functions\n$lambda_fun" > file.csv

# updating lambda function by iterating using while loop
i=0
# input file separate
OLDIFS=$IFS
IFS=","
# while loop
while read  lambda_functions
do

  ((i=i+1))

  # skipping header
  if [  "$i" -eq 1  ]; then
      continue
  fi

  runtime_update=$(
    aws lambda update-function-configuration \
    --function-name "$lambda_functions" \
    --runtime python3.9 --output text --query "Runtime"
  )
  echo "$lambda_functions" is successfully updated to "$runtime_update"

done < file.csv
IFS=$OLDIFS
profile picture
answered 2 years ago
profile picture
EXPERT
reviewed 9 months ago
  • be aware that aws lambda list-functions will not return all lambda since it uses pagination... (you can add the switch --no-paginate to prevent this)

1

You can do something like this. It will update all function in one region.

Attention though:

  • this will ruin any Infrastructure as Code that you have. If you have IaC you should update any code there in stead of running a script
  • there are differences between python 3.6 and 3.9, your lambdas might stop working after the update so testing out all code paths is necessary
import boto3
import json

client = boto3.client('lambda')

functions = []
response = client.list_functions()
nexttoken = response.get('NextMarker', None)
functions.extend(response['Functions'])
while(nexttoken):
    response = client.list_functions(Marker=nexttoken)
    nexttoken = response.get('NextMarker', None)
    functions.extend(response['Functions'])

for function in functions:
    if function['Runtime'] == 'python3.6':
        client.update_function_configuration(
            FunctionName=function['FunctionName'],
            Runtime='python3.9'
        )
profile picture
JaccoPK
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions