Hello Learners,
You guys are doing a fantastic job creating EC2 instances (mostly) manually. Today, letโs automate this process. How? Well, Terraform is the solution! ๐
What is Terraform? ๐
Terraform is an Infrastructure as Code (IaC) tool that allows you to create, manage, and update infrastructure resources such as virtual machines, networks, and storage in a repeatable, scalable, and automated way.
Task 1: Install Terraform on Your System ๐ฅ๏ธ
Let's get Terraform installed on your AWS EC2 instance step-by-step. Follow these instructions to get up and running! ๐๐
Step 1: Launch an EC2 Instance ๐ฅ๏ธ
Open the AWS Management Console:
Navigate to the EC2 Dashboard ๐.
Launch a New Instance:
Click on "Launch Instance" ๐.
Choose an Ubuntu AMI (Amazon Machine Image). For example, select "Ubuntu Server 20.04 LTS (HVM), SSD Volume Type" ๐ฅ๏ธ.
Select the instance type (e.g.,
t2.micro
which is free tier eligible) ๐.
Configure Instance Details:
Keep the default settings for "Configure Instance" โ๏ธ.
Add storage if needed (8 GB default is usually sufficient) ๐พ.
Add Tags:
- Optionally, add a tag to name your instance (e.g.,
Key: Name, Value: Terraform-Instance
) ๐ท๏ธ.
- Optionally, add a tag to name your instance (e.g.,
Configure Security Group:
Create a new security group or select an existing one ๐ก๏ธ.
Add a rule to allow SSH (port 22) access from your IP ๐.
Review and Launch:
Review your instance configuration ๐ง.
Click on "Launch" ๐.
Select an existing key pair or create a new one and download it ๐.
Step 2: Connect to Your EC2 Instance ๐
Open Terminal:
- If you're on Windows, you can use PuTTY . On macOS or Linux, use the terminal ๐ง๐.
Connect via SSH:
Use the following command to connect to your instance:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
Replace
/path/to/your-key.pem
with the path to your key file andyour-ec2-public-ip
with your instance's public IP address ๐.
Step 3: Update the Package List โณ
Update the package list:
sudo apt-get update
- This command updates the list of available packages and their versions ๐.
Step 4: Install Terraform ๐ป
Install the Required Dependencies:
Install software-properties-common package:
sudo apt-get install -y software-properties-common
This package allows you to easily manage your distribution and independent software vendor software sources ๐ ๏ธ.
Add the HashiCorp GPG Key:
Add the GPG key for HashiCorp products:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
This key is used to sign the Terraform packages, ensuring their authenticity ๐.
Add the Official HashiCorp Linux Repository:
Add the repository to your system:
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
This adds the repository for HashiCorp products to your list of repositories ๐ฆ.
Update the Package List Again:
Update the package list to include the new repository:
sudo apt-get update
This ensures the latest packages from the new repository are included in your package list ๐.
Install Terraform:
Finally, install Terraform:
sudo apt-get install -y terraform
This command installs Terraform on your system, making it ready for use ๐ ๏ธ.
Step 5: Verify the Installation โ
Check the Terraform Version:
Verify that Terraform is installed correctly:
terraform -version
You should see the version of Terraform displayed in the terminal, indicating that the installation was successful ๐.
Conclusion ๐
Congratulations! ๐ You've successfully installed Terraform on your AWS EC2 instance. You're now ready to start using Terraform to manage your infrastructure as code. Stay tuned for more tasks and happy automating! ๐โจ
Task 2: Answer the Following Questions โ
Why do we use Terraform? ๐ค
Terraform is used to automate the provisioning of infrastructure. It allows for the creation, updating, and versioning of your infrastructure safely and efficiently. By defining your infrastructure as code, you can easily replicate your environment and manage it with version control, reducing errors and manual intervention. ๐โจ
What is Infrastructure as Code (IaC)? ๐ป
Infrastructure as Code (IaC) is the practice of managing and provisioning computing infrastructure through machine-readable definition files, rather than through physical hardware configuration or interactive configuration tools. IaC allows for consistency, repeatability, and automation in infrastructure management. ๐ ๏ธ๐
What is a Resource? ๐ฆ
In Terraform, a resource is a component of your infrastructure, such as a virtual machine, storage bucket, or network. Resources are the basic building blocks of your infrastructure, and Terraform allows you to define these resources in configuration files. ๐๏ธ๐ง
What is a Provider? ๐
A provider in Terraform is a plugin that allows Terraform to interact with cloud providers, SaaS providers, or other APIs. Providers offer a set of resource types and data sources that Terraform can manage. For example, the AWS provider enables Terraform to create and manage AWS resources. ๐โ๏ธ
What is a State File in Terraform? ๐
The state file in Terraform is a critical component that keeps track of the current state of your infrastructure. It records the mappings between the resources defined in your configuration files and the actual resources in the real world. The state file is important because it helps Terraform determine the changes that need to be applied to achieve the desired state of the infrastructure. ๐๐
What is Desired and Current State? ๐๐
Desired State: This is the state defined in your Terraform configuration files, representing how you want your infrastructure to look. ๐โจ
Current State: This is the state of your infrastructure as recorded in the Terraform state file. It represents the actual state of your managed resources at a given time. ๐๐
Terraform compares the desired state with the current state to determine what changes are necessary to align the two. โ๏ธ๐ง
You can prepare for tomorrow's task from here ๐๐
We hope these tasks will help you understand how to write a basic Terraform configuration file and basic commands on Terraform.
Sample Terraform Configuration File ๐
Here's a basic example to get you started:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
Basic Terraform Commands ๐
terraform init: Initialize a working directory containing Terraform configuration files. ๐ ๏ธ
terraform plan: Create an execution plan to preview the changes that Terraform will make to your infrastructure. ๐๏ธ๐
terraform apply: Apply the changes required to reach the desired state of the configuration. ๐๐ง
terraform destroy: Destroy the Terraform-managed infrastructure. ๐ฅ๐๏ธ
By following these steps, youโll automate the process of creating and managing your infrastructure, making it more efficient and less prone to manual errors.
Happy automating with Terraform! ๐๐ค
#DevOps #Terraform #90DaysOfDevOpsChallenge ๐