diff --git a/.github/workflows/deploy_aws_vpc.yml b/.github/workflows/deploy_aws_vpc.yml index 08fadf3..8f52119 100644 --- a/.github/workflows/deploy_aws_vpc.yml +++ b/.github/workflows/deploy_aws_vpc.yml @@ -21,6 +21,11 @@ jobs: - uses: opentofu/setup-opentofu@v1.0.3 name: Setup OpenTofu + - name: Setup SSH Key using webfactory/ssh-agent + uses: webfactory/ssh-agent@v0.5.4 + with: + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} + - name: OpenTofu Setup and Validate run: | cd ./IaC/aws/terraform/ @@ -33,3 +38,8 @@ jobs: run: | cd ./IaC/aws/terraform/ tofu apply -auto-approve -var="AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}" -var="AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}" + + - name: Destroy VPC + run: | + cd ./IaC/aws/terraform/ + tofu destroy -auto-approve -var="AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}" -var="AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}" diff --git a/IaC/ansible/apache.yml b/IaC/ansible/apache.yml index 1728955..0428125 100644 --- a/IaC/ansible/apache.yml +++ b/IaC/ansible/apache.yml @@ -1,3 +1,4 @@ +--- - name: Install and start Apache hosts: all become: yes diff --git a/IaC/aws/terraform/ec2.tf b/IaC/aws/terraform/ec2.tf index a11af3d..1c38e08 100644 --- a/IaC/aws/terraform/ec2.tf +++ b/IaC/aws/terraform/ec2.tf @@ -11,13 +11,20 @@ resource "aws_instance" "ec2example" { associate_public_ip_address = true key_name = aws_key_pair.ec2_key_pair.key_name - provisioner "local-exec" { - command = "ansible-playbook -i '${self.public_ip},' --private-key ${path.module}/ ansible/playbook.yml" - } - provisioner "remote-exec" { inline = [ - "echo 'Waiting for instance to be ready'" + "echo 'Waiting for instance to be fully ready...'", + "sleep 20" ] } + + provisioner "local-exec" { + command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i '${self.public_ip},' ${path.module}/../../ansible/apache.yml -u 'ubuntu'" + } + + connection { + type = "ssh" + user = "ubuntu" + host = self.public_ip + } }