AWS with Terraform — EC2
Structure AWS with Terraform — EC2
Let’s create EC2 instance using Terraform and also cover some AWS concept
To understand AWS and Terraform better, we will cover each concept at a time. This article will demonstrate on how to configure an EC2 instance with necessary configuration using Terraform.
Let’s install Terraform and AWS CLI
Before starting the process, install Terraform in the system. You can follow the installation process from this documentation. Once it’s installed, check the version (just to make sure installation went well).

In order to provide AWS authentication/access for Terraform, we need to install AWS cli. Follow this documentation for the installation and configuration. Once it’s installed, check the version.

Let’s begin to code
Let’s start with the terraform version configuration (This is similar to the package.json/requirements.txt file). This file will contain the version of Terraform and the providers (such as AWS, Azure) that we are going to use.
Create terraform.tf to define versions
It’s always a best practice to maintain the versions of the terraform and the providers that are been used.
Create a file called terraform.tf
and add the following code in it.

There is an option of changing the local name for the provider inside required_providers. For example, you can say my-aws instead of aws (in line 5). But it is not a best practice.
Once terraform.tf
has been created and configured as above, run terraform init
which will install the required provider modules into our project directory.

Let’s configure EC2 instance
Create a file called main.tf
and initialize aws
provider with necessary values.

In order to create an EC2 instance, we need the necessary information such as:
- What ami (Amazon Machine Image) to use? — AWS provides a wide range of machine images. Machine image is nothing but the type of server you need, similar to your PC/laptop — whether you need a Windows/Linux/MacOS.
You can check those list of machine images from AWS console.

- What instance type you need ? Instance type is the size of the machine that we are going to create. There are multiple instance types that AWS provides (documentation)and the below screenshot is some of the General Purpose instance types.

For this article, we will go with the free-tier configuration (Linux OS with t2.micro instance type).
we will use the resource
that aws provides to create the EC2 instance (in terraform perspective, it's aws_instance
). Below is the code to create an EC2 instance.

The complete main.tf
looks like:
Let’s apply these changes and see what happens.
You can also run
terraform validate
to verify the changes that’s been made. If there is any issues, it will output the error and we can fix it before runningapply
.
Run terraform apply
to apply the configurations that we defined. It will then populate all the changes that’s going to be made as below.

Once you verified, type yes
and run it.

Once the command completes the process and shows the output as above, navigate to your AWS console and go to the EC2 instances. You will find the new EC2 instance that’s been created.

Now we have created the EC2 instance and it’s up and running. Let’s make it more interesting now.
Run web server inside EC2 through Terraform
Let’s run a small apache web server inside the EC2 instance so we can access it from the browser. Since the created EC2 instance is new, it doesn’t have any setup in it (kind of a fresh new laptop).
So we are going to install http
and create a html file so we can access it via browser. To run a command when instancing the EC2 instance, we are going to use user_data
Let’s create a simple shell script which will install http
and html file for us.
Now let’s use the above file to run the command when EC2 instance is created. Add the user_data
argument into the resources
that we defined for EC2 instance initialization.

You can refer to any file in terraform using the
file
function. Refer to this documentation for more info.
The overall code in the main.tf
looks like:
Let’s run the terraform by terraform apply
command.
You can also run
terraform apply --auto-approve
if you want to void the interruption. It will not ask for the permission to apply.
Once it’s completed, navigate to your EC2 instance dashboard and copy the public IP address.

Open a new tab and paste the address that you copied. (Make sure you use http
and not https
). Once you enter the address in your browser and check, you will see something like this:

Ahhh…why is that?
This is because we have to allow incoming traffic to access our EC2 instance. At the moment, there is no security that is added to allow incoming traffic. You can check this by validating into the security in your EC2 instance dashboard.

As you can see, there is no rule added in the inbound rules
to allow incoming traffic. Let’s fix it in our terraform.
These security configurations comes under security groups
under Networks & security
in AWS. So, let’s configure it in our terraform file.

Let’s link the created security group to our EC2 instance resource. To link it, we are going to use vpc_security_group_ids
//<resource_name.local_name_of_created_resource.created_id>
vpc_security_group_ids = [aws_security_group.web_security.id]
The overall main.tf
looks like:
Now, apply the changes using terraform apply
command.
Once it’s done, go to the public IP address of the EC2 instance created. (Note: use http
)

Cleanup
Now let’s delete the created instance from AWS. To remove the instance that we created, run terraform destroy
and it will show the resources that it’s going to delete.
Type yes
and run it.
You can also run
terraform destroy --auto-approve
if you want to void the interruption. It will not ask for the permission to apply.
Once terraform completes the process, navigate to EC2 instance and refresh the instance table.

Thanks for reading my blog. Feel free to provide feedback about this blog so I can improve on it.
We will meet on the next topic (If you got one, let me know :) ) on Structure AWS with Terraform.