Step-by-Step Guide: Deploying Flask Applications on AWS EC2 with Terraform

Step-by-Step Guide: Deploying Flask Applications on AWS EC2 with Terraform

As part of my learning journey in DevOps and cloud engineering, I explored how to deploy a Flask application on an AWS EC2 instance using Terraform. In this blog, I'll share what I learned, how I structured the project, and the steps I took to bring it all together.

Let’s dive in!


What I Set Out to Achieve

The goal was simple: deploy a Flask app on AWS while learning to create and manage resources like VPCs, subnets, and EC2 instances using Terraform. This hands-on exercise helped me deepen my understanding of Infrastructure as Code (IaC).


Architecture Overview

Here’s the basic architecture I worked on:

  1. A VPC with proper networking setup.

  2. A subnet, route table, and internet gateway to ensure connectivity.

  3. An EC2 instance to host the Flask app.

Setting Up with Terraform

1. Writing the main.tf File

I started by creating the main.tf file, which defines all the AWS resources. The key components I added were:

  • VPC: For an isolated network.

  • Subnet: For placing the EC2 instance within the VPC.

  • Route Table: To manage routing rules.

  • Internet Gateway: For internet access.

  • EC2 Instance: To deploy and run the Flask application.

Here’s what the configuration looked like:


2. Executing Terraform Commands

Once the configuration was ready, I executed the following commands:

  • Initialize Terraform

      terraform init
    

    This set up my working directory and downloaded all required plugins.

  • Validate the Configuration

      terraform validate
    

    I ran this to ensure there were no errors in my Terraform files.

  • Apply the Configuration

      terraform apply
    

    This command created all the resources. I loved seeing everything get provisioned automatically—just as defined in my file!


Resources I Created

Here’s a breakdown of the AWS resources I learned about and set up:

1. VPC

The VPC served as the foundation for networking.


2. Subnet

The subnet was created to ensure proper placement and connectivity of the EC2 instance within the VPC.


3. Route Table

I linked the route table with the subnet and added a rule for internet connectivity via the internet gateway.


4. Internet Gateway

The internet gateway allowed my EC2 instance to communicate with the internet.


5. EC2 Instance

This is where the magic happened! I configured an Ubuntu EC2 instance and used file provisioners to upload the Flask app (app.py) to the server.


Testing the Setup

Once everything was set up, I wanted to ensure it worked as expected.

Connecting to the EC2 Instance

Using SSH, I connected to the EC2 instance to verify the configurations:

ssh -i “~/.ssh/id_rsa” ubuntu@<EC2_PUBLIC_IP>

After connecting, I confirmed that the app.py file was correctly uploaded to the specified directory.


Accessing the Flask Application

Finally, I accessed the Flask app using the public IP of the EC2 instance.

It felt great to see the application live and running!


Cleaning Up

To avoid incurring any unnecessary costs, I destroyed all the resources using Terraform:

terraform destroy

This command safely removed everything I had provisioned. Watching the resources disappear was a reminder of how powerful Terraform is.


What I Learned

This hands-on experience was an exciting journey! Here are my key takeaways:

  1. Terraform makes IaC simple and efficient: Writing a few lines of code to manage complex cloud infrastructure is incredible.

  2. Understanding AWS networking: Setting up VPCs, subnets, and internet gateways gave me a deeper insight into AWS networking.

  3. Testing and debugging are crucial: Verifying each resource and connection ensured everything worked seamlessly.


Final Thoughts

Deploying a Flask app with Terraform was a rewarding experience. It taught me how to bring together different AWS services, structure IaC projects, and manage cloud resources efficiently.

If you’re on a similar learning journey, I highly recommend trying this out—it’s a fantastic way to gain hands-on experience!

Let me know your thoughts or questions in the comments. Happy building! 🚀