This example shows how to build and deploy a simple Python application using AWS Elastic Beanstalk for managed hosting and Terraform for infrastructure as code. The deploy.sh script automates the application packaging and deployment process.
Project Structure
Here’s a quick overview of the project’s key components:
app/: Contains the Python application,main.py, along withProcfilefor defining the web server, andrequirements.txtfor Python dependencies.terraform/: Holds all the Terraform configuration files (.tf) that define the AWS infrastructure:main.tf: Defines the core AWS resources like the S3 bucket for application versions, IAM roles for Elastic Beanstalk, and the Elastic Beanstalk application and environment themselves.variables.tf: Declares input variables for customizability (e.g., AWS region, instance type).outputs.tf: Exports important values like the S3 bucket name and the Elastic Beanstalk environment URL.
deploy.sh: A shell script that orchestrates the deployment process:- Reads outputs from Terraform to get dynamic values.
- Zips the
app/directory. - Uploads the zipped application to the designated S3 bucket.
- Creates a new Elastic Beanstalk application version.
- Updates the Elastic Beanstalk environment to use the new version.
How It Works
First, Terraform provisions all necessary AWS resources for Elastic Beanstalk. Then, the deploy.sh script automates the rest: it fetches deployment details from Terraform, zips your Python application, uploads it to S3, and finally, updates your Elastic Beanstalk environment with this new application version.
This setup provides a robust and automated way to deploy and manage your Python applications on AWS, leveraging the power of Elastic Beanstalk for ease of use and Terraform for infrastructure as code best practices.
Project Repository: https://github.com/OmarMakled/terraform-eb-python