Simple Terraform demo for GCP to deploy a Flask app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
terraform-demo/README.md

78 lines
1.8 KiB

1 year ago
# TERRAFORM / GCP
## PRE REQUIS
- [COMPTE GCP](https://cloud.google.com/?hl=fr)
- [TERRAFORM](https://www.terraform.io/)
```bash
# LINUX INSTALL
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
```
## PREPARATION
- Créer un projet "terraform-demo"
- Séletionner le projet créé puis créer un compte de service dans l'onglet [IAM et administration](https://console.cloud.google.com/projectselector2/iam-admin/serviceaccounts?hl=fr) de la console GCP
- Ajouter les rôles suivant:
- Compute admin
- Compute network admin
- Service account admin
- Activer les APIs nécessaires (compute engine API, storage API, cloud billing API)
![SERVICES ACCOUNT](docs/service-account.png)
- Créer une clé au sein du compte de service avec les droits sur le compute engine (pour créer les VMs), et télécharger le fichier json contenant la clé pour le mettre dans le dossier auth
- Modifier l'id du projet ainsi que le chemin du fichier json contenant la clef dans le fichier variables.tf
1 year ago
## UTILISATION
- Lançer Terraform:
```bash
terraform init
```
- Lançer Terraform:
```bash
terraform plan
```
- Lançer Terraform:
```bash
terraform apply
# Saisir yes quand demandé
```
> L'adresse public de la VM est fournie en output du terraform apply http://public-ip:5000
- Se connecter en SSH à la VM pour créer l'application Flask:
```bash
nano app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_cloud():
return 'Hello Cloud!'
app.run(host='0.0.0.0')
```
- Installer Flask:
```bash
python3 -m pip install flask
```
- Lançer l'application:
```bash
python3 app.py
```
> Visiter l'adresse fournie en output du terraform apply