Skip to content

Commit

Permalink
chore: Update EC2 provisioning and Ansible playbook execution
Browse files Browse the repository at this point in the history
  • Loading branch information
nilgaar committed Jul 13, 2024
1 parent 0e99df0 commit b822813
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions IaC/aws/terraform/ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,30 @@ resource "aws_instance" "ec2example" {
associate_public_ip_address = true
key_name = aws_key_pair.ec2_key_pair.key_name

}

resource "null_resource" "wait_for_instance" {
depends_on = [aws_instance.ec2example]

provisioner "local-exec" {
command = "ansible-playbook -i '${self.public_ip},' ${path.module}/../../ansible/apache.yml"
command = <<EOF
while [ "$(aws ec2 describe-instances --instance-ids ${aws_instance.ec2example.id} --query 'Reservations[].Instances[].State.Name' --output text)" != "running" ]; do
echo "Waiting for instance to be in running state"
sleep 10
done
echo "Instance is running, proceeding to check system status checks..."
while [ "$(aws ec2 describe-instance-status --instance-ids ${aws_instance.ec2example.id} --query 'InstanceStatuses[].SystemStatus.Status' --output text)" != "ok" ]; do
echo "Waiting for system status check to pass"
sleep 10
done
EOF
}
}

resource "null_resource" "ansible_provisioning" {
depends_on = [null_resource.wait_for_instance]

provisioner "remote-exec" {
inline = [
"echo 'Waiting for instance to be ready'"
]
provisioner "local-exec" {
command = "ansible-playbook -i '${aws_instance.ec2example.public_ip},' ${path.module}/../../ansible/apache.yml"
}
}

0 comments on commit b822813

Please sign in to comment.