Integration of AWS and Terraform

Task : Have to create/launch Application using Terraform

Step1: configured my AWS profile in local system using Command Prompt. Filled details then Enter.

aws configure --profile sahana
AWS Access Key ID [****************5KMO]:
AWS Secret Access Key [****************VpMS]:
Default region name [ap-south-1]:
Default output format [None]:

Step 2:

provider  "aws" {
region = "ap-south-1"
profile = "sahana"
}
resource "aws_instance" "myin" {
ami = "ami-0447a12f28fddb066"
instance_type = "t2.micro"
key_name = "sahana12345"
security_groups = [ "launch-wizard-2" ]
connection {
type = "ssh"
user = "ec2-user"
private_key = file("C:/Users/sahanab/Downloads/sahana12345.pem")
host = aws_instance.myin.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo yum install httpd php git -y",
"sudo systemctl restart httpd",
"sudo systemctl enable httpd",
"sudo setenforce 0"
]
}
tags = {
Name = "sahanaos"
}
}
resource "aws_ebs_volume" "sahanavol" {
availability_zone = aws_instance.myin.availability_zone
size = 1
tags = {
Name = "sahanaebs"
}
}
resource "aws_volume_attachment"  "ebs_att" {
device_name = "/dev/sdd"
volume_id = "${aws_ebs_volume.ashuvol.id}"
instance_id = "${aws_instance.myin.id}"
force_detach = true
}
resource "null_resource" "public_ip"  {
provisioner "local-exec" {
command = "echo ${aws_instance.myin.public_ip} > public_ip.txt"
}
}
resource "null_resource" "mount"  {    depends_on = [
aws_volume_attachment.ebsatt,
]
connection {
type = "ssh"
user = "ec2-user"
private_key = file("C:/Users/sahanab/Downloads/sahana12345.pem")
host = aws_instance.myin.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo mkfs.ext4 /dev/xvdd",
"sudo mount /dev/xvdd /var/www/html",
"sudo rm -rf /var/www/html/*",
"sudo git clone https://github.com/sahanabalappa/cloudtask1 /var/www/html/"
]
}
}
resource "null_resource" "git_copy"  {
provisioner "local-exec" {
command = "git clone https://github.com/sahanabalappa/Integration_Of_Terraform_AND_AWS C:/Users/sahana/Pictures/"
}
}
resource "aws_s3_bucket" "sahanabkt" {
bucket = "sahana123"
acl = "private"
tags = {
Name = "sahana1234"
}
}
locals {
s3_origin_id = "myS3Origin"
}
resource "aws_s3_bucket_object" "object" {
bucket = "${aws_s3_bucket.sahanabkt.id}"
key = "test_pic"
source = "C:/Users/sahana/Pictures/img1.jpg"
acl = "public-read"
}
resource "aws_cloudfront_distribution" "sahanafnt" {
origin {
domain_name = "${aws_s3_bucket.sahanabkt.bucket_regional_domain_name}"
origin_id = "${local.s3_origin_id}"
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 = "${local.s3_origin_id}"
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
}
}
resource "null_resource" "local_exec"  {
depends_on = [
null_resource.mount,
]
provisioner "local-exec" {
command = "start chrome ${aws_instance.myin.public_ip}"
}
}

DevOps || Cloud || ML ||Coding Enthusiast