How to Use API Gateway with AWS Lambda Versioning and Aliases

2021/01/035 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Setup AWS Account
  3. Create Lambda Function
  4. Create API Gateway
  5. Test API Gateway
  6. Publish API Gateway
  7. Conclusion

Introduction

This blog shows how to build a simple API Gateway REST API with AWS Lambda. We'll also use AWS Lambda versioning and aliases to switch to a different version by using Node.js.

Here, we'll create a Lambda function by using Node.js, then we're going to create 2 versions of it and each will return a different value. Finally, we will create an alias called prod which will map to version 1. Furthermore, we'll switch from version 1 to version 2 and check the result. For the API Gateway, we will create a public REST API and connect it to the Lambda we just created.

Setup AWS Account

First, you'll need to have an AWS account. If you don't, you can create an AWS Free Tier account which will be free of charge for a one-year period. However, AWS Lambda and API Gateway are very cheap. For instance, Lambda costs $0.20 per 1M requests, and for API Gateway, the first 333 million requests cost $3.50, which is pretty cheap when you consider you don't have to host the server.

Create Lambda Function

Create Lambda Function from Scratch

Here, go to Lambda and click Create function, then choose Author from scratch.

Enter the function name. For example, let's use myLambdaTest.

Choose Node.js 14.x, so we'll be using Node.js version 14.

You can skip the entire Advanced Setting, but if you need to use your own VPC, subnet, or security group, you can use the advanced setting to modify the configuration you prefer.

You should be able to see your Lambda function. Click index.js, and you should be able to see the default code snippet provided by AWS.

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Publish New Version

Click Publish new version and enter the description. This will then become our version 1.

Create Alias

Go to the Lambda function, click Actions, then click Create an alias. Enter the alias name. Here we'll use prod. For the version, let's choose version 1. Now, here is the important thing for this blog: we notice our Lambda alias prod is mapping to version 1 now.

Also, check the Lambda function ARN. You should see something as follows. Please note this down, as we'll use it later at the API Gateway.

arn:aws:lambda:us-west-1:1234567890:function:myLambdaTest:prod

Modify the Lambda and Publish a New Version

Now, here we'll go back to the Lambda and modify the response as follows. We basically just modify the response. After the change, click Deploy and then click Actions to publish a new version.

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hi'),
    };
    return response;
};

At this point, we have 2 versions of our Lambda. One returns 'Hello from Lambda!' which is version 1, and the other returns 'Hi' which is version 2. The alias prod is using version 1.

Create API Gateway

Here, we'll create a public REST API and use the Lambda we just created as the back-end.

Go to API Gateway, then click the REST API Build button.

Choose New API and enter the API name.

Create Resource

At the API, click Actions, and then click Create Resource.

Enter the resource name and resource path.

Create Method

At the resource we just created, click Actions, then click Create Method. Here we'll create an HTTP method for the resource we have. We'll use Any for now, but you can specify GET, POST, PUT, or DELETE.

On the setup page, make sure the integration type is Lambda Function. At the Lambda Function field, choose the Lambda name we created earlier. You should see myLambdaTest. Now here is the important part: instead of using this name, we would apply the alias prod, so we change the name as follows:

myLambdaTest:prod

Click OK, so this API Gateway is now integrated with Lambda's alias prod. The reason we're doing it this way is so you can have control of what version the API Gateway uses to execute the Lambda.

Test API Gateway

Now, our API Gateway is pretty much finished. Let's click the Test button to test the API Gateway.

Click Test, choose the Method as GET, and you'll see the response shows 'Hello from Lambda!'. That's our Lambda's version 1.

Now, we'd like to update our Lambda alias prod to point to version 2.

Go back to the Lambda function, click the Aliases tab, choose prod, and then click Edit.

At the Edit alias window, change from version 1 to version 2 and click the Save button.

Now, go back to the API Gateway and test it again. You'll notice the response body is 'Hi' now. Using Lambda aliases gives you the flexibility of how you can deploy the Lambda function. You can do blue/green deployment or split the load across each version.

Publish API Gateway

Click Actions, then click Deploy API. At Deployment Stage, choose New Stage. Enter the stage name, then click the Deploy button.

Now, at the stage editor page, you should have a URL. Running the URL will execute the Lambda function behind the scenes and give you the same result.

Conclusion

This blog simply shows how to create a REST API with a Lambda function and how to switch Lambda versions with aliases.