Outputs
Mostrar información resultante de la infra
Creemos una ip publica y la usemos en la instancia de GCE
output "public_ip" {
description = "Dirección IP pública de la instancia GCP"
value = google_compute_instance.nginx-server.network_interface[0].access_config[0].nat_ip
}
-
Archivo terraform final
resource "google_compute_instance" "nginx-server" {
project = "sils-keeper-infra"
name = "nginx-server"
machine_type = "n1-standard-1"
zone = "us-central1-a"
tags = ["nginx", "http-server"]
labels = {
environment = "test"
owner = "julian_sanchez"
team = "sre"
project = "sils-keeper-infra"
}
boot_disk {
initialize_params {
image = "ubuntu-minimal-2210-kinetic-amd64-v20230126"
}
}
network_interface {
network = "default"
access_config {}
}
metadata_startup_script = <<EOF
#!/bin/bash
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
sudo apt update
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
EOF
metadata = {
ssh-keys = "nginx-server-ssh: ${file("nginx-server.key.pub")}"
}
}
output "public_ip" {
description = "Dirección IP pública de la instancia GCP"
value = google_compute_instance.nginx-server.network_interface[0].access_config[0].nat_ip
}
resource "google_compute_firewall" "ssh" {
name = "allow-ssh"
project = "sils-keeper-infra"
description = "Allow SSH for DevOps team - env: test, owner: julian sanchez"
allow {
ports = ["22"]
protocol = "tcp"
}
direction = "INGRESS"
network = "default"
priority = 1000
source_ranges = ["0.0.0.0/0"]
target_tags = ["nginx"]
}
resource "google_compute_firewall" "http" {
name = "allow-http"
project = "sils-keeper-infra"
description = "Allow HTTP for DevOps team - env: test, owner: julian sanchez"
allow {
ports = ["80"]
protocol = "tcp"
}
direction = "INGRESS"
network = "default"
priority = 1000
source_ranges = ["0.0.0.0/0"]
target_tags = ["nginx"]
}
resource "google_compute_firewall" "nginx-egress-all" {
name = "nginx-egress-all"
project = "sils-keeper-infra"
network = "default"
direction = "EGRESS"
allow {
protocol = "all"
}
destination_ranges = ["0.0.0.0/0"]
description = "Allow all outbound traffic - env: test, owner: julian sanchez"
target_tags = ["nginx"]
} -
terraform plan -
terraform apply
Output:
google_compute_firewall.http: Creating...
google_compute_firewall.nginx-egress-all: Creating...
google_compute_firewall.ssh: Creating...
google_compute_instance.nginx-server: Creating...
google_compute_firewall.http: Still creating... [10s elapsed]
google_compute_firewall.nginx-egress-all: Still creating... [10s elapsed]
google_compute_firewall.ssh: Still creating... [10s elapsed]
google_compute_instance.nginx-server: Still creating... [10s elapsed]
google_compute_firewall.http: Creation complete after 13s [id=projects/sils-keeper-infra/global/firewalls/allow-http]
google_compute_firewall.nginx-egress-all: Creation complete after 13s [id=projects/sils-keeper-infra/global/firewalls/nginx-egress-all]
google_compute_firewall.ssh: Creation complete after 15s [id=projects/sils-keeper-infra/global/firewalls/allow-ssh]
google_compute_instance.nginx-server: Still creating... [20s elapsed]
google_compute_instance.nginx-server: Creation complete after 23s [id=projects/sils-keeper-infra/zones/us-central1-a/instances/nginx-server]
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
Outputs:
public_ip = "34.59.81.30"
Probemos la ip!
curl http://34.59.81.30
Output!
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
-
Si quiero ver solo un solo output
terraform output public_ip -
Destruir infra
terraform destroy
Enjoy!