Terraform with AWS(including EFS instead of EBS) Full Automation.

AWS

Terraform

Problem statement:

Create/launch Application using Terraform

provider "aws" {
region = "ap-south-1"
profile = "testing"
}
resource "aws_security_group" "sc1" {    
name = "sc1"
description = "Allows SSH and HTTP"
vpc_id = "vpc-720b141a"
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
ingress {
description = "HTTP"
from_port = 80
to_port = 80
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 = "sc1"
}
}
resource "aws_efs_file_system" "myefs"{   
creation_token="my-efs"
tags = {
Name= "myefs"
}
}
resource "aws_efs_mount_target" "first" {
file_system_id = aws_efs_file_system.myefs.id
subnet_id = "subnet-7761651f"
security_groups= [aws_security_group.sc1.id]
}
resource "aws_instance" "myos1" {    
ami = "ami-0732b62d310b80e97"
instance_type = "t2.micro"
key_name = "mykeys"
security_groups = [aws_security_group.sc1.id]
subnet_id = "subnet-7761651f"
associate_public_ip_address = "1"
connection {
type = "ssh"
user = "ec2-user"
private_key = file("C:/Users/dell/Downloads/mykeys.pem")
host = aws_instance.myos1.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo yum install httpd php git -y",
"sudo systemctl restart httpd",
"sudo systemctl enable httpd",
]
}
tags = {
Name = "myos1"
}
}
resource "aws_s3_bucket" "abhi85fortask" {    
bucket = "abhi85fortask"
acl = "public-read"
versioning {
enabled = true
}
tags = {
Name = "abhi85fortask"
Environment = "Dev"
}
}
resource "aws_s3_bucket_object" "s3obj" {
depends_on = [
aws_s3_bucket.abhi85fortask,
]
bucket = "abhi85fortask"
key = "original.jpg"
source = "C:/Users/Dell/Desktop/original.jpg"
acl = "public-read"
content_type = "image or jpeg"
}
resource "aws_cloudfront_distribution" "abhiCF" {      
origin {
domain_name = "abhi85fortask.s3.amazonaws.com"
origin_id = "S3-abhi85fortask"
custom_origin_config {
http_port = 80
https_port = 80
origin_protocol_policy = "match-viewer"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
}
enabled = true
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3-abhi85fortask"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
}

OUTPUTS

For applying full automation we should run this command

Thank you…..