TERRAFORM LAB 2
Please reference link for INSTALLING TERRAFORM ON YOUR LOCAL MACHINE before starting this lab.
Deploying AWS EC2 instances with Terraform is one of the easiest ways to build infrastructure as code, and automate the provisioning, deployment and maintenance of resources to EC2 as well as custom solutions. This lab will walk you through the basics of configuring a single instance using a simple configuration file and the Terraform provider.
Prerequisites:
AWS access and secret keys are required to provision resources on AWS cloud.
- Open Visual Code Studio then click on File > Preferences > Extensions then search and install Terraform extension
- Login to AWS console, click on Username on top right corner and go to My Security Credentials

- Click on Access Keys and Create New Key
Step I: Open File Explorer, navigate to Desktop and create a folder terraform_workspace.
Step II: Once folder has been created, open Visual Code Studio and add folder to workspace
Step III: Create a new file main.tf and copy the below code in yellow color
provider "aws" {
access_key = "ACCESS KEY"
secret_key = "SECRET KEY"
region = "us-east-2"
}
resource "aws_instance" "ec2" {
ami = "ami-0a91cd140a1fc148a"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.ec2_sg.id]
tags = {
Name = "ec2_instance"
}
}
Add the block below in main.tf to output the Private IP, Public IP and EC2 Name after creation. (Note: This is not required)
output "ec2_ip" {
value = [aws_instance.ec2.*.private_ip]
}
output "ec2_ip_public" {
value = [aws_instance.ec2.*.public_ip]
}
output "ec2_name" {
value = [aws_instance.ec2.*.tags.Name]
}
Name = "ec2-dev-sg"
}
Execute the below command
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
No comments:
Post a Comment