5
votes

update-function-code AWS CLI command updates all code files except the handler function file, lambda_function.py

Specifically, I made a bash script that

  1. Downloads code zip from one lambda (source)
  2. Uploads code zip to another lambda (dest)

Everything works, except the main function file lambda_function.py does not get updated.

Oddly, when I download the zip from a lambda, make a change, and then upload to the same lambda, it works (all files are updated).

FYI, here is my bash script to download code from one lambda, and upload to another:

#!/bin/sh

SRC_LAMBDA_FUNCTION_NAME="$1"
DEST_LAMBDA_FUNCTION_NAME="$2"

export PYTHONIOENCODING=utf8

# get lambda function config
LAMBDA_JSON=$(aws lambda get-function --function-name $SRC_LAMBDA_FUNCTION_NAME)
export LAMBDA_JSON

# parse the code zip file download URL (link expires 10 minutes after creation)
ZIP_FILE_URL=$(python -c 'import json,sys,os;obj=json.loads(os.environ["LAMBDA_JSON"]);print(obj["Code"]["Location"])')

# make temp dir
mkdir -p download

# download the code from src lambda
curl -o "download/$SRC_LAMBDA_FUNCTION_NAME.zip" $ZIP_FILE_URL

# upload the code to dest lambda
aws lambda update-function-code --function-name $DEST_LAMBDA_FUNCTION_NAME --zip-file "fileb://download/$SRC_LAMBDA_FUNCTION_NAME.zip"
2
I suspect you may be misdiagnosing the issue... update-function-code replaces the entire zip file with a new one and returns the CodeSha256 of the new package. Partial changes should be impossible.Michael - sqlbot
@Michael-sqlbot Agreed, and I just found the issue, see my answer ..James Wierzba

2 Answers

9
votes

I was verifying the code changes by navigating to the lambda code editor on the AWS web portal, and it appears this was just a client side issue in the web UI.

It took about 5 minutes before the lambda_function.py was updated in the UI (despite refreshing), whereas the other code files did get updated immediately.

It is very strange that other files got updated, but not lambda_function.py. This makes me think it is not just a browser caching issue, but maybe a bug.

0
votes

I believe you may need to publish the new code changes according to https://docs.aws.amazon.com/cli/latest/reference/lambda/update-function-code.html

--publish | --no-publish (boolean)

Set to true to publish a new version of the function after updating the code. This has the same effect as calling PublishVersion separately.