Creating and Retrieving EC2 Instance IDs with Terraform and AWS
An In-Depth Guide to Automating Infrastructure as Code in AWS
Intro ๐
Automation can be particularly useful in managing cloud infrastructure, and Amazon Web Services (AWS) provides a wide range of services that can be automated using tools like Terraform.
In this blog, I will be sharing the process of automating EC2 instances using Terraform and AWS and providing a step-by-step guide for others who are interested in doing the same.
Previous Links ๐ค
If you don't know what AWS and Terraform are and how you can use them then you check out my previous blog which mentioned them.
Here is a video about this topic ๐
Creating an EC2 instance with Terraform ๐
Terraform is an open-source tool that allows users to automate infrastructure as code. It can be used to create, modify, and delete AWS resources by writing code in the Terraform language. In this section, we will be discussing how to create an EC2 instance using Terraform.
Provider configuration ๐ชฃ
The first step in creating an EC2 instance with Terraform is to configure the AWS provider. This is done by specifying the region in which the EC2 instance will be created. In this example, we will be creating the EC2 instance in the ap-south-1 region.
provider "aws" {
region = "ap-south-1"
}
EC2 instance resource ๐ชต
The next step is to create an EC2 instance resource. This is done using the aws_instance resource type. In this example, we will be creating a t2.micro instance.
resource "aws_instance" "example" {
ami = "put the ami image here"
instance_type = "t2.micro"
}
The ami field is left empty as it will be populated with the appropriate AMI ID at runtime. The instance_type field specifies the instance type for the EC2 instance.
I have shown you in the video above how you can create an AMI image.
Retrieving the EC2 instance ID using data sources ๐
Data sources in Terraform allow users to retrieve information about existing resources. In this section, we will be discussing how to retrieve the ID of the EC2 instance we created in the previous section.
EC2 instance data source ๐พ
To retrieve information about an EC2 instance, we can use the aws_instance data source. In this example, we will be using the data source to retrieve information about the EC2 instance we created in the previous section.
data "aws_instance" "example_running" {
filter {
name = "instance-state-name"
values = ["running"]
}
instance_id = aws_instance.example.id
depends_on = [aws_instance.example]
}
The filter block is used to filter the EC2 instances based on their state. In this example, we are only interested in running instances. The instance_id field is used to specify the ID of the EC2 instance we want to retrieve information about. Finally, the depends_on field is used to ensure that the data source is only executed after the EC2 instance has been created.
Outputting the EC2 instance ID ๐ค
Once we have retrieved the EC2 instance ID using the data source, we can output it using the output block.
output "instance_id" {
value = data.aws_instance.example_running.id
}
This will output the ID of the running EC2 instance.
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 the AWS instance that you have defined in your Terraform configuration file and wait to retrieve the ID once it is running.
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 and is running successfully
Conclusion ๐โโ๏ธ
In this blog, we discussed how to create an EC2 instance using Terraform and how to retrieve its ID using data sources.
Terraform provides a powerful tool for automating infrastructure as code, and the aws_instance resource type and aws_instance data source can be used to create and retrieve information about EC2 instances in AWS. By following the steps outlined in this blog, users can create and manage EC2 instances in a repeatable and scalable way.
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!