Skip to content

Instantly share code, notes, and snippets.

@welmends
Last active August 24, 2022 20:35
Show Gist options
  • Save welmends/c7ade2d1b170d4a418d214bd62e11057 to your computer and use it in GitHub Desktop.
Save welmends/c7ade2d1b170d4a418d214bd62e11057 to your computer and use it in GitHub Desktop.
Terraform Simpel Project with AWS - Apache Server
# Terraform v1.2.0
#
# Terraform Simple Project with AWS - Apache Server
#
# $ terraform init
# $ terraform plan
# $ terraform apply --auto-approve
# $ terraform output
# $ terraform refresh
# $ terraform destroy -target aws_route_table_association.a --auto-approve
# $ terraform apply -target aws_route_table_association.a --auto-approve
# $ terraform state list
# $ terraform state show aws_instance.web-server-instance
# $ terraform destroy
#
# 0. Configuration
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
provider "aws" {
region = "us-east-1"
access_key = "AKIAWIFFFRMEGSCHOF4Y" # Replace with your Access Key
secret_key = "7KVa4mdP5x3KHZM2Dh7XY4jR+diXvOGI0ipHcyRg" # Replace with your Secret Key
}
variable "all_addrs" {
type = string
default = "0.0.0.0/0"
description = "All Addresses"
}
# 1. Create a vpc
resource "aws_vpc" "main-vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "main-vpc"
}
}
# 2. Create Internet Gateway
resource "aws_internet_gateway" "main-gw" {
vpc_id = aws_vpc.main-vpc.id
tags = {
Name = "main-gw"
}
}
# 3. Create Custom Route Table
resource "aws_route_table" "main-rt" {
vpc_id = aws_vpc.main-vpc.id
route {
cidr_block = var.all_addrs
gateway_id = aws_internet_gateway.main-gw.id
}
route {
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.main-gw.id
}
tags = {
Name = "main-rt"
}
}
# 4. Create Subnet
resource "aws_subnet" "main-subnet" {
vpc_id = aws_vpc.main-vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
tags = {
Name = "main-subnet"
}
}
# 5. Associate subnet with Route Table
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.main-subnet.id
route_table_id = aws_route_table.main-rt.id
}
# 6. Create Security Group to allow port 22, 80, 443
resource "aws_security_group" "allow-web" {
name = "allow-web-traffic"
description = "Allow Web inbound traffic"
vpc_id = aws_vpc.main-vpc.id
ingress {
description = "HTTPS"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [var.all_addrs]
}
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [var.all_addrs]
}
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.all_addrs]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [var.all_addrs]
}
tags = {
Name = "allow-web"
}
}
# 7. Create a network interface with an ip in the subnet that was created in step 4
resource "aws_network_interface" "web-server-ni" {
subnet_id = aws_subnet.main-subnet.id
private_ips = ["10.0.1.50"]
security_groups = [aws_security_group.allow-web.id]
}
# 8. Assign an Elastic IP to the network interface created in step 7
resource "aws_eip" "vm-eip" {
vpc = true
network_interface = aws_network_interface.web-server-ni.id
associate_with_private_ip = "10.0.1.50"
# instance = aws_instance.web-server-instance.id
depends_on = [
aws_internet_gateway.main-gw,
aws_instance.web-server-instance
]
}
# 9. Create ubuntu server and install/enable apache2
resource "aws_instance" "web-server-instance" {
ami = "ami-0c4f7023847b90238" # Replace with an Ubuntu Server 20.04 AMI
instance_type = "t2.micro"
availability_zone = "us-east-1a"
key_name = "main-key" # Need to create and download it on Key Pairs at EC2 Dashboard (main-key.pem)
network_interface {
device_index = 0
network_interface_id = aws_network_interface.web-server-ni.id
}
user_data = <<-EOF
#!/bin/bash
sudo apt update -y
sudo apt install net-tools
sudo apt install apache2 -y
sudo systemctl start apache2
sudo bash -c 'echo Web server is online > /var/www/html/index.html'
EOF
tags = {
Name = "web-server-instance"
}
}
# 10. Output the Ec2 instance id and Ec2 instance public ip
output "ec2_instance_id" {
value = aws_instance.web-server-instance.id
}
output "ec2_instance_private_ip" {
value = aws_eip.vm-eip.private_ip
}
output "ec2_instance_public_ip" {
value = aws_eip.vm-eip.public_ip
}
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment