Earlier in Datacenter Infrastructure were manage and provision through traditional way of physical hardware configuration or interactive configuration tool but now Infrastructure as a code has changed the picture of managing and provisioning the Infrastructure. Infrastructure as a code or IaC is the process of managing and provisioning computer data centers through machine-readable definition files.
Terraform one of the example of infrastructure as a code software developed by HashiCorp. It enable the user to build datacenter infrastructure with the help of high level configuration language on Private Cloud such as OpenStack or on Public Clouds like AWS, Microsoft Azure, Google Cloud Platform, IBM Cloud (formerly Bluemix), Oracle Cloud Infrastructure, or VMware vSphere Infrastructure.
The Infrastructure as a code is just like a programming scripts which is developed for automate the IT process. Codes are written in high level language or any descriptive language use to automate the series of static steps which must be repeated numerous times across multiple servers.
It is an ideal for DevOps environment as not only System Admin is require to perform Provisioning, Configuration management and Deployment of Infrastructure, Developer can also perform this activity only by writing the code in the language.
As Infrastructure as a code is adopted by DevOps it follows the Software design practice in which Codes versions are carefully controlled, Iteration of test are done and limit the deployment till the software is proven and approved to deployed in production.
There are many Infrastructure as Code capability tools which can be useful in deployment and configuration management like: vagrant, ansible, puppet, docker makes the whole process even easier. AWS is providing RESTful APIs that can be leveraged.
Below is the example of Ansible Play book which installs mysql-server on the remote machine (server), ensures that mysql is running, creates a user with the password, deletes the test database, creates the ansible_example database, copies a sql dump on to the machine, and restores it into ansible_example database.:
---
- hosts: server
sudo: yes
sudo_user: root
tasks:
- name: install mysql-server
apt: name=mysql-server state=present update_cache=yes
- name: install ansible dependencies
apt: name=python-mysqldb state=present
- name: Ensure mysql is running
service: name=mysql state=started
- name: Create user with the password and all previleges
mysql_user: login_user=root login_password="" name={{ mysql_user }} password={{ mysql_password }} priv=*.*:ALL host=% state=present
- name: Delete test database
mysql_db: name=test state=absent
- name: Create ansible_example database
mysql_db: name=ansible_example state=present
- name: Copy mysql back up dump to the remote_user
copy: src=dump.sql.bz2 dest=/tmp
- name: Restore the dump into ansible_example database
mysql_db: name=ansible_example state=import target=/tmp/dump.sql.bz2
Another example of AWS Cloud formation in this infrastructure-as-code example, the resource is WebServer in an AWS CloudFormation template.