In today’s rapidly evolving cloud landscape, efficiently managing infrastructure has become paramount for businesses. With the rise of cloud computing, the concept of infrastructure as code (IaC) has gained immense popularity. Terraform, an open-source tool by HashiCorp, enables users to provision and manage infrastructure declaratively using simple configuration files.
in this blog, we will cover the following topics:
- Introduction to AWS EC2 and Terraform
- Install Terraform on Cloud shell
- Create 2 instances using Terraform
- Cleanup Resources
- Conclusion
- FAQs
Introduction to AWS EC2 and Terraform
What is AWS EC2?
AWS EC2 offers a simple web interface that allows users to obtain and configure capacity with minimal friction. It provides complete control of your computing resources and allows you to run on Amazon’s proven computing environment.
What is Terraform?
Terraform simplifies and automates the process of managing infrastructure. It codifies APIs into declarative configuration files, enabling you to define the desired state of your infrastructure using code.
Install Terraform on Cloud shell
Terraform can be downloaded and installed from the official website or via package managers like Homebrew (for macOS) or Chocolatey (for Windows). Once installed, Terraform CLI provides a powerful set of commands for infrastructure provisioning and management.
In this section, we will install and configure the Terraform on the AWS cloud shell.
- Initialize your cloud Shell Environment to check if your AWS Cloud Shell is available in your region, look for a shell icon in the top bar. Check out the image below.
Note: If you can’t find the icon, it means AWS cloudShell is not available in that region yet.
Click on the icon or click here to access the shell.
- Install Terraform We’ll make use of tfenv, Terraform version manager. It allows us to install and switch between multiple versions of Terraform. Clone tfenv by running this command :
git clone https://github.com/tfutils/tfenv.git ~/.tfenv mkdir ~/bin
- Make sysmlink for tfenv/bin/*scripts into a path ~/bin because it has already added to $PATH
ln -s ~/.tfenv/bin/* ~/bin/
- Install Terraform with tfenv. As of this moment the latest, the latest terraform is 3.9
tfenv install 1.3.9
- Tell tfenv to use the installed version
- To check if everything is set, we can check for the terraform version with.
terraform --version
The output should look like the screenshot below if everything goes well.
Now, Our Terraform is now installed on AWS Cloud Shell. Now we can create our instances by writing YAML files which we are created in next step.
Create 2 instances using Terraform
In this section, we will see how we create 2 instances on AWS using Terraform.
1. Set up AWS credentials in AWS Cloud Shell
a. Open AWS Cloud Shell in your AWS Management Console
b. Run the command aws configure to set up your access key and secret key. Enter your Access Key ID and Secret Access Key when prompted.
aws configure
2. Create a Terraform configuration file
a. Open a new file in the AWS Cloud Shell editor and name it tf
b. Paste the following code into tf:
Note 1: You can find complete configuration file from this GitHub URL to avoid indentation:
provider "aws" { region = "eu-west-2" } resource "aws_instance" "node1" { ami = "ami-0c94855ba95c71c99" instance_type = "t2.micro" subnet_id = "subnet-0c93d46fa03982829" tags = { Name = "node1" } } resource "aws_instance" "node2" { ami = "ami-0c94855ba95c71c99" instance_type = "t2.micro" subnet_id = "subnet-0c93d46fa03982829" tags = { Name = "node2" } }
We have to change AMI ID and Subnet ID according to our preference. For this follow the Note 1 & Note 2 steps below.
Note 1: Replace <subnet_id> with the ID of your default subnet. You can find this by running the command aws ec2 describe-subnets in the AWS Cloud Shell.
aws ec2 describe-subnets
Note 2: To find the correct AMI ID for your region, you can use the AWS CLI command aws ec2 describe-images and filter the output for the AMI you need. For example:
aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-2.0.????????.?-x86_64-gp2" --query 'sort_by(Images, &CreationDate)[-1].ImageId' --output text
This command will return the latest Amazon Linux 2 AMI ID for your region. You can then replace the ami attribute in your Terraform configuration file with this new AMI ID.
3. Now open AWS Cloud Shell terminal and run terraform init command to initialize the directory.
terraform init
4. Now run the terraform plan command to validate.
terraform plan
5. Run the terraform apply command, type yes when asked for confirmation to execute the code.
terraform apply OR terraform apply -–auto-approve
6. Once the Terraform configuration has been applied, you should see two new instances in your AWS Management Console named “node1” and “node2“.
Now, Our 2 Instances have been created.
Cleanup Resources
1. Now run terraform destroy command, type yes when asked for confirmation to destroy resources.
terraform destroy OR terraform destroy --auto-approve
2. Verify the instances have been terminated
a. Go to the AWS Management Console and navigate to the EC2 Dashboard
b. Verify that the instances you created using Terraform have been terminated and are no longer running.
By following these steps, you can safely delete the instances you created using Terraform and clean up any remaining resources that are no longer needed.
Conclusion
In conclusion, using Terraform to create EC2 instances on AWS offers numerous benefits, including Infrastructure as Code, version control, and resource management. By following the steps outlined in this guide and adhering to best practices, you can effectively manage your EC2 infrastructure with Terraform.
FAQs
Can I use Terraform to create other AWS resources besides EC2 instances?
Yes, Terraform supports provisioning a wide range of AWS resources, including but not limited to S3 buckets, RDS databases, and VPCs.
Is Terraform suitable for managing infrastructure in a multi-cloud environment?
Absolutely, Terraform's multi-cloud support enables you to manage infrastructure across multiple cloud providers using a single tool.
Can I integrate Terraform with CI/CD pipelines?
Yes, Terraform integrates seamlessly with popular CI/CD tools like Jenkins and GitLab CI, allowing you to automate infrastructure changes.
Does Terraform provide built-in support for security compliance?
While Terraform itself does not enforce security compliance, you can use Terraform modules and provider plugins to implement security best practices.
Is there a limit to the number of resources I can provision with Terraform?
There are no inherent limits to the number of resources you can provision with Terraform. However, you should be mindful of AWS service quotas and account limits.
Related/References
- Terraform Installation Overview
- HashiCorp Infrastructure Automation Certification: Terraform Associate
- Resource Manager In Oracle Cloud Infrastructure
- HashiCorp Certified Terraform Associate
- How to Provision AWS Infrastructure with Terraform?
- Terraform Cheat Sheet
- Why Choose Terraform Over Chef, Ansible, Puppet or CloudFormation?
Join FREE Class
🚀 Master Terraform & DevOps to get High-Paying Jobs! 🔥 Join our EXCLUSIVE Free class! 🚀
Get your hands dirty with lots of projects and labs based on Terraform and DevOps in our Program.Click on the below image to Register for Our FREE Class Now!
Leave a Reply