1

Create an EC2 instance on AWS using Terraform

 2 years ago
source link: https://www.howtoforge.com/create-an-ec2-instance-on-aws-using-terraform/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Create an EC2 instance on AWS using Terraform

In this article, we will see how to create an EC2 Instance using Terraform. Before proceeding, I assume that you are familiar with the basics of Terraform and AWS EC2 Instance. If you want to learn to create an EC2 instance from the AWS console then click here

Pre-requisites

  1. Basic understanding of Terraform.
  2. Terraform installed on your system.
  3. AWS Account (Create if you don’t have one).
  4. 'access_key' & 'secret_key' of an AWS IAM User. (Click here to learn to create an IAM user with 'access_key' & 'secret_key' on AWS, )

 What we will do

  1. Write Terraform configuration files for creating an EC2 Instance.
  2. Create an EC2 using the Terraform configuration files.
  3. Delete the created EC2 instance using Terraform.

Write Terraform configuration files to create an EC2 Instance

Create a dedicated directory where you can create terraform configuration files.

Use the following command to create a directory and change your present working directory to it.

mkdir terraform
cd terraform/

 I am using "vim" as an editor to write in files, you can use an editor of your choice and copy paste the following configurations to create variables.tf, terraform.tfvars and  main.tf

Create 'main.tf' which is responsible to create an EC2  on to AWS. This main.tf will read values of variables from variables.tf and terraform.tfvars.

vim main.tf
provider "aws" {
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
    region = "eu-west-3"
}

resource "aws_instance" "ec2_instance" {
    ami = "${var.ami_id}"
    count = "${var.number_of_instances}"
    subnet_id = "${var.subnet_id}"
    instance_type = "${var.instance_type}"
    key_name = "${var.ami_key_pair_name}"
}

Change the value of "region" if you want to create the instance in some other region than what I have specified.

Create 'variables.tf' which contains the declaration and definition of the variables.

vim variables.tf
variable "access_key" {
        description = "Access key to AWS console"
}
variable "secret_key" {
        description = "Secret key to AWS console"
}


variable "instance_name" {
        description = "Name of the instance to be created"
        default = "test"
}

variable "instance_type" {
        default = "t2.micro"
}

variable "subnet_id" {
        description = "The VPC subnet the instance(s) will be created in"
        default = "subnet-a5a72ce8"
}

variable "ami_id" {
        description = "The AMI to use"
        default = "ami-096b8af6e7e8fb927"
}

variable "number_of_instances" {
        description = "number of instances to be created"
        default = 1
}


variable "ami_key_pair_name" {
        default = "tomcat"
}

Once you have created 'variables.tf', do not forget to change values assigned to variable. You must change ami_key_pair_name, ami_id and subnet_id as these are specific to my environment. You can keep the rest variable as is.

Create 'terraform.tfvars' which contains the definition of access_key and secret_key variables defined in the above file. We have kept the declaration of these 2 variables in 'terraform.tfvars' file.

The following keys need to be changed with the keys of your IAM user.

vim terraform.tfvars
access_key = "AKIAQ6GAIA5XIHHM2GJM"
secret_key = "pEPqnBW1jZ/PJPGn/wlydEge3kgGdCPzQ+xkJqG1"

Now, you should have 3 files, viz, variables.tf, terraform.tfvars and  main.tf

Create an EC2 Instance using the Terraform configuration files

Before you execute the following commands make sure you have configured the valid access_key and secret_key.

The first command to be used is 'terraform init'. This command downloads and installs plugins for providers used within the configuration. In our case it is AWS.

terraform init

The second command to be used is 'terraform plan'. This command is used to see the changes that will take place on the infrastructure.

 terraform plan

'terraform apply' command will create the resources on the AWS mentioned in the main.tf file. You will be prompted to provide your input to create the resources.

terraform apply

When you execute the above command,  you can see that 1 new resource has been added and 0 has been destroyed in the output.

You can go to the AWS EC2 console to verify if theEC2 instance is created or not.

Delete the created EC2 Instance using Terraform

If you no longer require resources you created using the configuration mentioned in the main.tf file, You can use the "terraform destroy" command to delete all those resources.

terraform destroy

Conclusion

In this article, we saw the steps to create an EC2 instance in the region of our choice. We also saw how the instance can be deleted.Advertisement


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK