aws lambda automated code deployment

AWS Lambda Automated Code Deployment

When working with Lambda functions on Amazon Web Services, it quickly becomes necessary to upload the (Java) code to S3 buckets as the dependencies inevitably cause the code package to exceed 10 MiB. This approach brings with it the ability to automate the code deployment, as we can use AWS triggers to detect an upload, and it’s even possible to automate the uploading of the code from the CI/CD pipeline. The latter is beyond the scope of this document, but it’s beneficial to be aware of it.

This approach is probably not suited for production deployments – Amazon offers other services such as CodeDeploy that are better suited for this. When working with rapidly iterative approaches such as Proofs of Concept, it is often a must to relieve the developer of actions such as uploading the new code to S3 and editing the Lambda function trigger a deployment of the code.

The approach can be summarized in the following way. If one or more Lambda functions perform some tasks and share some common code in the form of an application that is uploaded to an AWS S3 bucket. It is possible to write an additional lambda function that is called whenever a new version of the code is uploaded, which updates the Lambda functions that use this code. The function updates it with the new code, automating this time-consuming and repetitive process which in turn allows for faster iterations and testing of the code.


Assuming that there already exist some lambdas that use code that is deployed to AWS S3, the user can take the following steps to implement a lambda to update the code:

  • Create a new lambda function, using NodeJS and a role that gives it basic lambda execution rights


  • Once the lambda is created, update the inline code as follows:
const AWS = require('aws-sdk');

exports.handler = async function (event, context) {
    const key = event.Records[0].s3.object.key;
    const bucket = event.Records[0];
    const version = event.Records[0].s3.object.versionId;

    console.log("Processing zip " + key + " in bucket " + bucket + " with version " + version);

    const functionNames = [

    const failures = updateLambdaFunctions(functionNames, key, bucket);

    for (var index in failures) {
        console.log("Failed to update code for lambda function: " + failures[index]);

    context.succeed("Updated code for " + (functionNames.length - failures.length) + " lambda functions");

function updateLambda(lambda, params) {
    let result = true;

    lambda.updateFunctionCode(params, function (err, data) {
        if (err) {
            console.log("Error uploading code to lambda function: " + params.FunctionName);
            console.log(err, err.stack);
            result = false;
        } else {
            console.log("Uploaded code to lambda function: " + params.FunctionName);

    return result;

function updateLambdaFunctions(functionNames, key, bucket) {
    const lambda = new AWS.Lambda();
    const failures = [];

    for (var index in functionNames) {
        const functionName = functionNames[index];
        console.log("Uploading code to lambda function: " + functionName);

        const params = {
            FunctionName: functionName,
            S3Key: key,
            S3Bucket: bucket

        if (!updateLambda(lambda, params)) {
            failures += functionName;

    return failures;
  • Add S3 trigger to the lambda:


  • Configure the trigger to watch for the upload event for the S3 bucket and filename that you will be uploading your code to:


  • Add the trigger, and then save the lambda function.
  • Create a test for the lambda:


  • Configure the test event as an Amazon S3 Put


  • Replace the bucket name and the object key to correspond to the bucket and file names you are using:


  • Run the test. Assuming you have already uploaded the code ZIP to S3, you should see the following output:



Now that the lambda is configured, it will be called every time that a new version of the code is uploaded to the S3 bucket. This, in turn, uploads the code to the lambda function(s) specified in the NodeJS code.

Like this article and want to stay updated of more news and events?
Then sign up for our newsletter!

Don't miss out!

Subscribe to our newsletter and stay up to date with our latest articles and events!

Subscribe now

Newsletter Subscription