Launching an Web Application on AWS Cloud using Terraform , GitHub, EFS and CloudFront

Task Details

LET’S GET STARTED!!

aws configure --profile aman1
provider "aws" {
region = "ap-south-1"
profile = "aman1"
}
resource "aws_security_group" "my_security_group" {
name = "my_security_group"
description = "Allow HTTP inbound traffic"
vpc_id = "vpc-87819cef"ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
description = "HTTP from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}ingress {
description = "EFS-storage"
from_port = 2049
to_port = 2049
protocol = "tcp"
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 = "aman_sg"
}
}
resource "aws_instance" "myinstance" {
ami = "ami-0447a12f28fddb066"
instance_type = "t2.micro"
key_name = "mykey"
security_groups = ["my_security_group"]connection {
type = "ssh"
user = "ec2-user"
private_key = file("C:/Users/user/Downloads/mykey.pem")
host = aws_instance.myinstance.public_ip
}provisioner "remote-exec" {
inline = [
"sudo yum install httpd php git -y",
"sudo systemctl restart httpd",
"sudo systemctl enable httpd",
]
}
tags = {
Name = "my-os"
}
}
resource "null_resource" "image"{
provisioner "local-exec" {
command = "git clone https://github.com/AmanGoyal31/multicloud.git images"
}
}
resource "aws_efs_file_system" "efs1" {
depends_on = [aws_security_group.my_security_group , aws_instance.myinstance ,]
creation_token = "EFS-file"
tags = {
Name = "efs-storage"
}
}
resource "aws_efs_mount_target" "EFS_mount" {
depends on = [aws_efs_file_system.efs1,]
file_system_id = aws_efs_file_system.efs1.id
subnet_id = aws_instance.myinstance.subnet_id
security_groups = [aws_security_group.my_security_group.id]
}
output "myos_ip" {
value = aws_instance.myinstance.public_ip
}resource "null_resource" "nulllocal2" {
provisioner "local-exec" {
command = "echo ${aws_instance.myinstance.public_ip} > publicip.txt"
}
}
resource "aws_s3_bucket" "myamanefsbucket" {
bucket = "myamanefsbucket"
acl = "public-read"
tags = {
Name = "myamanefsbucket"
}
}
locals {
s3_origin_id = "s3_origin"
}resource "aws_s3_bucket_object" "object"{
depends_on = [aws_s3_bucket.myamanefsbucket,null_resource.image]
bucket = aws_s3_bucket.myamanefsbucket.bucket
acl = "public-read"
key = "sample.jpg"
source = "C:/Users/user/Pictures/sample.jpg"

}
resource "aws_cloudfront_distribution" "cf_distribution" {
origin {
domain_name = aws_s3_bucket.myamanefsbucket.bucket_regional_domain_name
origin_id = local.s3_origin_id
}

enabled = true
default_root_object = "sample.jpg"default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.s3_origin_id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 10
max_ttl = 86400
}

restrictions {
geo_restriction {
restriction_type = "none"
}
}# SSL certificate for the service.
viewer_certificate {
cloudfront_default_certificate = true
}
}
resource "null_resource" "nullremote3" {
depends_on = [aws_efs_mount_target.EFS_mount,aws_instance.myinstance,]
connection {
type = "ssh"
user = "ec2-user"
private_key = file("C:/Users/user/Downloads/mykey.pem")
host = aws_instance.myinstance.public_ip
}

provisioner "remote-exec" {
inline = [
"sudo mount -t efs -o tls '${aws_efs_file_system.efs1.dns_name}':/ /var/www/html",
"sudo rm -rf /var/www/html/*",
"sudo git clone https://github.com/AmanGoyal31/multicloud.git /var/www/html/",
"sudo su << EOF",
"echo \"<img src=\"https://\"${aws_cloudfront_distribution.cf_distribution.domain_name}\"/sample.jpg\">\" >> /var/www/html/index.html",
"EOF",
"sudo systemctl restart httpd",
]
}
}
resource "null_resource" "nulllocal1" {
depends_on = [null_resource.nullremote3,]
provisioner "local-exec" {
command = "start chrome ${aws_instance.myinstance.public_ip}"
}
}
terraform init
terraform apply --auto-approve
terraform destroy --auto-approve

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store