In this tutorial, we will learn how to create an AWS EC2 instance using Terraform with the help of an example and how to add the name tag to the AWS ec2 instance after modifying the Terraform file.
This is a very easy-to-understand Terraform tutorial for beginners who are starting with Terraform. We will be creating only one file called main.tf.
Table of Contents
Requirements
For this tutorial, we need the following software to be installed & configured:
5 Steps to Create AWS EC2 Instance using Terraform
1. Once you fulfill the above requirement, just create a directory for this project and create a file called main.tf.
If you are in Mac or Linux operating system, you can use the below commands.
$ mkdir tf_ec2 && cd tf_ec2
$ touch main.tf
2. Now open main.tf in your favorite text editor and add the below lines. This is a very basic terraform code for creating an AWS ec2 instance.
resource "aws_instance" "st_web1" {
ami = "ami-007855ac798b5175e"
instance_type = "t2.micro"
}
The code declares a resource block with the type aws_instance. The resource block defines an EC2 instance that will be created in AWS. The st_web1 string is a unique name given to this resource block.
The instance_type attribute specifies the type of instance to create. In this case, it is set to t2.micro, which is a low-cost, general-purpose instance type that is suitable for a wide variety of workloads.
3. Now, initialize the terraform using the “terraform init” command. This command will simply initialize the backend and install any required providers, which are responsible for communicating with various infrastructure APIs in this case AWS.
➜ tf_ec2 $ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v4.61.0...
- Installed hashicorp/aws v4.61.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future.
Terraform has been successfully initialized!
4. Once terraform is initialized, you just have to run “terraform plan” to see what terraform going to create. This command is not required, though.
➜ tf_ec2 $ terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
+ create
Terraform will perform the following actions:
# aws_instance.st_web1 will be created
+ resource "aws_instance" "st_web1" {
+ ami = "ami-007855ac798b5175e"
+ arn = (known after apply)
+ associate_public_ip_address = (known after apply)
+ availability_zone = (known after apply)
+ cpu_core_count = (known after apply)
+ cpu_threads_per_core = (known after apply)
+ ebs_optimized = (known after apply)
+ get_password_data = false
+ host_id = (known after apply)
+ host_resource_group_arn = (known after apply)
+ iam_instance_profile = (known after apply)
+ id = (known after apply)
+ instance_initiated_shutdown_behavior = (known after apply)
+ instance_state = (known after apply)
+ instance_type = "t2.micro"
+ key_name = (known after apply)
..................
..................
+ source_dest_check = true
+ subnet_id = (known after apply)
+ tags_all = (known after apply)
+ tenancy = (known after apply)
+ user_data = (known after apply)
+ user_data_base64 = (known after apply)
+ user_data_replace_on_change = false
+ vpc_security_group_ids = (known after apply)
5. Once you are satisfied with the output of terraform plan command, just go ahead and run the “terraform apply” command to create an AWS ec2 instance using Terraform.
➜ tf_ec2 $ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
+ create
Terraform will perform the following actions:
# aws_instance.st_web1 will be created
+ resource "aws_instance" "st_web1" {
+ ami = "ami-007855ac798b5175e"
+ arn = (known after apply)
..........................................
..........................................
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
aws_instance.st_web1: Creating...
aws_instance.st_web1: Still creating... [10s elapsed]
aws_instance.st_web1: Still creating... [20s elapsed]
aws_instance.st_web1: Still creating... [30s elapsed]
aws_instance.st_web1: Creation complete after 32s [id=i-0537369c8ace134f8]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
As you can see from the above output, it is showing “1 added” which is for the ec2 instance. Along with this, it will add all the resources required to run the instance.
This is a very basic example, and the ec2 instance will be created in the default VPC directly.
Now to verify that your ec2 instance is created, just go AWS console and visit the EC2 dashboard.
Assigning Name to EC2 Instance using Terraform
As you might have observed, we did not add any name to the ec2 instance, and that is why there is no name in the AWS console.
Now to add the name to your ec2 instance, just add the tag parameter in your main.tf file like below:
resource "aws_instance" "st_web1" {
ami = "ami-007855ac798b5175e"
instance_type = "t2.micro"
tags = {
Name = "st_web1"
}
}
In the above code, we have added “tags” with a name tag which will add a name to your AWS EC2 instance.
After updating terraform file, just run “terraform plan” to see what is going to change and then run the “terraform apply” command.
Note: With the “terraform apply” command we use –auto-approve, this will run terraform apply command without waiting for user input and will automatically approve our apply command.
Once you have added the tag, you can go to the AWS console again and verify this.
GitHub Repo with sample code for creating AWS ec2 instance using Terraform: https://github.com/storagetutorials/terraform-aws-ec2
Conclusion
This was a basic Terraform tutorial where we learned how to create an AWS ec2 instance using Terraform and add a name to an AWS EC2 instance with the help of Terraform.
You can also modify the terraform code to include other optional attributes like security_groups, key_name, etc.
We are going to cover more advanced Terraform tutorials on this website like using user data, importing Terraform modules, destroying specific resources, etc., so do make sure to bookmark the page or subscribe to our free newsletter.