Auto updating AWS Lambda function from S3 bucket

Gautham Sreenivasan
2 min readApr 1, 2022

Working on AWS have you ever been on a situation were you really required your Lambda function code to get updated automatically without any manual intervention or infrastructure re-provision?

Well here is a solution that worked well for me.

First of all we have to keep in mind that, once a code is deployed to Lambda function, it creates a cache and the deployed code is being executed from this cached memory. Even if you are deploying the code from S3 bucket and if there is update to the deployment files in the S3 bucket, Lambda function fails to fetch the updated files, it used the initial cached code.

As a solution I have created a new function, lets say “Updater” function which is invoked on event trigger when there is an update to the S3 bucket and the python code inside the Updater function reads the bucket name and key name from the event trigger. The Updater function then invokes the corresponding Lambda function and updates the code.

The key point to note is that the S3 bucket event trigger contains the bucket name and the keyfile name, so the corresponding lambda function should match with the keyfile name.
For example: if the Keyfile is “demoFunction.zip”, the corresponding Lambda function name should be “demoFunction

Architecture

Python code for the Updater Lambda function

import boto3
import json

client = boto3.client("s3")
lambda_client = boto3.client("lambda")

def lambda_handler(event, context):

bucket = event["Records"][0]["s3"]["bucket"]["name"]
file = event["Records"][0]["s3"]["object"]["key"]

get_version = client.get_object(
Bucket = bucket,
Key = file
)

versionId = (get_version["VersionId"]) #Getting the latest version of the code

update_lambda = lambda_client.update_function_code(
FunctionName= file.split("/")[-1].split(".")[0],
S3Bucket=bucket,
S3Key=file,
S3ObjectVersion= versionId
)

Note: its important to create the lambda permission resource for S3 bucket to invoke the Lambda function.

resource "aws_lambda_permission" "lambda-perm-updater" {
statement_id = "AllowS3Invoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.updater_lambda.function_name
principal = "s3.amazonaws.com"
source_arn = "arn:aws:s3:::${aws_s3_bucket.<bucket_name>.id}"
}

Takeaway

Hope you enjoyed my tutorial. You can access this project from my github profile here.

Huge thanks to Jino .

--

--