Creating an EC2 Instance on AWS using Terraform
A step-by-step guide to setting up your EC2 instance on AWS with Terraform code.
Intro ๐ณ๏ธ
Hey friends and welcome to my blog post about creating an Amazon Web Services (AWS) EC2 instance using Terraform! My name is Bilal Khan, and in this post, I will guide you through the process of creating an EC2 instance using Terraform code.
Before we get started, let me give you a brief introduction to AWS and Terraform.
What is AWS? โ๏ธ
AWS is a cloud computing platform that provides various services, including computing, storage, and databases.
What is Terraform? ๐ต
Terraform is a tool that allows you to manage infrastructure as code. With Terraform, you can create, update, and delete infrastructure resources such as EC2 instances, security groups, and VPCs.
In this blog post, I will show you how to create an AWS instance using Terraform but before this.
Here is a video about this topic ๐
Set up your AWS account ๐
To get started, you will need an AWS account. If you don't have one, you can create a free account on the AWS website. Once you have an AWS account, you will need to set up your credentials to access AWS through Terraform.
Install Terraform ๐
To create an AWS instance using Terraform, you will need to have Terraform installed on your computer. You can download Terraform from the official website and install it on your system.
Configure the AWS provider ๐ช
The first thing you need to do is configure the AWS provider. You can do this by creating a new file by the name of main.tf and adding the following code:
provider "aws" {
region = "ap-south-1"
}
This code specifies the AWS region that you want to use. In this example, we are using the Asia Pacific (Mumbai) region.
Define your AWS instance โ๏ธ
The next step is to define your AWS instance. You can do this by adding the following code to your Terraform configuration file:
resource "aws_instance" "example" {
ami = "put the ami image here"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
In this code, we are creating an AWS instance of the type t2.micro with an Amazon Machine Image (AMI) that you can create, copy its ID and paste it here and at the end, add a tag with the name "example-instance".
I have shown you in the video above how you can create an AMI image.
Initialize your Terraform configuration ๐ฌ
After you have defined your AWS instance, you need to initialize your Terraform configuration. You can do this by running the following command in your terminal:
terraform init
This command will initialize the Terraform configuration and download any necessary plugins.
Preview your changes ๐บ
Once you have initialized your Terraform configuration, you can preview the changes that will be made to your AWS account by running the following command:
terraform plan
Apply your changes ๐ฅ
If you are happy with the changes that Terraform will make to your AWS account, you can apply them by running the following command:
terraform apply
This command will create your AWS instance and any other resources that you have defined in your Terraform configuration file.
Verify your AWS instance โ๏ธ
After you have created your AWS instance, you can verify that it is running by logging into your AWS account and opening the EC2 instance page. You will see that a new instance is created.
Conclusion ๐โโ๏ธ
In this blog post, we have shown you how to create an AWS instance using Terraform.
By following these steps, you can quickly and easily create virtual machines in the cloud that you can use to run your applications. Terraform is a powerful tool that can help you automate your cloud infrastructure and make it more scalable and reliable.
That's it for now. Did you like this blog? Please let me know.
You can Buy Me a Coffee if you want to and please don't forget to follow me on YouTube, Twitter, and LinkedIn also.
Happy Learning!