Create an EC2 instance using Terraform

Hello Security Gurus,

In this post today, we are going to learn creating an EC2 instance using Terraform. This EC2 instance will have a Jenkins running on it.

Full details with code is present in this GitHub repo:GitHub Repo for this Post

To learn the complete course, Checkout this link Learn to Setup EC2 Instance using Terraform

Few things first:

1) We need Terraform setup on our system
2) We need to generate a key pair in AWS account and download it on our system

Once these prerequisites are completed. We can complete this activity with below steps:

Step 0: Initialize Terraform
terraform init

Terraform init command is used to initialize Terraform on our system.

Step 1: Plan Resources
terraform plan -var-file="vars/dev-west-2.tfvars"

Plan command is used to plan the resources and see what our resources will look like once created.

Step 2: Apply Resources
terraform apply -var-file="vars/dev-west-2.tfvars"

Apply command in Terraform will create our resources.

Step 3: Commands to connect to EC2 instance via command line
chmod 400 <keypair> ssh -i <keypair> ec2-user@<public_dns>

Finally we need to delete the resources created using below command:

Step 4: Cleanup Terraform Resources
terraform destroy -var-file=”vars/dev-west-2.tfvars”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top