Blog Blog Posts Business Management Process Analysis

How to Create an AWS EC2 Instance With Terraform?

In this blog, we will guide you through the step-by-step process of creating an AWS EC2 instance using Terraform. Starting from the initial setup of your AWS account to effectively managing the lifecycle of your instance, we will cover it all.

Check out this insightful video on AWS Tutorial for Beginners

“@context”: “”,
“@type”: “VideoObject”,
“name”: “AWS Certification | AWS Training | Intellipaat”,
“description”: “How to Create an AWS EC2 Instance With Terraform?”,
“thumbnailUrl”: “”,
“uploadDate”: “2023-07-14T08:00:00+08:00”,
“publisher”: {
“@type”: “Organization”,
“name”: “Intellipaat Software Solutions Pvt Ltd”,
“logo”: {
“@type”: “ImageObject”,
“url”: “”,
“width”: 124,
“height”: 43
“embedUrl”: “”

Understanding AWS EC2 and Terraform

AWS EC2 (Elastic Compute Cloud) is a web service provided by Amazon Web Services (AWS) that allows users to launch and manage virtual servers, known as instances in the cloud. 

It provides a flexible and scalable infrastructure for running various types of applications and workloads. With EC2, users have complete control over their virtual servers, including the ability to choose the type, operating system, and storage options of instance.

On the other hand, Terraform is an open-source infrastructure as a code tool developed by HashiCorp. It enables users to define and provision infrastructure resources using declarative language. Terraform supports multiple cloud providers, including AWS, Azure, and Google Cloud Platform, allowing users to manage their infrastructure consistently across different platforms.

Do you need the best AWS training in your area? Attend the AWS Certification Training at Intellipaat immediately!

Setting Up AWS Account and Access Key

To get started with AWS EC2 and Terraform, you first need to set up an AWS account. Look into the following steps to create an AWS account:

  1. Go to the AWS Management Console
  2. Click “Create a new AWS account” and read the instructions. Verify your identity using the provided verification method.
  3. Provide the necessary information, such as your email address, password, and payment details.
  4. Once your account is created, you can sign in to the AWS Management Console.

Next, you need to generate an access key to authenticate Terraform with your AWS account. Here’s how to do it:

  1. Sign in to the AWS Management Console.
  2. Open the “IAM” (Identity and Access Management) service.
  3. Choose “Users” from the sidebar and select click on “Add user.”
  4. Give a username and select “Programmatic access” as the access type.
  5. Attach the necessary permissions to the user (e.g., EC2 full access).
  6. Review the user details and create the user.
  7. On the final screen, you will see the access key ID and secret access key. Save this information securely as it will be required when configuring Terraform.

Interested in learning more? Go through this AWS Tutorial to gain a better understanding of AWS.

Installing and Configuring Terraform

To work with Terraform, you need to install it on your local machine and configure it with your AWS access key. Follow these steps:

Next, configure Terraform with your AWS access key:

Go through this blog on AWS Interview Question to crack the next job interview!

Creating a Terraform Project

After you’ve installed and configured Terraform, it’s time to start a new Terraform project. Here’s an example of how you could organize your project:

For example, to create an EC2 instance, you would define a resource block like this:

resource "aws_instance" "example" { ami = "ami-0c94855ba95c71c99" instance_type = "t2.micro" subnet_id = tags = { Name = "Example Instance" } }

In this example, we specify the Amazon Machine Image (AMI), instance type, subnet ID, and tags for the EC2 instance. Terraform will use this configuration to create the specified EC2 instance when you apply the configuration.

Writing Infrastructure as Code with Terraform

Writing Infrastructure as Code with Terraform

Infrastructure as Code (IaC) is a methodology that allows you to manage and provision infrastructure resources using code rather than manual processes. Terraform, being an IaC tool, enables you to define your infrastructure in a declarative language and maintain it as version-controlled code. Let’s explore how to write infrastructure as code with Terraform and create an AWS EC2 instance.

provider "aws" {
  access_key = var.aws_access_key
  secret_key = var.aws_secret_key
  region     = var.aws_region

In the above code, we use variables to store sensitive information like access keys and secret keys. Variables provide flexibility and allow us to reuse the same code across different environments or teams. You can define variables in a separate file or directly within the Terraform configuration file.

resource "aws_vpc" "my_vpc" { cidr_block = "" } resource "aws_subnet" "my_subnet" { vpc_id = cidr_block = "" }

In the above code, we define an AWS VPC resource with a specific CIDR block (IP range) and an AWS subnet resource associated with the VPC. This will create the networking infrastructure required for our EC2 instance.

resource "aws_security_group" "my_sg" {
  name        = "my-security-group"
  description = "Allow inbound SSH and HTTP traffic"
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = [""]
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = [""]
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = [""]

In the above code, we define an AWS security group resource with inbound rules to allow SSH and HTTP traffic, and an outbound rule to allow all traffic. Adjust the rules according to your specific requirements.

resource "aws_instance" "my_instance" {
  ami           = var.ec2_ami
  instance_type = var.ec2_instance_type
  subnet_id     =
  vpc_security_group_ids = [
  tags = {
    Name = "my-ec2-instance"

In the above code, we define an AWS EC2 instance resource with the specified AMI (Amazon Machine Image), instance type, subnet, and security group. Also, we assigned a tag to the instance for identification.

Initializing and Applying the Terraform Configuration

After writing the Terraform code, you need to initialize the project by running the following command:

terraform init

This command initializes the working directory, downloads the necessary provider plugins, and sets up the backend configuration.

To apply the Terraform configuration and create the EC2 instance, run the following command:

terraform apply

Terraform will show you the execution plan, including the resources to be created, modified, or destroyed. If everything looks good, you can confirm the plan, and Terraform will provision your EC2 instance.

Accessing and Managing Your EC2 Instance

Accessing and Managing Your EC2 Instance

By following the below mentioned steps, you can effectively manage the lifecycle of your EC2 instances, connect to them securely via SSH, and clean up the resources when they are no longer needed. 

Connecting to Your Instance via SSH

Once you have successfully launched an EC2 instance using Terraform, the next step is to connect and manage. The most common method to access your EC2 instance is through SSH (Secure Shell), which provides a secure and encrypted connection between your local machine and the remote server.

To connect to your EC2 instance via SSH, you’ll need the public IP address or public DNS name of the instance, along with the private key file (.pem) you specified during instance creation. Follow these steps to establish an SSH connection:

  1. Open your preferred terminal application and navigate to the directory where your private key file is stored.
  2. Set the permissions of the private key file to restrict access:
chmod 400 key.pem
  1. Connect to your EC2 instance using SSH and the private key file:
ssh -i key.pem ec2-user@

Note: Replace “key.pem” with the actual name of your private key file, and “” with the IP address or DNS name of your EC2 instance.

  1. If prompted, confirm the SSH fingerprint authenticity.

    Congratulations! You are now connected to your EC2 instance via SSH and have full control over it.

Managing the EC2 Instance Lifecycle

Once you have access to your EC2 instance, you can manage its lifecycle according to your requirements. Here are some important management tasks:

Cleaning Up and Destroying Resources

Cleaning up and destroying AWS resources is an essential step to prevent unnecessary costs and maintain a tidy infrastructure. Here’s how you can clean up the resources created by your EC2 instance:

terraform destroy

Confirm the destruction when prompted, and Terraform will proceed with removing the resources.


Creating an AWS EC2 instance with Terraform provides an efficient and scalable way to deploy virtual servers in the cloud. Throughout this guide, we have covered the basics of AWS EC2 and Terraform, from setting up your AWS account and configuring Terraform to writing infrastructure as code and managing the EC2 instance lifecycle.

By using Terraform, you can define your infrastructure in code, enabling you to version control, automate, and replicate your infrastructure across different environments. With the power of Terraform modules, you can easily reuse and share infrastructure configurations, reducing duplication and promoting consistency.

Visit our AWS Community for additional information if you’re still unsure about AWS.

The post How to Create an AWS EC2 Instance With Terraform? appeared first on Intellipaat Blog.

Blog: Intellipaat - Blog

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples