Infrastructure as Code and Terraform

Tram Ho

Welcome to the series about Terraform. In the first lesson we will learn what IaC (Infrastructure as Code) is, what Terraform is and why we need it.

Infrastructure as Code

From the name of Infrastructure as Code, we can simply understand that we will write code to describe and provide (provisioning) our infrastructure. The Vietnamese word Infrastructure means infrastructure, and in our IT industry, I understand it is the infrastructure of the system, including the server, network, gateway, database, all the things needed to deploy the application. our application on the server environment. Infrastructure as Code is probably the most commonly used on Cloud environments.

For example, on AWS Cloud, normally, we will log in to the web console, then we need a virtual machine, then we operate on the web to create a virtual machine (EC2), we need a database, we will operate on the web to create a database . And slowly, their system infrastructure will expand, this is when we will have problems, we will not know what the current system is, even if we remember, if the manager That cloud quits, how will newcomers know the current infrastructure? In addition, what if someone deletes my EC2, we have to recreate it manually, but we don’t know how the EC2 guy created it with config before, even if there are docs, the re-creation is very time consuming. And what if the entire cloud infrastructure goes down, why don’t we just have to recreate the entire system infrastructure from scratch? Then IaC will help us solve the above problems, we will write files to describe and backup our infrastructure, if something happens like the whole infrastructure down or someone corrects something wrong on our infrastructure, we can easily redeploy it easily.

Terraform

In this IaC array, the most popular tool at the moment is probably Terraform. Terraform is an open-source HashiCorp, specialized for provisioning infrastructure, we just need to write code, then type a few simple CLI sentences, it will create Infrastructure for us, instead of reaching the web console to click and click very time-consuming. time.

The flow of the terraform will be as follows, we write the code, then we type the CLI command, and wait for it to provide the infrastructure, after it has created, it will create a state file to save our current infrastructure architecture.

There are many other tools that can do this like Ansible, for example, but Ansible is a Configuration Management tool, it is not created to focus on IaC arrays, so using it will take a lot of work. unnecessary things.

To deploy an application, we can do the following flow, use Terraform to create the infrastructure, then use Ansible to setup the necessary things for the server, like install docker for example, setup CI tools on the server. Then we use docker or kubernetes to run our application.

Why should you use Terraform?

Here are 4 advantages of Terraform over other tools:

  • Easy to use.
  • Open source and free.
  • Declarative programming: just describe what you need and Terraform does it for you.
  • It is possible to provide infrastructure for many different clouds such as AWS, GCP, Azure in the same configuration file, this is called Cloud-agnostic.

At this point, we have said a lot, now I will do a small example for us to understand better. In this series I will use Terraform to provisioning infrastructure on AWS (in other clouds I have not used).

And to do that, it is required that you have an AWS account, and we create an IAM user and give it admin permission, then we get its access key to configure it on our computer. Create a file at path ~/.aws/credentials with the following content:

Please follow the steps here https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html.

Then we install the terraform CLI. You can see it at this link https://learn.hashicorp.com/tutorials/terraform/install-cli.

Ok, after the installation is complete, let’s start writing code.

“Hello Terraform!”

In this example, we will use Terraform to create an EC2 on AWS Cloud, the language Terraform uses is called HashiCorp Configuration Language (HCL).

The steps we take are as follows:

  1. Write terraform files.
  2. Configure AWS providers.
  3. Do not create Terraform with the command terraform init.
  4. Deploy an EC2 instance with the command terraform apply.
  5. Delete EC2 with the command terraform destroy.

Create a file named main.tf and we type the following code:

Here we will specify we use aws provider, and our resource will be created in the region of us-west-2. Then we added the code to describe our EC2:

Above we use a block called resources, this is the most important block of the terraform, we will use this block to create our resource. Behind resources, we will have an additional value that is the resource type we want to create (this depends on what resource types our provider will provide), above our resource type is  aws_instance, and the last value. The same is the name of that resource, we can put whatever we want.

To see the properties of a certain resource, go to the page https://registry.terraform.io/ let’s see. For example, here I need to see the aws_instance property of the aws provider.

After clicking, the website will take you through the docs of how to use this provider. We click through Documentation.

Tìm kiếm aws_instance.

Each of our resources will have arguments (input) and attributes (output) depending on the resource type, and the attributes will have a type called computed attributes, which are attributes we only know when the resource has been created.

When we finish writing the config, we open the terminal and type terraform init, This step is required when we write a configuration for a new infrastructure, it will download the provider code to the current folder in which we write the file. main.tf.

After the init is done, we continue to type the apply command so that it creates EC2 for us.

Once it’s done, go to your aws console and you’ll see EC2 has been created.

Bây giờ nếu ta muốn xóa EC2 đi, ta chỉ cần chạy câu lệnh destroy.

When we go to the aws console, we will see that our EC2 has been deleted successfully. So we’ve finished our first example with Terraform.

Conclude

So we have learned about what IaC is and how Terraform is used. As you can see, with Terraform, we create and delete resources very easily. If you have any questions or need more clarification, you can ask in the comment section below. In the next post, I will talk more deeply about how to write Terraform’s config file and life cycle when it creates a resoruce, hope you guys will follow my series.

Share the news now