What is Infrastructure As Code ?
The typical life cycle of any java based application would look something like below, where, you have to first provision the server, then install & configure software and then you have to deploy the app:
Just imagine there are hundred of microservices talking to each other, it would be a cumbersome and error prone if we have to manually do these steps. The solution for this is Infrastructure As Code. It says treat your infrastructure provisioning as a code.
Some famous Infrastructure As Code are – Terraform AWS, CloudFormation, which can be used for provisioning server like creating virtual servers, load balancers, databases, networking configurations, etc.. Terraform is cloud-agnostic and can manage resources on multiple cloud platforms, including AWS, Azure, and Google Cloud. CloudFormation is specific to AWS and only manages AWS resources.
Some famous Infrastructure As Code tools for managing softwares (Installing and configuring) are Ansible, Chef, Puppet.
Terraform:
First you need to install Hashicorp Terraform from this official site.
Using Terraform you can provision server to any cloud platform, in this tutorial we will be using AWS.
Create the Terraform file with .tf extension:
main.tf
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 3.0" } } } # Configure the AWS Provider provider "aws" { region = "us-east-1" # VERSION IS NOT NEEDED HERE }
In the above terraform file we had mentioned the cloud provider- in this example its AWS.
Now we need to execute the above terraform file. For that we have to go to the place where we have saved the same terraform file and execute the below command:
terraform init
Ansible:
It is one of the tool of Infrastructure As Code. As we have seen in the above it deals with “Provision of Server” and “Install & Configuration of Softwares”.
To work with Ansible you have to first install Python and then install Ansible in your environment.
Ansible can not be installed in Windows. One of the option for windows user is to use Azure Cloud Shell.
Next you need to create some files, one is ansible.cfg and other is ansible_hosts. The sample code in the file is given below:
ansible.cfg:
[defaults] inventory=./ansible_hosts #inventory=inventory/01_aws_ec2.yml remote_user=ec2-user private_key_file=~/aws/aws_keys/default-ec2.pem host_key_checking=False retry_files_enabled=False #interpreter_python=auto_silent
ansible_hosts:
[dev] dev1 ansible_host=3.83.104.44 dev2 ansible_host=54.81.35.168 [qa] qa1 ansible_host=3.95.249.131 [first] dev1 qa1 [groupofgroups:children] dev first [devsubset] dev[1:2]