Blue-Green Deployments With AWS Lambda

Effective ways of achieving zero downtime deployments.

•

Design with skepticism, and you will achieve resilience. Ask, “What can system X do to hurt me?” and then design a way to dodge, duck, dip, dive, and dodge whatever wrench your supposed ally throws — Michael T. Nygard

Developing resilient software is great, but updating it seamlessly without any disruptions to the users is a skillful art. It’s quite a challenge. One strategy that has gained momentum is the Blue-Green deployment method.

What is the Blue-Green Deployment Strategy?

The Blue-Green deployment strategy is a way of releasing the application updates with two environments, or two versions per se. The current version(Blue) and the latest version(Green) that is to be tested out.

The application traffic is directed to both versions by assigning certain weights. For example, We assign 90% of traffic to be routed to the current version and 10% of the traffic to the new version. Once the testing results are according to the expectations, the complete traffic can be redirected to the latest environment or green version. This ensures the application is thoroughly tested and the downtime of the application is reduced.

Another advantage of this strategy is that if any issues are found, it can be easily rolled back to the previous version. This increases a high application availability time and the end-users experience very minimal disruptions. Also, the testing is safe! Isn’t that cool!?

How do we achieve this with AWS Lambda?

As the AWS Lambda holds the center of the Serverless-verse, it is important to come up with many optimizing strategies, be it code or deployment. Providing a user experience without any disruptions is very important. AWS Lambda does this using versions and aliases.

Every version of the lambda function holds the snapshot of your lambda function. Lambda versions are immutable, that is once you publish a version, you cannot change it. Also, version names are numerical and autogenerated.

A lambda version holds the following:

  • The code and its related dependencies.
  • The runtime of the lambda function.
  • The environment variables and all related settings.
  • A unique ARN for every new version.

The next comes Aliases, an alias is simply a pointer to your lambda function’s version, you can use this alias to point to whatever version of the lambda you want into production. You can use it to control traffic between versions by specifying weights, we will see this in a bit.

Alright! With that said, let’s jump straight into the implementation:

Create your lambda function, I have made a slight change to the boilerplate code that is generated during creation.

If you want to know about lambda in detail, you can refer to one of my previous blogs here.

Now test it out once and publish this lambda, It’s that easy!

Describe as you need and click on Publish.

Once this is done, you will be able to see an immutable version of your lambda function that you won’t be able to edit, now to understand the Blue-Green deployment strategy, go to the code console, make a change to your lambda, and publish a new version. You can easily navigate to it from the navbar path that you see above, and here you will be able to edit.

Note: You won’t be able to publish a new version unless you make any changes to the lambda function

In the snapshot below, I made a new change to my lambda function, this time calling it to be green and follow the same steps that you did for the previous version.

In the next step, create an Alias.

Creating an Alias is very straightforward, you can give it any name, I named it Prod, and give a bit of description, click on Weighted alias, and select another version. As you can see, I selected version 2 for green and version 1 for blue, to indicate that the blue is the current one. I also specified 50% weights for both aliases to see quick results.

Once you save this, you will be taken to the Triggers tab.

Here, let’s create a trigger for our lambda function, We will directly create an API from here and when we hit the URL in the browser, we must be able to see the results.

Select API Gateway as a lambda trigger.

Here, You will be able to see the following options. Specify the options I have selected, You can notice that we are creating a REST API here. Make sure to set the Security to Open, This is just for demonstration, we don’t want to get into any authorization issues.

Once you save the above settings, you will see the API endpoint that API Gateway gives you.

That’s it! Copy the endpoint, paste it in the browser, and hit it multiple times, you should be able to see the desired output as below. As you reload the page multiple times, the response should keep changing from Blue to Green and vice-versa.

Conclusion

Achieving zero downtime deployments is a goal for any developer out there, This is very important to enhance the user experience and ensure high application availability. The Blue-Green Deployment strategy simplifies the process of managing the deployment process making it smoother and allowing a quick rollback at times of critical production issues.

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics