🌙

Getting Started with Terraform: Deploying Your First AWS Infrastructure Using Terraform Configuration Files (Part 3)

terraformawshashicorpdevopsInfrastructure as Code

Published on

April 29, 2023

Youtube Video link: https://youtu.be/bUmGc8Sd3Cw

Github repo: https://github.com/rashiddaha/terraform-course


Introduction

In Part 04 We will be Creating a VPC with Subnet, Security Group, and EC2 instance using Terraform on AWS.

In the previous parts( Part 01 , Part 02 ) of this Terraform series, we discussed Terraform and its key features and concepts, and we also walked through the installation and configuration of the AWS CLI and Terraform. In this part, we will create our first Terraform configuration file and deploy it to AWS.

Terraform uses configuration files to define the desired state of your infrastructure. These configuration files are written in the HashiCorp Configuration Language (HCL) or JSON. In this article, we will use HCL to write our configuration files.

Creating Your First Terraform Configuration File

To create your first Terraform configuration file, follow these steps:

  1. Open a text editor of your choice.

2. Create a new file and name it main.tf.
3. Copy and paste the following code into main.tf:
provider "aws" {
  region = "us-east-1"
}
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

This configuration file defines an AWS provider and an AWS instance resource.

The provider block defines the AWS provider and specifies the region where the resources will be created. In this example, we are using the us-east-1 region.
The resource block defines an AWS instance resource and specifies the AMI (Amazon Machine Image) and instance type. In this example, we are using the ami-0c55b159cbfafe1f0 AMI and t2.micro instance type.

To find Ami’s, you can visit this site https://cloud-images.ubuntu.com/locator/ec2/.

Deploying Your Infrastructure

To deploy your infrastructure using Terraform, follow these steps:

  1. Open a terminal window.
    2. Change to the directory where main.tf is located.
    3. Initialize the Terraform working directory by running the following command:
terraform init

This command will download the necessary provider plugins and initialize the working directory.

4. Preview the changes that Terraform will make by running the following command:

terraform plan

Enter the following command to check the format:

terraform fmt

One thing to note here is that the file must be saved with all of the most recent changes.

This is one thing that I love about Terraform! No more copy and pasting code back and forth between your editor and a validator.

Enter the following command to validate:

terraform validate

The validate runs checks that verify whether a configuration is syntactically valid and internally consistent. It is useful for general verification of attribute names and value types.

This command will show you the changes that Terraform will make to your infrastructure.

terraform plan

Apply the changes to your infrastructure by running the following command:

terraform apply

This command will create the resources defined in main.tf.

2. Then prompted, type yes to confirm that you want to apply the changes.
After the changes have been applied, Terraform will output the details of the created resources, including the instance ID and public IP address.

Verifying Your Infrastructure

To verify that your infrastructure has been deployed correctly, follow these steps:

  1. Open the AWS Management Console.

2. Navigate to the EC2 dashboard.
3. Verify that an EC2 instance has been created with the specified AMI and instance type.
4. Copy the public IP address of the instance.
5. Open a web browser and navigate to the public IP address.
6. Verify that you can access the default web page hosted on the instance.

Conclusion

In this article, we created our first Terraform configuration file and deployed it to AWS. We used the aws_instance resource to create an EC2 instance in the us-east-1 region.

Terraform enables you to define your infrastructure as code, which makes it easier to version, review, and collaborate on changes to your infrastructure. With Terraform, you can easily create, modify, and delete infrastructure resources, and you can do it all with a few simple commands.

In the next part of this Terraform series, we will discuss how to use Terraform to create more complex infrastructure, including VPCs, subnets, and security groups. Stay tuned!

In Part 04 We will be Creating a VPC with Subnet, Security Group, and EC2 instance using Terraform on AWS.



You can Folllow me at:

Note: Thank You for reading!

Note: For Quries and for projects work you cna react out to me at codewithmuh@gmail.com