This repository contains Terraform code and instructions for deploying a sample Python application on an AWS EC2 instance.
- Overview
- Prerequisites
- Project Structure
- Terraform Configuration
- Application Deployment
- Accessing the Application
- Cleaning Up
The Terraform code in this repository automates the creation of an AWS EC2 instance and deploys a sample Python application on it. This README provides guidance on setting up and deploying the application.
Before you begin, ensure you have the following prerequisites installed and configured:
- Terraform CLI
- AWS CLI with appropriate access credentials
- AWS account
The project structure is as follows:
terraform-ec2-python/
├── main.tf
├── variables.tf
├── terraform.tfvars
├── provisioners.md
├── app.py
└── README.md
main.tf
: Terraform configuration for creating EC2 instance, security group, VPC, subnet, route table, Internet Gatway and key-pair.variables.tf
: Input variables used in Terraform configuration.terraform.tfvars
: Specifies values for input variables.app/
: Sample Python application directory.README.md
: Instructions and documentation (you're reading it now).provisioners.md
: Types of provisioners
Modify the variables.tf
and terraform.tfvars
files to customize your EC2 instance configuration. Key variables include:
region
: AWS region where the EC2 instance will be created.instance_type
: Type of EC2 instance to launch (e.g., t2.micro).key_name
: Name of the SSH key pair used to access the instance.ami
: ID of the Amazon Machine Image (AMI) to use for the EC2 instance.security_group_name
: Name of the security group allowing inbound traffic on port 80 (HTTP).
-
Clone this repository to your local machine:
git clone https://github.com/your/repository.git
-
Navigate to the cloned repository:
cd terraform-provisioners/
-
Initialize Terraform:
terraform init
-
Review and modify the Terraform configuration files (
main.tf
,variables.tf
,terraform.tfvars
) as needed. -
Apply the Terraform configuration to create the EC2 instance:
terraform apply
-
Once the EC2 instance is created, the sample Python application will be automatically deployed on the instance.
To access the deployed Python application:
-
Note the public IP address or DNS name of the EC2 instance from the Terraform output.
-
Open a web browser and enter the following URL:
http://<EC2_Public_IP_or_DNS>
Replace
<EC2_Public_IP_or_DNS>
with the actual public IP address or DNS name of your EC2 instance. -
You should see the sample Python application running and displaying a welcome message.
To clean up and destroy the resources created by Terraform:
-
Navigate to the project directory.
-
Run the following command to destroy the resources:
terraform destroy
Confirm the action when prompted.