| by Arround The Web | No comments

How to Create an EC2 Instance on AWS Using Terraform

With the rise in cloud computing technology, more industries are migrating their workloads to cloud-based infrastructure. As a result of this pattern, technologists have felt the need for some mechanism to automate the process of instance deployment (and other cloud resources). Terraform is one such Open-source tool to facilitate this progress.

What We Will Cover

This article will show how we can create an EC2 instance on AWS using Terraform. We will see an example of installing a simple web server on this instance. Let us first talk a little about the installation of Terraform.

How You Can Install Terraform

Official Terraform packages for various operating systems like Windows, Mac, and Linux-based distros, such as Ubuntu/Debian, CentOS/RHEL, etc., are available. In addition, Terraform also maintains pre-compiled binary and can also be compiled from the source. You can check the various installation procedures on the Terraform website. To verify your Terraform installation, run the following command:

1
$ terraform -version

Creating AWS EC2 Instance Using Terraform

After installing Terraform on your system, proceed with creating an EC2 instance on AWS. There are some files to effectively manage a Terraform deployment. Although we can create a single file and declare all the stuff, this approach will make the entire scenario clumsy. So, let us first create a working directory as seen in the following:

Step 1. Start with a folder that will hold all the configuration files. Create the folder, and move inside it as shown in the following:

1
$ mkdir linuxhint-terraform && cd linuxhint-terraform

Step 2. Let us create our first configuration file, “variables.tf”, that contains information about our AWS region and the type of instance we want to use, as shown in the following:

1
$ nano variables.tf

Now, put the below text inside it and save the file as shown in the following:

1
2
3
4
5
6
7
8
9
10
variable "aws_region" {
  description = "The AWS region to deploy the EC2 instance in."
  default   = "us-east-1"
}

variable "instance_type" {
  description = "instance type for ec2"
  default   =  "t2.micro"
}

Step 3. By default, when Terraform creates a new instance, the default security group associated with the instance denies all the traffic. We will therefore create a new file, “secgrp.tf”, to create a security group, “web-sg”, that will allow the inbound “SSH” and “HTTP” traffic, as well as all outbound traffic, as shown in the following:

1
$ nano secgrp.tf

Now, put the following code inside it as shown in the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
resource "aws_security_group" "web-sg" {
  name = “new-secgrp”
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
        from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

Step 4. Create a “main.tf” file that will define the desired infrastructure as shown in the following:

1
$ nano main.tf

Now, put the following configuration inside it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
terraform {

  required_providers {
    aws = {
    source  = "hashicorp/aws"

    version = "~> 3.27"

    }
  }

  required_version = ">= 0.14.9"

}

provider "aws" {

  region                = var.aws_region
  shared_credentials_file = "/home/User_Name/.aws/credentials"
  profile               = "profile1"

}

resource "aws_instance" "webserver" {

  ami = "ami-09d56f8956ab235b3"
  instance_type = var.instance_type
  key_name = "EC2-keyPair-Name"
 vpc_security_group_ids = [aws_security_group.web-sg.id]
associate_public_ip_address = true
  root_block_device {
    volume_type = "gp2"
    volume_size = "30"
    delete_on_termination = false

}

 

  user_data = <<EOF

#!/bin/bash

sudo apt-get update

sudo apt-get upgrade -y

sudo apt-get install apache2 -y

sudo systemctl restart apache2

sudo chmod 777 -R /var/www/html/

cd /var/www/html/

sudo echo "<h1>This is our test website deployed using Terraform.</h1>" > index.html

EOF

  tags = {
    Name = "ExampleEC2Instance"
  }
}

output "IPAddress" {
  value = "${aws_instance.webserver.public_ip}"
}

In the previous code, do not forget to change the “User-Name” to your system user’s name and the “EC2-keyPair-Name” to the name of the key pair in your case. Let us see a bit about the parameters used in the above files:

aws_instance: This creates an EC2 instance resource. Instances can be created, changed, and destroyed

AMI: Specify the AMI id to be used with the EC2 instance

instance_type: This option is used to declare the type of the instance to be used

key_name: Specifies the name of the key pair to use with the EC2 instance

vpc_security_group_ids: An argument for a list of security group IDs to attach

associate_public_ip_address: Specify whether to attach public IP with an instance inside a VPC

user_data: Used for passing commands/data on an instance when launching it

Now, initialize Terraform by running the following command:

1
$ terraform init

Now, apply the changes using the following command:

1
$ terraform apply

Verifying the Procedure

Now, let us check whether the desired EC2 instance is created. Head to the EC2 console and check for the running instances as shown in the following image:

Since our instance was created successfully, we will now see if the website we deployed is working correctly or not. Copy the DNS name or public IP of the instance and enter it inside a web browser as shown in the following:

Well done! Our web server is working nicely.

Cleaning up the Resources

When you have tested your infrastructure or when you do not require it, clean up the resources by running the following command:

1
$ terraform destroy

Conclusion

This guide taught us about creating an EC2 instance on AWS using Terraform. We have also demonstrated how to provision a simple AWS web server using Terraform.

Share Button

Source: linuxhint.com

Leave a Reply