Introduction to Infrastructure as Code with Terraform and How to Configure Terraform Server

Terraform Server and its various commands
Figure 7 : Terraform Server and its various commands

What is Infrastucture as Code (IaC) ?

  • In our previous articles AWS Infrastucture we have created and configured infrastructure through graphical user interface (GUI). In order to avoid this manual steps to bring up the network , we now have a better way to do such things : Infrastructure as Code (IaC).

  • Infrastructure as Code allows you to manage infrastructure with configuration files rather than through graphical user interface. IaC is to write code to define, provision and manage your infrastructure in a repeatable way that you can version, share or reuse .

  • IaC helps us to set up a server or even a complicated infrastructure in a short span of time/in a single click which will save an hours of productivity and complexity .

  • There are so many tools in the market that helps us to achieve the infrastructure as code . Some of them are –
    • Chef
    • Puppet
    • Ansible
    • SaltStack
    • CloudFormation
    • Terraform

  • All of these can manage infrastructure and almost all of them are opensource as well. They work with different cloud providers (AWS,Google Cloud Platform,Azure…) except CloudFormation is limited to AWS only .

What is Terraform ?

  • Terraform is an open-source Infrastructure as Code tool by HashiCorp.

  • It provides the configuration of an infrastructure in a structured text files that can be managed easily like any other source code in a version control system.

  • Terraform supports a number of cloud infrastructure providers such as Amazon Web Services, IBM Cloud, Google Cloud Platform, Linode, Microsoft Azure, Oracle Cloud Infrastructure, or VMware vSphere as well as OpenStack.

  • Working with Terraform is divided into several phases. Main phases are –
    • init – Prepare your working directory for other commands.
    • validate – Check whether the configuration is valid.
    • plan – Show changes required by the current configuration.
    • apply – Create or update infrastructure.
    • destroy – Destroy previously created infrastructure.

Since this is going to be the process of  Infrastructure as a Code paradigm. We need a API programmatic access for AWS. Now let us see how to configure Terraform server or create an EC2 instance having terraform installed/configured within it .

We can then use this terraform instance for bringing up or destroy the infrastructure as required.

 

Configure Terraform Server : –


Step 1
 
: Create an EC2 instance of any type. (Here we have created t2.micro) and access it through SSH.

ec2instance

Figure 1 : EC2 instance of type t2.micro

Connect to an instance

Figure 2 : Connect to an instance


Step 2 : In order to connect to AWS , terraform has to successfully authenticate. It is done with the help of Programmatic API Keys (Access Key and Secret.

Setup Programmic Access :- 

    1. Login to the AWS Console and in the services, go to IAM .

    2. Add user and give the Programmic access to that user .
      AddUser

      Figure 3 : Add an IAM user with Programmic access

    3. Attach Existing Policies and select AdministratorAccess . ( Here for demo we are giving admin access to the user , but in actual scenario , we should attach only specific policies/role to this user depending on the resources we are going to create )
      AttachPolicies

      Figure 4 : Attach Existing Policies to IAM user

       
    4. Add Tags if required and click on Create User . A user will be successfully created having Access Key ID and Secret access Key .
      IAMUserCreated

      Figure 5 : User created with Access Key ID and Secret Key

    5. Go to the console where EC2 instance is connected and enter the command – 
       aws configure . Copy and paste the AWS Access Key ID and Secret Key ID & enter the default region name .

       

      configureKey

      Figure 6 : Configure Secret Key

Step 3 : Download and install AWS CLI . 

Since we are creating server on linux , use below command to download AWS CLI .

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install


Step 4
 : Finally we have to download and install Terraform CLI.

Install yum-config-manager to manage your repositories.

sudo yum install -y yum-utils


Use yum-config-manager to add the official HashiCorp Linux repository.

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo


Install Terraform

sudo yum install terraform


Our terraform server is now ready and we can check various phases/commands available in terraform .

TerraformCommands

Figure 7 : Terraform Server and its various commands


In next article , we will see how to create an infrastructure (EC2 instance) using terraform .

Hope you like the article . If this is worth reading atleast once , please do like and share…!!!

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: