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.

質問済み 2年前6669ビュー
2回答
4
承認された回答

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
回答済み 2年前
profile picture
エキスパート
レビュー済み 10ヶ月前
  • 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
回答済み 2年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ