Compare commits
No commits in common. "1fec82de6c823afc71aa8018ef47a026da8b409d" and "16f6df29cca05db207b545b522412e050a37e5f6" have entirely different histories.
1fec82de6c
...
16f6df29cc
54
README.md
54
README.md
@ -1,17 +1,9 @@
|
|||||||
# TERRAFORM / GCP
|
# TERRAFORM / GCP
|
||||||
|
|
||||||
Demo Terraform déployant une application Flask dans un conteneur Docker sur GCP
|
|
||||||
|
|
||||||
## PRE REQUIS
|
## PRE REQUIS
|
||||||
|
|
||||||
- [COMPTE GCP](https://cloud.google.com/?hl=fr)
|
- [COMPTE GCP](https://cloud.google.com/?hl=fr)
|
||||||
- [TERRAFORM](https://www.terraform.io/)
|
- [TERRAFORM](https://www.terraform.io/)
|
||||||
|
|
||||||
## PREPARATION
|
|
||||||
|
|
||||||
### MACHINE LOCALE
|
|
||||||
|
|
||||||
- Installer Terraform:
|
|
||||||
```bash
|
```bash
|
||||||
# LINUX INSTALL
|
# LINUX INSTALL
|
||||||
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
|
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
|
||||||
@ -19,13 +11,7 @@ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(l
|
|||||||
sudo apt-get update && sudo apt-get install terraform
|
sudo apt-get update && sudo apt-get install terraform
|
||||||
```
|
```
|
||||||
|
|
||||||
- Créer une paire de clé ssh dédiée pour Terraform
|
## PREPARATION
|
||||||
```bash
|
|
||||||
# NOMMER LE CLE TERRAFORM
|
|
||||||
ssh-keygen -t rsa -b 4096
|
|
||||||
```
|
|
||||||
|
|
||||||
### DASHBOARD GCP
|
|
||||||
|
|
||||||
- Créer un projet "terraform-demo"
|
- 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
|
- 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
|
||||||
@ -37,28 +23,56 @@ ssh-keygen -t rsa -b 4096
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
- 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/
|
- 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
|
||||||
### TERRAFORM.TFVARS
|
|
||||||
|
|
||||||
- Modifier le fichier terraform.tfvars en fonction de votre environnement
|
|
||||||
|
|
||||||
## UTILISATION
|
## UTILISATION
|
||||||
|
|
||||||
- Lançer Terraform:
|
- Lançer Terraform:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
terraform init
|
terraform init
|
||||||
```
|
```
|
||||||
|
|
||||||
- Lançer Terraform:
|
- Lançer Terraform:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
terraform plan
|
terraform plan
|
||||||
```
|
```
|
||||||
|
|
||||||
- Lançer Terraform:
|
- Lançer Terraform:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
terraform apply
|
terraform apply
|
||||||
# Saisir yes quand demandé
|
# 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
|
> Visiter l'adresse fournie en output du terraform apply
|
@ -1,13 +0,0 @@
|
|||||||
FROM python:3.8-slim-bullseye
|
|
||||||
|
|
||||||
WORKDIR /python-docker
|
|
||||||
|
|
||||||
COPY ./requirements.txt requirements.txt
|
|
||||||
|
|
||||||
RUN python3 -m pip install -r requirements.txt
|
|
||||||
|
|
||||||
COPY ./ .
|
|
||||||
|
|
||||||
EXPOSE 5000
|
|
||||||
|
|
||||||
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
|
|
@ -1,8 +0,0 @@
|
|||||||
from flask import Flask
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def hello_cloud():
|
|
||||||
return 'Hello Cloud!'
|
|
||||||
|
|
||||||
app.run(host='0.0.0.0')
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
sudo apt-get update
|
|
||||||
sudo apt-get install -yq docker.io
|
|
||||||
sudo docker build --network=host -t flaskapp .
|
|
||||||
sudo docker run -d -p 5000:5000 flaskapp:latest
|
|
||||||
#python3 -m pip install flask
|
|
||||||
#python3 app.py >> log.txt 2>&1 &
|
|
||||||
#nohup python3 -u ./app.py > output.log &
|
|
@ -1 +0,0 @@
|
|||||||
flask
|
|
81
main.tf
81
main.tf
@ -24,6 +24,31 @@ resource "google_compute_subnetwork" "default" {
|
|||||||
network = google_compute_network.vpc_network.id
|
network = google_compute_network.vpc_network.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# VM
|
||||||
|
resource "google_compute_instance" "default" {
|
||||||
|
name = "flask-vm"
|
||||||
|
machine_type = "e2-micro"
|
||||||
|
zone = var.gcp_zone
|
||||||
|
tags = ["ssh"]
|
||||||
|
|
||||||
|
boot_disk {
|
||||||
|
initialize_params {
|
||||||
|
image = "debian-cloud/debian-11"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
## INSTALL FLASK
|
||||||
|
metadata_startup_script = "sudo apt-get update; sudo apt-get install -yq build-essential python3-pip rsync; pip install flask"
|
||||||
|
|
||||||
|
network_interface {
|
||||||
|
subnetwork = google_compute_subnetwork.default.id
|
||||||
|
|
||||||
|
access_config {
|
||||||
|
# Include this section to give the VM an external IP address
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
## FIREWALL
|
## FIREWALL
|
||||||
### SSH
|
### SSH
|
||||||
resource "google_compute_firewall" "ssh" {
|
resource "google_compute_firewall" "ssh" {
|
||||||
@ -51,62 +76,6 @@ resource "google_compute_firewall" "flask" {
|
|||||||
source_ranges = ["0.0.0.0/0"]
|
source_ranges = ["0.0.0.0/0"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
## VM
|
|
||||||
resource "google_compute_instance" "default" {
|
|
||||||
name = "flask-vm"
|
|
||||||
machine_type = "e2-micro"
|
|
||||||
zone = var.gcp_zone
|
|
||||||
tags = ["ssh"]
|
|
||||||
|
|
||||||
boot_disk {
|
|
||||||
initialize_params {
|
|
||||||
image = "debian-cloud/debian-11"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata = {
|
|
||||||
ssh-keys = "${var.gcp_ssh_user}:${file(var.gcp_ssh_pub_key_file)}"
|
|
||||||
}
|
|
||||||
|
|
||||||
network_interface {
|
|
||||||
subnetwork = google_compute_subnetwork.default.id
|
|
||||||
|
|
||||||
access_config {
|
|
||||||
#### TO HAVE A PUBLIC IP ADRESS
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
### COPY DES FICHIERS
|
|
||||||
provisioner "file" {
|
|
||||||
source = "app/"
|
|
||||||
destination = "./"
|
|
||||||
connection {
|
|
||||||
host = google_compute_instance.default.network_interface.0.access_config.0.nat_ip
|
|
||||||
type = "ssh"
|
|
||||||
user = var.gcp_ssh_user
|
|
||||||
timeout = "500s"
|
|
||||||
private_key = "${file(var.gcp_ssh_priv_key_file)}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
### START DEPLOY.SH
|
|
||||||
provisioner "remote-exec" {
|
|
||||||
inline = [
|
|
||||||
"chmod +x ~/deploy.sh",
|
|
||||||
"cd ~",
|
|
||||||
"./deploy.sh"
|
|
||||||
]
|
|
||||||
connection {
|
|
||||||
host = google_compute_instance.default.network_interface.0.access_config.0.nat_ip
|
|
||||||
type = "ssh"
|
|
||||||
user = var.gcp_ssh_user
|
|
||||||
timeout = "500s"
|
|
||||||
private_key = "${file(var.gcp_ssh_priv_key_file)}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
## GET VM PUBLIC IP
|
## GET VM PUBLIC IP
|
||||||
output "Web-server-URL" {
|
output "Web-server-URL" {
|
||||||
value = join("",["http://",google_compute_instance.default.network_interface.0.access_config.0.nat_ip,":5000"])
|
value = join("",["http://",google_compute_instance.default.network_interface.0.access_config.0.nat_ip,":5000"])
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
# GCP Settings
|
# GCP Settings
|
||||||
gcp_project_id = "terraform-demo-381114" # A MODIFIER
|
gcp_project_id = "terraform-demo-381114"
|
||||||
gcp_region = "europe-west9"
|
gcp_region = "europe-west9"
|
||||||
gcp_zone = "europe-west9-a"
|
gcp_zone = "europe-west9-a"
|
||||||
gcp_auth_file = "./auth/terraform-demo-381114-158cfce10778.json" # A MODIFIER
|
gcp_auth_file = "./auth/terraform-demo-381114-158cfce10778.json"
|
||||||
gcp_ssh_user = "devops" # A MODIFIER OU PAS
|
|
||||||
gcp_ssh_pub_key_file = "~/.ssh/terraform.pub"
|
|
||||||
gcp_ssh_priv_key_file = "~/.ssh/terraform"
|
|
15
variables.tf
15
variables.tf
@ -20,19 +20,4 @@ variable "gcp_project_id" {
|
|||||||
variable "gcp_zone" {
|
variable "gcp_zone" {
|
||||||
type = string
|
type = string
|
||||||
description = "GCP zone"
|
description = "GCP zone"
|
||||||
}
|
|
||||||
|
|
||||||
variable "gcp_ssh_user" {
|
|
||||||
type = string
|
|
||||||
description = "SSH user"
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "gcp_ssh_pub_key_file" {
|
|
||||||
type = string
|
|
||||||
description = "SSH public key file path"
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "gcp_ssh_priv_key_file" {
|
|
||||||
type = string
|
|
||||||
description = "SSH private key file path"
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user