🔗Terraform EC2 & Ansible Deployment

>>> Provisioning an AWS EC2 Instance with Terraform and Deploying a Web Application using Ansible

Sai Manasa
5 min readAug 12, 2024
Terraform EC2 & Ansible Deployment by Sai Manasa

Hello World! Welcome to the ultimate hands-on guide on a task using both Terraform and Ansible. Terraform and Ansible are powerful tools used for automating the deployment and management of IT infrastructure. While Terraform focuses on provisioning infrastructure, Ansible is used to configure and deploy applications on top of that infrastructure. Together, they provide a comprehensive solution for automating the full lifecycle of cloud environments. So, let's get started…

Overview:

  • What is Terraform?
  • What is Ansible?
  • Task Overview
  • Hands-On Guide
  • Architecture Diagram
  • Source Code

What is Terraform?

  • Terraform is an open-source infrastructure as code (IaC) tool created by HashiCorp.
  • It allows you to define and provision your entire infrastructure using a simple, declarative configuration language.
  • Terraform lets you manage infrastructure (servers, databases, networks, etc.) using code.
  • This means you can version, share, and collaborate on the infrastructure setup like with software code.
  • With Terraform, we can write configurations in a high-level, human-readable language called HashiCorp Configuration Language (HCL).
  • Terraform supports multiple cloud providers (such as AWS, Azure, Google Cloud) and on-premises solutions.
  • We can manage resources across different platforms from a single configuration.
  • Terraform keeps track of your infrastructure and makes changes safely.
  • It can create, update, and delete resources as needed, ensuring your infrastructure matches your configuration.
  • Terraform maintains a state file that records the current state of your infrastructure.
  • This state file helps Terraform determine what changes need to be made to achieve the desired infrastructure setup.

What is Ansible?

  • Ansible is an open-source automation tool that simplifies IT tasks such as configuration management, application deployment, and orchestration.
  • It’s designed to be simple, powerful, and agentless, which means it doesn’t require any special software to be installed on the managed systems.
  • Ansible helps manage and maintain the state of your systems’ configurations.
  • You can ensure that all servers are consistently configured and up-to-date with your desired settings.
  • Ansible can automate the process of deploying applications across your infrastructure, making it easier to roll out updates and new features without manual intervention.
  • Ansible uses YAML (Yet Another Markup Language) for its playbooks, which are easy to read and write.
  • Ansible playbooks are designed to be idempotent, meaning that running them multiple times will produce the same result.
  • This ensures that systems reach and maintain the desired state without unintended side effects.

Task Overview:

  • Launch an AWS EC2 server using Terraform.
  • Deploy a web app in the above-launched server using Ansible.

Hands-On Guide:

Step 1: Prerequisites

Step 2: Launch an EC2 server using Terraform

  • Create a directory to store the terraform files.
  • Write the configuration file:
  • Commands:
terraform init # Initializes a Terraform working directory by downloading necessary provider plugins and setting up the backend configuration.
terraform plan # Creates an execution plan, showing the changes that will be made to the infrastructure, without actually applying them.
terraform apply # Applies the changes required to reach the desired state of the configuration, as defined in the Terraform scripts.
  • The public IP address of the launched EC2 server will be displayed in the output of the ‘terraform apply’ command.
  • You can avoid giving the AWS credentials by configuring them using the AWS CLI:
1. Install AWS CLI.
2. COnfigure it using 'aws configure' command.
3. Once this is done, the terraform extracts the credentials from the default path where they are configured.
4. Or we can store the credentials in one file and can give the file path in the terraform code.
  • Outputs:

Step 3: Configure Ansible:

  • Create an Inventory file.
  • Create a file named hosts.ini with the below content, replacing ec2-public-IP with the actual IP address from the Terraform output.
  • Create a web app.
  • Create a file named index.html with the below code:
  • Create an Ansible playbook.
  • Create a file named playbook.yaml with the below code:

Step 4: Execution

  • Run the playbook to configure the EC2 instance:
ansible-playbook -i hosts.ini playbook.yaml
  • Explanation:
    ansible-playbook: This is the Ansible command-line tool used to run playbooks. A playbook is a YAML file that defines the automation tasks you want to perform on your managed nodes.
    -i hosts.ini: The -i option specifies the inventory file to use. In this case, hosts.ini is the inventory file that contains a list of hosts (servers) on which the playbook will be executed. The inventory file can define groups of hosts and specify variables for them.
    playbook.yaml: This is the playbook file that contains the instructions for configuring the hosts listed in the inventory file. The playbook specifies a series of tasks, such as installing software, configuring services, and managing files, that Ansible will run on the target hosts.
  • Output:

Step 5: Testing

  • Open a web browser and navigate to the public IP address of the EC2 instance.
  • http://<your-public-IP>/
  • Output:

Architecture Diagram:

Source Code:

Step into my GitHub Repo, where I’ve compiled a comprehensive collection of source code.

Let’s Connect:

Feel free to reach out, share your thoughts, or ask any questions. I’m excited to engage with you and learn from each other as we navigate this exciting field!

LinkedIn: Sai Manasa

GitHub: Sai Manasa

Happy Configure-ing 😄

Happy Learning 💻

--

--