This is the simplest tutorial of IAC using terraform provision following resource
a. An EC2 Instance
b. S3 Bucket
c. VPC.
d. Covering security groups and subnets.
Pre Requisites
AWS account
Terraform downloaded in local PC.
Step1.
Using IAM AWS console create your keys
In PowerShell execute the command :
aws configure
This will now require 4 entries two of which are regarding access and other two are standard until changed.
Open up VSCode and make four files
- main.tf: Terraform configuration file.
- variables.tf: Define variables for your project.
- outputs.tf: Define output variables for your project.
- provider.tf: Store name of region The Code to be pasted in Main.tf is below and for further clarity it is commented.
# Create a custom VPC
resource "aws_vpc" "set14-vpc" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "set14-vpc"
}
}
#Create a public subnet
resource "aws_subnet" "set14-public-subnet" {
vpc_id = aws_vpc.set14-vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
tags = {
Name = "set14-public-subnet"
}
}
#creating an IGW
resource "aws_internet_gateway" "set14-igw" {
vpc_id = aws_vpc.set14-vpc.id
tags = {
Name = "main"
}
}
resource "aws_s3_bucket" "set-14-s3-backend" {
bucket = var.bucket_name
tags = {
Name = "set-14-s3-backend"
Environment = "Dev"
}
}
resource "aws_s3_bucket_acl" "set14-acl" {
bucket = aws_s3_bucket.set-14-s3-backend.id
acl = "public-read"
}
#security gp
resource "aws_security_group" "set14-sg" {
name = "allow_tls"
description = "Allow TLS inbound traffic"
vpc_id = aws_vpc.set14-vpc.id
ingress {
description = "TLS from VPC"
from_port = var.port_ssh
to_port = var.port_ssh
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "TLS from VPC"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "allow_tls"
}
}
#creating ec2 Instance
resource "aws_instance" "set14-ec2" {
ami = "ami-05a5f6298acdb05b6"
instance_type = "t2.micro"
subnet_id = aws_subnet.set14-public-subnet.id
vpc_security_group_ids = [aws_security_group.set14-sg.id]
user_data = <<-EOF
#!bin/bash
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
EOF
tags = {
Name = "HelloWorld"
Owner = "Kenny"
}
}
The Code to be pasted in variable.tf is below and for further clarity it is commented.
variable "bucket_name"{
default = "set-14-s3-backend"
}
variable "port_ssh"{
default = 22
}
The Code to be pasted in output.tf is below
output "Kenny-ip-address" {
value = aws_instance.set14-ec2.public_ip
}
output "Kenny-vpc-id" {
value = aws_vpc.set14-vpc.id
}
The Code to be pasted in provide.tf is below
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}
In VSC using terminal execute :
terraform init
In VSC using terminal execute :
terraform plan
In VSC using terminal execute :
terraform apply
If you open up the Amazon Console, you will be able to see the resource provisioned.
In order to destroy all resources and get back to initial stage .
In VSC using terminal execute :
terraform destroy
I hope you liked this tutorial !. Let me know in the comments
Top comments (0)