Blue/Green Deployment of API Using AWS Lambda and API Gateway

Deploy an API using AWS API Gateway, which triggers two AWS Lambda Versions, on the basis of the weight specified in percentage(%).

Rushi Donga
Enlear Academy

--

What is Blue/Green Deployment?

Blue/Green Deployment is a deployment strategy, in which you deploy two different, but identical environments. One environment runs the current application version and another environment runs the updated application version.

Overview

  • Architecture
  • Pre-requisite
  • Configuration
  • Output

Architecture

Pre-requisite

  • An AWS account is required to implement the configurations.
  • Prior knowledge of AWS API Gateway and AWS Lambda.
  • AWS CLI installed and configured on your PC.

Configuration

Note : Configure all the resources in the same Region.

Log in to the AWS Management Console and search for the Lambda in the Search Bar and select Lambda.

You will then be redirected to the AWS Lambda Management Console.

Step 1 - Create a Lambda Function.

Note : We simply need to create a Lambda Function which returns a “Hello World” message. Below are the steps to create the same Lambda Function.

Click on Create Function button.

You will be redirected to a web page that allows you to create a Lambda function.

Select Author from Scratch from the options available.

In the Basic Information give the name of the function as test-lambda and select the Runtime to be Python 3.9.

Leave all the configurations to default and click on Create Function button at the bottom.

Edit the code in the Lambda function to return “Hello World from Version 1" and deploy the code.

Step 2 -Create Lambda Versions

After editing the code, click on Actions at the top and then Publish New Version.

Then give the Version Description if required and click on Publish button.

Now again edit the code of the Lambda function as below for deploying it as Version 2.

Now, again publish a new Lambda Version repeating the same process as above by clicking on Actions, then Publish Lambda Version and finally click on Publish button, on the dialog box appeared.

Step 3 - Create Lambda Alias

To create a Lambda Alias, click on Actions, then from the drop-down choose to Create Alias.

Give the name to the Alias as test-alias and choose Version any version.

Then, click on the Weighted alias, choose additional-version any version from the drop-down, and specify the weight to be 50%. Then click on the Save Button.

Step 4 — Create a REST API using API Gateway.

Come to the AWS API Gateway console, by searching API Gateway in the search bar in AWS Management Console.

Choose the API type to be REST API and click on Build Button.

Choose the protocol to be REST and choose New API in Create new API.

In Settings give the name to the API as test-API.

Leave all the settings to default and click on Create API.

Select Actions and create a Method by clicking on Actions and selecting Create Method.

Choose the GET Method from the drop-down and click on the tick button.

Select the Integration type to be Lambda Function and then choose the Lambda function we just created i.e. test-lambda.

Note : Below is the most Important step to point to the Lambda Alias.

Give the name of the Lambda Functions as below

<lambda-function-name>:${stageVariables.lambdaAlias}

Note : We will create Stage Variable named lambdaAlias in further steps.

Then click on the Save Button.

Note : Below is the most Important Step to give permission to API Gateway to invoke AWS Lambda Alias.

A dialog box will appear, displaying the CLI command. Copy the command and paste it at a secure place for future reference. Then click on the OK button.

Now, deploy the API by clicking on Actions and choosing Deploy API.

Deploy the API, by creating a new Stage, with the Stage Name as a test.

Now, choose Stages on the Left Navigation panel and choose Stage Variables.

Click on Add Stage Variable, give the Name as lambdaAlias and the Value as test-Alias.

Note : Here lambdaAlias in the Value is the same as we mentioned while choosing Lambda in the IntegrationType while creating GET Method.

Now, it’s time to run the Command, which we copied and pasted it for our future reference.

Now, open the Command Prompt with AWS CLI installed and configured with the region you have deployed the Architecture.

Run the command after changing the function-name attribute in the command, by replacing ${stageVariables.lambdaAlias} with the name of the Lambda Alias we created i.e. test-Alias.

You will get a response something like this in the JSON format.

Congratulations ♥

With this, you have successfully configured the above-mentioned Architecture.

Output

You can check the deployed API, by navigating to the AWS API Gateway Console, clicking on the API we just created.

Choose stages on the left navigation panel and choose the stage that we just created i.e. test.

A link will be displayed beside Invoke URL.

Open the URL in the browser and try refreshing the browser multiple times, you will find different responses from different Lambda Versions.

--

--

AWS Certified Solutions Architect. Presenting my learnings @AWS by translating them to a plain English. Writer @Enlear Academy | Writer @AWS in plain English