AWS EC2 Instance creation and loading file to Instance using Terraforms

ARAVIND ALLU
2 min readJul 21, 2020

We can create an AWS EC2 instance and also make code infrastructure appropriately using Terraforms.

Terraform is an open-source infrastructure as code software tool created by HashiCorp. It enables users to define and provision a data center infrastructure using a high-level configuration language known as Hashicorp Configuration Language, or optionally JSON.

Terraform Installation:

Manual Installation for terraforms by using the following link:

https://www.terraform.io/downloads.html

Install Terraform using HomeBrew on OS X by using the command:

brew install terraform

Move the terraforms to user local bin by using move command:

mv ~/Downloads/terraform /usr/local/bin/

Verify the installation:

terraform -help

Note: Launch the AWS EC2 AMI (Amazon Machine Image) and configure accordingly by making config file.

Configuring AWS and exporting values:

Configure the AWS CLI after by giving the following command.

pip install awscli

Then give AWS Configure command

aws configure

It asks for the secret key and secret access key and region. Provide it accordingly, so that it will be safe in our server environment and then export the credentials in our server by giving the commands like

export AWS_SECRET_KEY = ‘ABC’export AWS_SECRET_ACCESS_KEY = ‘DEF’

AWS Instance Launching and moving file and make code infrastructure:

Create a directory called terraform_folder and create a file called var.tf which indicates the constant variables for the file.

var.tf
provider.tf

By using the provider.tf file, we can upload the file to AWS instance and also setup the code infrastructure.

Now run the commands like:

Terraform init command installs the AWS provider plugins and sets up the packages installed.

terraform init

Terraform plan command plans the instance to performs the necessary operation.

terraform plan -lock=false

Terraform apply command will create the instance and performs required operations.

terraform apply -var="source_file=${source_file_path}" -var="destination_file=${destination_file_path}" -lock=false -auto-approve

To destroy the instance, use the following command.

terraform destroy -auto-approve

References:

--

--