This repository contains a Django Python application that demonstrates how to provision an EC2 instance on Amazon Web Services (AWS) using Infrastructure as Code (IaC) principles. The process involves utilizing Terraform for infrastructure provisioning and Ansible for configuration management. By following this guide, you will set up an EC2 instance programmatically, showcasing the power of IaC in maintaining consistent and scalable infrastructure.
Before you begin, ensure you have the following prerequisites in place:
- AWS Account: Sign up for an AWS account if you don't have one.
- Terraform: Install Terraform by following the official installation guide.
- Ansible: Install Ansible by following the official installation guide.
- Python: Install Python and Django framework. Use
pip install django
.
Follow these steps to provision an EC2 instance on AWS using Terraform and Ansible:
git clone https://github.com/eduardoraider/terraform-ansible-ec2-demo.git
cd terraform-ansible-ec2-demo
Ensure your AWS credentials are properly configured either through environment variables or the ~/.aws/credentials
file.
chmod 400 key.pem
Modify the main.tf
file to suit your configuration preferences, such as the AWS region, instance type, etc.
terraform init
terraform apply
Terraform will provision the specified infrastructure components on AWS. Check if the instance is running by accessing it in your browser using http://public-ip-address:8080
note: Make sure to replace
public-ip-address
with the actual public IP address of your EC2 instance.
Update the hosts.yml
file with the public IP address of the EC2 instance created by Terraform.
ansible-playbook playbook.yml -u ubuntu --private-key key.pem -i hosts.yml
The Ansible playbook will connect to the EC2 instance and apply configuration settings.
You can now SSH into your EC2 instance using the provided public IP address.
ssh -i /path/to/your/private/key.pem ec2-user@public-ip-address
Navigate to the backend
directory and start the Django development server:
cd backend
. venv/bin/activate
python manage.py runserver 0.0.0.0:8000
Access the Django application in your browser at http://public-ip-address:8000
.
note: Make sure to replace
public-ip-address
with the actual public IP address of your EC2 instance.
To prevent unnecessary charges, remove the resources created:
terraform destroy
This project is licensed under the MIT License. See the LICENSE file for details.
🛠 🥋 Software Engineer