Let’s talk about “Infrastructure as code”. It has already become a standard. It will be used by default in a few years in the deploying process.

How “Infrastructure as code” works

“Infrastructure as code” means that the code works directly with the target system like cluster and server. The declarativity of the written code is another feature of the methodology.

Nowadays, the system administrator can describe the final state that is expected to be seen. Today there are many tools like Terraform, which organize declarativity.

What is Terraform?

Terraform is a tool for managing and configuring a cloud infrastructure. It works with services such as DigitalOcean, Heroku, AWS, and others. If you want to learn more about Terraform, you can read the official documentation.

In our case, we’ll use “Hetzner” as a cloud provider. Here we can implement the “infrastructure as code” approach.

To try it yourself, you should install Terraform. You also should have access to cloud resources.

How to Implement Infrastructure as Code with Hetzner

The aim: to deploy an app to Hetzner using Terraform and keeping configuration in the repository. It allows easy upgrading of the entire infrastructure or any part of it.

Solution

At first, we will write the main configuration of the hosting API, the state’s storage instance configuration, and the user data description.

The data can change, so we will store it in the variables file:

variables .tf contains variables. It is not a secret data and can be stored in the repository.

terraform.tfvars also contains variables, but it is not recommended storing it in the repository. Such data must be kept carefully.

Scheme

Scheme to Implement Infrastructure as Code with Hetzner.
Image.

Implementation

1. Create the repository to store configuration there

2. Create .gitignore file

3. It is necessary to get a token before start using API

Open Hetzner Cloud Console https://console.hetzner.cloud, choose your project, go to Access→ Tokens, and create a new token. You must copy the token because it will not be shown again. The token is linked to the project, and you need to create a new token within the project for linking it with API of another one.

Hetzner Cloud Console.

4. Create a configuration file in JSON format

state.tf is the backend configuration file. Here we will store infrastructure state. We choose the S3 bucket because it is easy to use. Create bucket S3 and write access to the bucket in the file.

main.tf is the file, where we define the main configurations

variables.tf is the variables file

instance.tf is the instance configuration file

user-data/instance.tpl file is a bash script with helper tasks. In our case:

  1. Install Docker
  2. Install Docker-Compose
  3. Another stuff

In addition

There is an example of terraform.tfvars file

Start

Just run commands in the configuration folder.

Image.

Conclusion

So, what we have:

  1. The working instance with the installed software.
  2. Configurations that are stored in the repository and will not be lost.
  3. “Infrastructure as code” approach is implemented.
  4. Reproducibility that allows configuring the new server easy and fast.

Infrastructure Optimization.
What Is FinOps and How It Changes Approach to Cloud Financial Management?

What Is FinOps and How It Changes Approach to Cloud...

What Is FinOps and How It Changes...

What Is FinOps and How It Changes Approach to Cloud Financial Management?

When a company adopts a hybrid, multi-cloud approach, it may struggle to optimize value and control cloud spend. One of the solutions in this...

Problems with using Istio in Kubernetes Jobs and docker images built using FROM scratch.

Golang, Docker “FROM scratch,” Kubernetes Cronjob,...

Golang, Docker “FROM scratch,”...

Golang, Docker “FROM scratch,” Kubernetes Cronjob, Istio or How to Make Them Friends

Docker has become an indispensable tool for developers and DevOps engineers, making it easier to package, distribute, and deploy applications...

Cloud service

Cloud Service: Which Type of Cloud Computing Services...

Cloud Service: Which Type of Cloud...

Cloud Service: Which Type of Cloud Computing Services to Choose in 2023?

Cloud computing is the process of delivering computing services (software, databases, servers and networking) over the Internet. It allows you to...