-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
johnbedeir
committed
Jan 4, 2024
1 parent
1dd3e69
commit c6114a3
Showing
12 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# This file specifies files that are _not_ uploaded to Google Cloud | ||
|
||
# using gcloud. It follows the same syntax as .gitignore, with the addition of | ||
|
||
# "#!include" directives (which insert the entries of the given .gitignore-style | ||
|
||
# file at that point). | ||
|
||
# | ||
|
||
# For more information, run: | ||
|
||
# $ gcloud topic gcloudignore | ||
|
||
# | ||
|
||
.gcloudignore | ||
|
||
# If you would like to upload your .git directory, .gitignore file or files | ||
|
||
# from your .gitignore file, remove the corresponding line | ||
|
||
# below: | ||
|
||
.git | ||
.gitignore | ||
Dockerfile | ||
.history | ||
venv | ||
|
||
# Python pycache: | ||
|
||
**pycache**/ | ||
|
||
# Ignored by the build system | ||
|
||
/setup.cfg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Deploying Flask App on GCP App Engine | ||
|
||
<img src=cover.png> | ||
|
||
This guide will walk you through the steps to deploy a Flask application, like the provided `main.py`, on Google Cloud Platform's App Engine. | ||
|
||
## Prerequisites | ||
|
||
- A Google Cloud Platform account. | ||
- Google Cloud SDK installed on your local machine. | ||
|
||
## Getting Started | ||
|
||
1. **Rename Your Application File:** | ||
Ensure that your Flask application file is named `main.py`. | ||
|
||
2. **App Engine Configuration File:** | ||
Create an `app.yaml` file in the same directory as your `main.py`. This file is used to configure your App Engine environment. | ||
|
||
3. **Update Cloud SDK:** | ||
Run the following command to update all the installed components of the Google Cloud SDK to the latest version: | ||
|
||
``` | ||
gcloud components update | ||
``` | ||
|
||
4. **Create a GCP Project:** | ||
If you haven't already, create a new project on Google Cloud Platform. Note down the `PROJECT_ID`. | ||
|
||
5. **Enable App Engine Admin API:** | ||
Enable the App Engine Admin API for your project. This is required for deploying applications. | ||
|
||
6. **Authenticate Your Account:** | ||
Authenticate your GCP account with the following command: | ||
|
||
``` | ||
gcloud auth login | ||
``` | ||
|
||
7. **Set Project ID:** | ||
Configure `gcloud` to use your project with the following command: | ||
|
||
``` | ||
gcloud config set project PROJECT_ID | ||
``` | ||
|
||
Replace `PROJECT_ID` with your actual GCP project ID. | ||
|
||
8. **Deploy the Application:** | ||
Navigate to your application directory where `main.py` and `app.yaml` are located. Run the following command to deploy your application: | ||
|
||
``` | ||
gcloud app deploy | ||
``` | ||
|
||
Follow the on-screen prompts to complete the deployment. | ||
|
||
9. **Access Your Application:** | ||
Once the deployment is successful, you can access your application using the following command: | ||
``` | ||
gcloud app browse | ||
``` | ||
|
||
## Additional Information | ||
|
||
- Make sure you are in the correct directory where your `main.py` and `app.yaml` files are located before running the deployment commands. | ||
- For more detailed information and troubleshooting, refer to the [Google Cloud App Engine Documentation](https://cloud.google.com/appengine/docs). | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
runtime: python39 | ||
|
||
handlers: | ||
- url: /.* | ||
script: auto |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import os | ||
from flask import Flask | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello, World! This is a Flask app running on GCP App Engine.' | ||
|
||
if __name__ == '__main__': | ||
port = int(os.environ.get('PORT', 8080)) | ||
app.run(host='0.0.0.0', port=port, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Flask==2.0.1 | ||
Werkzeug==2.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.history | ||
Dockerfile | ||
.gcloudignore | ||
.gitignore | ||
&.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM python:3.9 | ||
|
||
WORKDIR /usr/src/app | ||
|
||
COPY . . | ||
|
||
RUN pip install --upgrade pip | ||
|
||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
EXPOSE 8080 | ||
|
||
CMD ["python3", "./main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
## Deploying Flask App on Google Cloud Run | ||
|
||
<img src=cover.png> | ||
|
||
### Prerequisites | ||
|
||
- A Google Cloud Platform account. | ||
- Google Cloud SDK installed on your local machine. | ||
- Docker installed on your local machine (for building the container image). | ||
|
||
### Steps | ||
|
||
1. **Prepare Your Application:** | ||
|
||
- Ensure your Flask application is in a file named `main.py`. | ||
- Create a `requirements.txt` file in the same directory as your `main.py`, listing all the dependencies. | ||
|
||
2. **Containerize Your Application:** | ||
|
||
- Create a `Dockerfile` in the same directory as your `main.py`. A basic example of a Dockerfile for a Flask application is: | ||
|
||
3. **Build Your Container Image:** | ||
|
||
- Build the Docker image and tag it for uploading: | ||
``` | ||
docker build -t gcr.io/PROJECT_ID/my-flask-app . | ||
``` | ||
- Replace `PROJECT_ID` with your actual GCP project ID. | ||
4. **Push Your Container Image to Container Registry:** | ||
- Push the image to Google Container Registry: | ||
``` | ||
docker push gcr.io/PROJECT_ID/my-flask-app | ||
``` | ||
5. **Deploy on Cloud Run:** | ||
- Ensure you have set your GCP project ID: | ||
``` | ||
gcloud config set project PROJECT_ID | ||
``` | ||
- Deploy the image to Cloud Run: | ||
``` | ||
gcloud run deploy --image gcr.io/PROJECT_ID/my-flask-app --platform managed | ||
``` | ||
- Follow the prompts to specify the service name and region. Allow unauthenticated invocations if needed. | ||
6. **Access Your Application:** | ||
- Once the deployment is successful, Cloud Run will provide a URL to access your application. | ||
To remove or delete the deployment of your application from Google Cloud Run, you can follow these steps: | ||
### Delete the Cloud Run Service | ||
First, you'll need to delete the specific Cloud Run service where your application is deployed. You can do this using the Google Cloud Console or the `gcloud` command-line tool. | ||
Using `gcloud`, the command to delete a service is: | ||
```sh | ||
gcloud run services delete SERVICE_NAME --platform managed | ||
``` | ||
|
||
Replace `SERVICE_NAME` with the name of the service you want to delete. | ||
|
||
This command will prompt you to confirm the deletion. Once confirmed, it will remove the service and stop serving your application. | ||
|
||
### Remove the Container Image | ||
|
||
If you also want to remove the container image from the Google Container Registry, you can do so through the Google Cloud Console or using `gcloud`: | ||
|
||
```sh | ||
gcloud container images delete gcr.io/PROJECT_ID/my-flask-app --force-delete-tags | ||
``` | ||
|
||
Replace `PROJECT_ID` with your actual GCP project ID and `my-flask-app` with the name of your Docker image. | ||
|
||
This command will delete the container image and its tags from your Container Registry. | ||
|
||
### Delete the GCR Repository | ||
|
||
``` | ||
gcloud container images list --repository=gcr.io/PROJECT_ID | ||
``` | ||
|
||
### Notes | ||
|
||
- Make sure you replace `PROJECT_ID` with your actual Google Cloud project ID in the commands. | ||
- The first deployment might take a bit longer as it involves setting up the Cloud Run service. | ||
- For more detailed information, refer to the [Google Cloud Run Documentation](https://cloud.google.com/run/docs). |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import os | ||
from flask import Flask | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello, World! This is a Flask app running on GCP Cloud Run.' | ||
|
||
if __name__ == '__main__': | ||
port = int(os.environ.get('PORT', 8080)) | ||
app.run(host='0.0.0.0', port=port, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Flask==2.0.1 | ||
Werkzeug==2.0.1 |