Create infrastructure in 5 simple steps using IaC - Terraform
Infrastructure as code (IaC) tools allow you to manage infrastructure with configuration files rather than through a graphical user interface. IaC allows you to build, change, and manage your infrastructure in a safe, consistent, and repeatable way by defining resource configurations that you can version, reuse, and share.
OBJECTIVE: The objective of this blog post is:
- Steps involved in creating infrastructure using terraform
- Downloading docker image of your choice using terraform
You will find the link to the GitHub repository with sample code
To deploy infrastructure with Terraform, the following fives steps are involved,
1. Scope: Identify the infrastructure of your project
2. Author: Write the configuration of your infrastructure
3. Initialise: Install the plugins Terraform needs to manage the infrastructure
4. Plan: Preview the changes Terraform will make to match your configuration
5. Apply: Execute the planned changes
Scope:
Write a IaC using terraform to pull any docker image by passing name
and version
. For example, nginx:latest, node:latest
Note: In this case, we require docker as provider for the terraform
Author:
The set of files used to describe infrastructure in Terraform is known as a Terraform Configuration
- Each Terraform configuration must be in its own directory.
- Create a
main.tf
file and define the configuration
2.1 Elements of a Terraform Configuration
Terraform Block: The terraform {}
block contains Terraform settings, including providers. Terraform will be relying on this to provision of infrastructure.
2.1.1 Provider:
Each provider holds the source
attribute that defines an optional hostname, a namespace and the provider type.
The above code snippets is an example for docker provider. Terraform installs providers from the Terraform Registry . In this case its docker so the docker provider’s source is defined as kreuzwerker/docker.
Note: The version
is optional but it is recommended to set it to have an expected behaviour
2.1.2 Resources:
Use resource
blocks to define components of your infrastructure. Resource blocks have two strings before the block.
- resource type:
docker_images
Terraform manages the docker_images resource with thedocker
provider - resource name:
docker_image_of_choice
Name as identifier to the resource. It is of your choice.
Note: Together the Name and Type of the resource, forms a unique identifier for the resource. For example, the name of the docker image would be docker_images.docker_image_of_choice.name
3. Initialise:
When a new configuration is created, terraform init
command will create the directory which would download and install the provider defined in the configuration. In this case it is the docker
provider.
4. Plan
It is highly recommended using consistent formatting for all configuration files.
Format: terraform fmt
command automatically updates the configurations for readability and consistency
Validate: confirm your configuration is syntactically valid and internally consistent by using the terraform validate
command.
5.Apply:
terraform apply
command would apply the configurations now and print the output. Before it applies any changes, Terraform prints out the execution plan with the action and changes to your infrastructure based on the configuration.
Terraform will pause
and wait
for the your approval before proceeding.
If the plan is acceptable, then type yes
and terraform will proceed creating the infrastructure.
Lets build the infrastructure for the use case
- When you run
terraform apply
the above code snippet will download a nginx:latest image - When you want to download a specific version of a docker image then execute
terraform apply -var=”image.name=<IMAGE_NAME_OF_YOUR_CHOICE> -v “image_version=<IMAGE_VERSION_OF_YOUR_CHOICE>"
for example, if you want to download node:16-alpine, then execute
terraform apply -var="image_name=NODE" -var="image_version=16-alpine"
Resources:
You can find the source code here
https://github.com/santhoshkumarravichandran/learn_terraform_docker_example