Working with Lambda function versions

Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.

There are many scenarios where you may want to deploy more than one version of a Lambda function. You may want to deploy a stable version of the function and an experimental version where you can try out new features.

Lambda versions let you do this.

In a later post, I’ll show how to use versions with aliases to route a percentage of requests to stable and experimental versions of a Lambda function.

But for this post, let’s stick with versions.

Introduction

I am simplifying the process of publishing Lambda versions a little in this post. I am not covering publishing a function version directly from source code, other than a brief mention at the end.

When you deploy a Lambda function, it is referred to as the “$LATEST” version. If you make a code change and deploy it again, this new code will be the “$LATEST” version, and the previous code is lost.

However, you can explicitly publish versions of a function. Let’s say you do this and name it “Stable” (it’s actually a description field, not a name field). You now have a “$LATEST” version of the function, and a “Stable” version of the function. At this point, they both have the same code.

When you make a change to your source code, and deploy it, the “$LATEST” version will be overridden, but the “Stable” version remains, now you have two versions of the function.

Lambda versions are referred to by their numbers, the first version is 1, the second is 2, and so on.

Below is a diagram of what happens when you deploy a function, make some changes, and deploy a version of a function.

The changing colors of the Lambda indicate changing code in the function
The changing colors of the Lambda indicate changing code in the function
  1. Write your code, and deploy. At this stage, you have only the “$LATEST” version of the function.
  2. Update your code, and deploy. This deployment will override the “$LATEST” version, and create a new “$LATEST” version.
  3. Publish the “$LATEST” version as a new version, named “Stable”. Both the “$LATEST” and “Stable” versions now have the same code.
  4. Make more changes to your code and deploy. The “$LATEST” version will be overridden, but the “Stable” version remains the same. You now have two versions of the function.

How to deploy a version of a function

Create a very simple Lambda function

Follow the steps in this post to create the Lambda function. Name it “SimpleFunction”.

Don’t deploy it yet!

1. Update the function handler and deploy

This corresponds to step 1 in the diagram above.

Open the SimpleFunction/src/SimpleFunction/Function.cs file.

Replace the FunctionHandler method with -

public string FunctionHandler(ILambdaContext context)
{
    return $"Function: {context.FunctionName}, Version: {context.FunctionVersion}, Arn: {context.InvokedFunctionArn}"; 
}

Open a terminal in the SimpleFunction/src/SimpleFunction directory.

Run -

dotnet lambda deploy-function SimpleFunction

You will be asked to select an IAM role, or create a new one, at the bottom of the list will be *** Create new IAM Role ***, type in the associated number.

You will be asked for a role name, enter SimpleFunctionRole.

After this you will be prompted to select the IAM Policy to attach to the role, choose AWSLambdaBasicExecutionRole, it is number 6 on my list.

After a few seconds, the function will be deployed.

Invoke the function

From the command line run -

dotnet lambda invoke --function-name SimpleFunction

You will see output like this -

Function: SimpleFunction, Version: $LATEST, Arn: arn:aws:lambda:us-east-1:xxxxxxxxxx:function:SimpleFunction

Note that the version is $LATEST.

2. Update the code and deploy again

Add the word “Stable” to the start of the returned string.

public string FunctionHandler(ILambdaContext context)
{
    return $"Stable. Function: {context.FunctionName}, Version: {context.FunctionVersion}, Arn: {context.InvokedFunctionArn}"; 
}

Deploy using -

dotnet lambda deploy-function SimpleFunction

3. Publish the function as a new version

From the command line run -

aws lambda publish-version --function-name SimpleFunction --description Stable

You have now created a new version of the function, named “Stable”. As part of the output you will see a “Version” field, with the value “1”.

You now have a $LATEST version, and a “Stable” version, but both have the same code.

Invoke the function

From the command line run the below (note the :1 at the end of the function name) -

dotnet lambda invoke --function-name SimpleFunction:1 

You will see output like this -

Stable. Function: SimpleFunction, Version: 1, Arn: arn:aws:lambda:us-east-1:xxxxxxxxxx:function:SimpleFunction:1

4. Publish an experimental version as $LATEST

This corresponds to step 4 in the diagram above.

Update the function handler to the following -

public string FunctionHandler(ILambdaContext context)
{
    return $"Experimental. Function: {context.FunctionName}, Version: {context.FunctionVersion}, Arn: {context.InvokedFunctionArn}"; 
}

Deploy using -

dotnet lambda deploy-function SimpleFunction

Invoke the function

Invoke the function using the command line -

dotnet lambda invoke --function-name SimpleFunction

You will see output like this -

Experimental. Function: SimpleFunction, Version: $LATEST, Arn: arn:aws:lambda:us-east-1:xxxxxxxxxx:function:SimpleFunction

You now have two versions of the function deployed, each with different code.

The dotnet lambda command and versions

Above, I used the AWS CLI to create a version of a function from the $LATEST version. You can also use the dotnet lambda deploy-function command to create a new version of a function from the source code on your computer.

dotnet lambda deploy-function SimpleFunction --function-publish true --function-description Stable
comments powered by Disqus

Related