Build the following architecture:

Set up S3 buckets to store the Terraform state files and the image used by the web servers.
The Terraform state is stored in a bucket called acs730-final-nh in this project. You can use any globally unique name and update the value in the backend.tf files located under staging/aws_network and prod/aws_network. The buckets are already set in a way that the remote file of different environments is saved under different folders.
Another bucket named ansibleprojectassets has been created for this project. The ansible_terraform.jpeg image located under the assets folder has been uploaded to that bucket. This image will be displayed on the web servers deployed by Ansible. You can upload your own image to your own bucket and ensure you have access to it.
Make sure you have Terraform installed. Refer to this documentation based on the environment you are using.
git clone https://github.com/haingo-raz/terraform-git-automation.gitAnd navigate to the root directory.
- Navigate to the
staging/aws_networkdirectory. - Initialize Terraform with the command:
terraform init. - Deploy the network infrastructure by running:
terraform apply.
- Navigate to the
staging/aws_webserversdirectory. - Delete the
nh.pubfrom thestaging/aws_webserversdirectory because you will create your own keypair. - Create a key pair named
nhin theaws_webserversfolder using the command:ssh-keygen -t rsa -f nh. - Initialize Terraform with the command:
terraform init. - Deploy the web server infrastructure by running:
terraform apply.
- Install ansible using the command:
sudo yum install -y ansible - Install Python and Required Libraries (boto3, botocore)
First, make sure pip3 is installed:sudo yum install -y python3-pip
Then, install the required Python libraries:pip3 install boto3 botocore
Install the Amazon AWS Ansible collection using:ansible-galaxy collection install amazon.aws
- Make sure you have access to the keypair you created when deploying the terraform infrastructure.
- Change the path of the inventory file in the
ansible.cfgfile according to your file organization. - Once everything is installed, navigate in the
ansibledirectory and run the playbook to set up the web server:ansible-playbook playbooks/webserver_setup.yml
The GitHub Actions workflow will automatically deploy your infrastructure whenever changes are pushed/merged to the prod branch.
- Go to the repository's Settings > Environments
- Create two environments
prodandstaging. - Add the following Environment secrets in each environment:
AWS_ACCESS_KEY_ID="your-access-key-id"
AWS_SECRET_ACCESS_KEY="your-secret-access-key"
AWS_SESSION_TOKEN="your-session-token"
These credentials will allow GitHub Actions to interact with AWS during the deployment process.
- Create Pull Request from Staging to prod branch. After merging changes to the
prodfromstagingbranch, the GitHub Actions workflow will automatically:
Initialize Terraform: Run terraform init in both aws_network and aws_webservers directories.
Plan Changes: Run terraform plan to preview the changes.
Apply Changes: Run terraform apply to deploy the infrastructure to AWS.
Changes to the staging/aws_network and staging/aws_webservers directories will trigger this workflow.
Direct pushes to prod are prohibited to ensure all changes are thoroughly reviewed and tested through GitHub Actions workflows before deployment.
You can monitor the progress of the GitHub Actions workflow directly in the Actions tab of the GitHub repository.
- Navigate to the
staging/aws_webserversdirectory. - Destroy the infrastructure with command:
terraform destroy -auto-approve. - Navigate to the
staging/aws_networkdirectory. - Destroy the infrastructure with command:
terraform destroy -auto-approve.
- Delete the S3 bucket used to store the Terraform remote state files.
- Delete the S3 bucket containing the image displayed on the web servers if relevant.