Keep order and save time when managing your servers — Infrastructure as Code |

Infrastructure as Code — what is it all about?

Until recently, most companies operating in the IT sector and providing services related to the Web had to have their own servers, which required a lot of preparation — setting them up in data centres, configuring the operating system, network, setting up security mechanisms and preparing for the provision of services.

  • storing the server status in the repository — what is in the code reflects the actual state and in addition is clear and understandable, not only to administrators;
  • possibility to return to any moment in history — if the code is stored in the repository, then we know the entire history of changes and we can easily switch between them;
  • such representation is easily validated and tested, and the technologies we use provide us with such mechanisms;
  • deployment automation, which further accelerates the preparation and provision of servers in the production environment.

How do I get down to it?

Implementation of Infrastructure as Code in our case has been reduced to two steps — finding the software that would meet our assumptions and prepare the code that reflects the environment.

  • support for multiple Cloud platforms — at Droptica, we are not limited to one specific solution. We constantly test new products on the market to offer our clients more and more interesting and stable solutions (currently we use services offered by AWS, Digital Ocean and Linode, among others).
  • Immutable infrastructure — our goal is to store the actual state of the environment in a repository so that it can be retrieved and restored at any time. We cannot, therefore, have a situation where there were changes made to the state of the infrastructure, which has not previously been included in the code — which is a practical definition of the immutable infrastructure paradigm.

Tool selection

There are many tools available on the market that allow you to implement Infrastructure as Code. In our deliberations, we have taken into account a number of them, including Ansible, Cloud Formation and Terraform, which we finally decided to use.

- ec2: count: 10 image: ami-v1 instance_type: t2.micro
resource "aws_instance" "example" { count = 10 ami = "ami-v1" instance_type = "t2.micro" }
- ec2: count: 5 image: ami-v1 instance_type: t2.micro
resource "aws_instance" "example" { count = 15 ami = "ami-v1" instance_type = "t2.micro" }

How does Terraform work?

In this part of the article, we will present a short practical guide, which will allow you to take your first steps in the field of automation and implementation of IaC. We are going to focus on Ubuntu because we use this environment at Droptica on a daily basis.


Terraform can now be found in the official Ubuntu / Debian repositories, but the developer provides ready-made packages that can be simply unpacked and installed.

wget unzip cp terraform /usr/local/bin

Plug-in installation

Not all cloud providers are supported natively, so you might need to install plug-ins for some of them, for example, Linode and OVH. In most cases, these procedures are well described in the repositories, like in the following two cases:

Describing the first server

Using Terraform is really easy! The code shown above is enough to create the first virtual machine in AWS, but let’s deal with a slightly more complicated issue.

resource "aws_instance" "server-01" { ami = "ami-0d77397e" availability_zone = "eu-west-1a" instance_type = "t2.large" root_block_device { volume_type = "gp2" volume_size = "120" delete_on_termination = "true" } tags { Name = "server-01" Org = "droptica" } }
  • ami — image identifier (in our case Ubuntu Server 16.04);
  • availability_zone — region and zone in which the server will be provisioned;
  • Instance_type — the type and size of the instance.

Running the code

After the necessary preparations, the code is ready to run. However, before implementing the changes in the target environment, it is worth reading the list of steps that will be made. This will allow you to detect possible errors in the code. In order to do it, simply run the command:

$ terraform plan
> terraform plan aws_instance.example: Refreshing state... (ID: i-6a7c545b) (...) ~ aws_instance.example tags.%: "0" => "2" tags.Name: "" => "server-01" tags.Org: "" => "droptica" Plan: 1 to add, 0 to change, 0 to destroy.
terraform apply

What’s next?

Machine description itself is only the beginning since cloud computing services are now so extensive that they allow creating a much more complex architecture. At Droptica, we defined, among others, the network (both public and private), servers that host our websites, databases and load balancers — in short, we created a reliable hosting platform based on AWS.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store



Enterprise websites built with best Open Source solutions. We are an Agile software development company. We create big websites with Drupal 8, Symfony and React