Skip to content

Instantly share code, notes, and snippets.

@rohitg00
Last active July 12, 2023 03:57
Show Gist options
  • Save rohitg00/5e130470885175fb93550be6db9cb7c0 to your computer and use it in GitHub Desktop.
Save rohitg00/5e130470885175fb93550be6db9cb7c0 to your computer and use it in GitHub Desktop.
resource "null_resource" "remote1" {
depends_on = [ aws_instance.web, ]
//Executing Commands to initiate WebServer in Instance Over SSH
provisioner "remote-exec" {
connection {
agent = "false"
type = "ssh"
user = "ec2-user"
private_key = "${tls_private_key.tls_key.private_key_pem}"
host = "${aws_instance.web.public_ip}"
}
inline = [
"sudo yum install httpd git -y",
"sudo systemctl start httpd",
"sudo systemctl enable httpd",
]
}
}
//Creating EBS Volume
resource "aws_ebs_volume" "web-vol" {
availability_zone = "${aws_instance.web.availability_zone}"
size = 1
tags = {
Name = "ebs-vol"
}
}
//Attaching EBS Volume to a Instance
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.web-vol.id}"
instance_id = "${aws_instance.web.id}"
force_detach = true
provisioner "remote-exec" {
connection {
agent = "false"
type = "ssh"
user = "ec2-user"
private_key = "${tls_private_key.tls_key.private_key_pem}"
host = "${aws_instance.web.public_ip}"
}
inline = [
"sudo mkfs.ext4 /dev/xvdh",
"sudo mount /dev/xvdh /var/www/html/",
"sudo rm -rf /var/www/html/*",
"sudo git clone https://github.com/rohitg00/Terraform-Build-AWS.git /var/www/html/",
]
}
depends_on = [
aws_instance.web,
aws_ebs_volume.web-vol
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment