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.

gefragt vor 2 Jahren6667 Aufrufe
2 Antworten
4
Akzeptierte Antwort

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
beantwortet vor 2 Jahren
profile picture
EXPERTE
überprüft vor 10 Monaten
  • 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
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen