Feature flags in terraform
Video size:
Abstract
Terraform has taken over the world in the space of infra as code. It has become a key part of building a reproducible infrastructure. This talk will show some features of terraform which you can use to implement functionality like feature flag in your terraform code.
Summary
-
Today we will be discussing about feature flags in Terraform. Feature flags are a software engineering technique which allow you to enable or disable feature without additional code. Using feature flag in infrastructure as code it helps to improve scalability.
-
demo shows how to provision a load balancer and a database using configuration file. If I run terraform plan again you can see it added four resource to change. So that's all for the demo.
-
Feature flag is a very powerful technique which has been implemented in software engineering for a long time. I think it's time we start using it as part of our infrastructure as code. It helps in adding flexibility, scalability and maintainability to our infrastructure. I hope this talk was useful.
Transcript
This transcript was autogenerated. To make changes, submit a PR.
Hello everyone, I would like you to welcome to this talk. Today we will
be discussing about feature flags in Terraform. Before we
begin, I would like to introduce myself. I'm Pravesh Sapper
and I've been working in the field of SRV and DevOps for quite some time.
The key topic we'll be discussing today are what are feature flags
and why should you use them? How to implement feature toggles in terraform
and how we can pass feature flag from configuration file and
we'll do a quick demo. So what is terraform?
Terraform is an API driven infrastructure as code tool created by Hashicorp.
It uses declarative language called etc.
As we all know that features have been implemented in software engineering for quite
some time. Let's talk a little bit about them. So what
are feature flags? It is a software engineering technique which
allow you to enable or disable feature without additional code.
They are also known as toggles or switches.
There are various benefits of using feature flags like running rollout
or rollback operation or allowing developer to test code
in control environment. Feature flag are passed
from either configuration file environment variables or dedicated feature flag
management platforms such as launch darkly.
Next we'll discuss about why we should use it in
our infrastructure as code. The first reason is it increases
the flexibility. Feature flag allows granular control over
rollout of changes and can be used to turn specific feature
or functionality on and off for different environment
and different user group. This allows for more efficient and
flexible development, testing and deployment of specific
feature, increasing the flexibility. You can selectively provision resource
based on their environment using feature flags. That is, you can have
a smaller instance on dev but larger more capable instance
on prod. Using feature flag in infrastructure as code it helps to
improve scalability has we know that feature flags can be used
to enable or disable sorting feature or functionality based on current
load on your infrastructure. Now what it does is it
allows us to scale infrastructure up or down as necessary
without need to make changes to the code, hence improving the
scalability. In general, using feature flag we get better control
of over our deployment using techniques such as blue, green or
canary. We can test and validate changes in a controlled
environment and quickly roll back changes if necessary.
It also helps to improve safety by reducing the risk of introducing
breaking changes. This helps to ensure that your infrastructure is
always in a stable and working state. Using feature
flags as part of your infrastructure as code configuration, we are able
to perform effective monitoring and debugging. Let me tell
you why, because if we use feature flag,
we are aware which changes were introduced incrementally and knowledge
of this allows us to isolate specific issue
of feature or functionality, hence improving the debugging.
Let's look into how we can achieve this. The key
player is count meta argument. It allows us to
create a specific resource, a specific number of
identical resources. Here in the example I have a resource block
to create AWS instance. You can see in the count
argument that we are asking terraform to provision four identical
AWS instances. One key thing to remember here is
we can pass zero to count as well, which means
if we set count to zero, then the resource will not be
created at all. Another key player in the story is
conditional expression. Conditional expression in terraform uses
value of a boolean expression to select one of two
values. So using count and conditional expression,
we can implement feature toggles in terraform. Let's look into some implementations.
First we will look into creating environment based feature toggles.
So basically environment based feature toggles are those
kind of feature flags which helps you to toggle between feature based on
the environment you are deploying to. To do that, we will first
create a variable named n and then we will use
that variable along with count and conditional expression in our
resource block. Here I have two resource block one to create a
small or puny Vm and another to create a beefy vm based on
the environment. If the value of n is dev, then the
value of count in puny Vm is set to one and hence smaller Vm
is created. And if the value of n is prod, then the value
of count in beefy Vm is set to three and hence three
instance of BFBM is created. We'll look into this in the
demo as well. Now let's look into how we can create feature
toggles based on specific value to toggle
specific resources. What we can do is similar to the thing
that we discussed before. We will create a local variable to enable
that flag and then pass it to the resource just like we did.
For environment based toggle. The working mechanism is
same, but instead of relying on environment here,
we rely on a specific feature flag to enable or disable creation of that
resource. Another way you can implement feature flag are at module level.
We create a flag for that module and pass it directly to the
module instead of passing it at resource level. This has been
possible after terraform 00:13 which was a good leap forward. The usage
is similar to implementing feature flag in resource. We created
a dedicated flag, but instead of passing to a resource level, we pass
the flag directly to the module and if the flag is set, it will
provision the resource. There are other ways you can use or
leverage feature flag like you could use it for blue green
deployments using floating ip in this lotion. Or if you are
using AWS, you can perform canary release using AWS load
balancer target group. I mean, possibilities are endless. Let's talk
about how to organize these flags. There are various
ways to do this, but first one is defining the feature flag
in local variable. We saw how to do this in our previous slides as well.
We define a flag as part of our local variable and then
use them as needed in other resources or module.
Another way we can organize feature flag is using configuration file.
This is my personal favorite as we can get a complete information
about the infrastructure without having to dig down deep into
variables or terraform configuration file. Here we
define variable in configuration file and then load this variable
into terraform and use it as needed. It's kind
of neat, right? Anyways, let's move on to the demo. Okay, in this demo we'll
be looking into the things that we discussed in the slides. So I have
environment based toggle created which is a variable named
n and I'm passing that n variable to
our droplet. Let's look into the droplet.
So I pass the droplet into the count with
conditional expression saying if the
value of environment is dev then it will provision a smaller vm.
And if the value of environment is broad then it will provision of beefy
vm. Let's run terraform plan and see what happens.
So as you can see that the value of inn was
defaulted to dev and based on that it provisioned puny
vm which can be verified from the output.
But let's see what happens if we override the
value of n to prod.
So as you can see, it detected the environment is
prod and provisioned beefy instance. The another thing that I want to
show you today is the feature flag using configuration
file. So I have two feature flags created here which
provisions the load balancer and provisions the database.
Let's see here in the module.
Basically what I'm doing is to provision
a load balancer. I'm passing count to module
itself. So if the value of provision lb or
value of provision load balancer flag is set to true,
the count will be one and it will provision the load balancer.
So let's set the value of provision
load balancer flags to true and run the code again.
Now we can see it provisioned the load balancer for us. Let's set
the value of database to true as well and run the
code. If I run terraform plan again you can
see it added four resource to change and if you scroll up and see
it has created the database as well. So that's all for the
demo. Before we end the talk, here are the key takeaway I
want to repeat again which are feature flag is a very powerful technique
which has been implemented in software engineering for a long time. I think it's
time we start using it as part of our infrastructure as code as well as
it helps in adding flexibility, scalability and maintainability to
our infrastructure as code. Here's the link to the source code
shown in the demo if you are keen a look and
here are the references I used to prepare the talk.
Thank you for listening. I hope this talk was useful. I would like
to connect with you all and talk more about terraform feature
flags and DevOps in general. Here are the links to my
socials. Thanks again and enjoy the rest of the conference.