Contents

Between Flask, AWS Elastic Beanstalk, and .env

Contents

The Problem

One of multiple alternatives to store some credentials information in your flask application is by storing it on a .env file. If you installed python-dotenv package in your environment, you would find the .env is automatically loaded when you run flask command. There are many ways to deploy your flask app on the cloud, including using Elastic Beanstalk. However, this setup might cause you an error in your EBS deployment as the .env is not automatically loaded. Because on the Elastic Beanstalk, your Flask application is not running by using flask run command. So, even though you already installed python-dotenv the environment variables won’t be available. This will break your code as you are using the values from environment variables somewhere on your code.

The Solution

To be able to load your environment variables from .env file in your EBS environment, you need to do it on your code. python-dotenv has load_dotenv() method which enable your application to load from .env file. You can simply add it to your code.

1
2
3
from dotenv import load_dotenv

load_dotenv()