lesson_terraform_cgp_01
Differences
This shows you the differences between two versions of the page.
| lesson_terraform_cgp_01 [2025/07/26 17:09] – - Imported by DokuWiki Advanced Plugin wikiadm | lesson_terraform_cgp_01 [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Lição Terraform GCP 01 ====== | ||
| - | FIXME **Fluxo: ** file.tf -> terraform init -> terraform plan -> terraform apply -> terraform destroy | ||
| - | |||
| - | FIXME **Para identar:** terraform fmt | ||
| - | |||
| - | ===== Iniciando o provider ===== | ||
| - | <code bash> | ||
| - | $ mkdir lession01 | ||
| - | $ cd lession01 | ||
| - | </ | ||
| - | |||
| - | <file tf provider.tf> | ||
| - | terraform { | ||
| - | required_providers { | ||
| - | google = { | ||
| - | source = " | ||
| - | version = " | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | |||
| - | provider " | ||
| - | # Configuration options | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | $ terraform init | ||
| - | |||
| - | Initializing the backend... | ||
| - | |||
| - | Initializing provider plugins... | ||
| - | - Finding hashicorp/ | ||
| - | - Installing hashicorp/ | ||
| - | - Installed hashicorp/ | ||
| - | |||
| - | Terraform has created a lock file .terraform.lock.hcl to record the provider | ||
| - | selections it made above. Include this file in your version control repository | ||
| - | so that Terraform can guarantee to make the same selections by default when | ||
| - | you run " | ||
| - | |||
| - | Terraform has been successfully initialized! | ||
| - | |||
| - | You may now begin working with Terraform. Try running " | ||
| - | any changes that are required for your infrastructure. All Terraform commands | ||
| - | should now work. | ||
| - | |||
| - | If you ever set or change modules or backend configuration for Terraform, | ||
| - | rerun this command to reinitialize your working directory. If you forget, other | ||
| - | commands will detect it and remind you to do so if necessary. | ||
| - | </ | ||
| - | |||
| - | ===== Iniciando o projeto ===== | ||
| - | |||
| - | Requisito: ativar o **Compute Engine API** e criar uma **Service accounts** | ||
| - | |||
| - | * Navigation menu -> Compute Engine -> VM instances | ||
| - | * Navigation menu -> IAM & Admin -> Service accounts | ||
| - | |||
| - | FIXME Baixar a chave depois de criar Service account | ||
| - | |||
| - | <file tf provider.tf> | ||
| - | $ cat provider.tf | ||
| - | terraform { | ||
| - | required_providers { | ||
| - | google = { | ||
| - | source | ||
| - | version = " | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | |||
| - | provider " | ||
| - | credentials = "/ | ||
| - | project | ||
| - | region | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | $ terraform fmt | ||
| - | $ terraform validate | ||
| - | Success! The configuration is valid. | ||
| - | </ | ||
| - | |||
| - | ===== VPC ===== | ||
| - | |||
| - | <file tf network.tf> | ||
| - | resource " | ||
| - | name = " | ||
| - | auto_create_subnetworks = false | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | $ terraform plan | ||
| - | |||
| - | Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: | ||
| - | + create | ||
| - | |||
| - | Terraform will perform the following actions: | ||
| - | |||
| - | # google_compute_network.vpc_network will be created | ||
| - | + resource " | ||
| - | + auto_create_subnetworks | ||
| - | + delete_default_routes_on_create = false | ||
| - | + gateway_ipv4 | ||
| - | + id = (known after apply) | ||
| - | + internal_ipv6_range | ||
| - | + mtu = (known after apply) | ||
| - | + name = " | ||
| - | + project | ||
| - | + routing_mode | ||
| - | + self_link | ||
| - | } | ||
| - | |||
| - | Plan: 1 to add, 0 to change, 0 to destroy. | ||
| - | |||
| - | ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── | ||
| - | |||
| - | Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run " | ||
| - | now. | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | $ terraform apply | ||
| - | |||
| - | Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: | ||
| - | + create | ||
| - | |||
| - | Terraform will perform the following actions: | ||
| - | |||
| - | # google_compute_network.vpc_network will be created | ||
| - | + resource " | ||
| - | + auto_create_subnetworks | ||
| - | + delete_default_routes_on_create = false | ||
| - | + gateway_ipv4 | ||
| - | + id = (known after apply) | ||
| - | + internal_ipv6_range | ||
| - | + mtu = (known after apply) | ||
| - | + name = " | ||
| - | + project | ||
| - | + routing_mode | ||
| - | + self_link | ||
| - | } | ||
| - | |||
| - | Plan: 1 to add, 0 to change, 0 to destroy. | ||
| - | |||
| - | Do you want to perform these actions? | ||
| - | Terraform will perform the actions described above. | ||
| - | Only ' | ||
| - | |||
| - | Enter a value: yes | ||
| - | |||
| - | google_compute_network.vpc_network: | ||
| - | google_compute_network.vpc_network: | ||
| - | google_compute_network.vpc_network: | ||
| - | |||
| - | Apply complete! Resources: 1 added, 0 changed, 0 destroyed. | ||
| - | </ | ||
| - | |||
| - | ===== Firewall ===== | ||
| - | <file tf firewall.tf> | ||
| - | $ cat firewall.tf | ||
| - | resource " | ||
| - | name = " | ||
| - | network = " | ||
| - | |||
| - | allow { | ||
| - | protocol = " | ||
| - | } | ||
| - | |||
| - | allow { | ||
| - | protocol = " | ||
| - | ports = [" | ||
| - | } | ||
| - | |||
| - | source_ranges = [" | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | $ terraform apply -auto-approve | ||
| - | google_compute_network.vpc_network: | ||
| - | |||
| - | Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: | ||
| - | + create | ||
| - | |||
| - | Terraform will perform the following actions: | ||
| - | |||
| - | # google_compute_firewall.allow-icmp-ssh will be created | ||
| - | + resource " | ||
| - | + creation_timestamp = (known after apply) | ||
| - | + destination_ranges = (known after apply) | ||
| - | + direction | ||
| - | + enable_logging | ||
| - | + id = (known after apply) | ||
| - | + name = " | ||
| - | + network | ||
| - | + priority | ||
| - | + project | ||
| - | + self_link | ||
| - | + source_ranges | ||
| - | + " | ||
| - | ] | ||
| - | |||
| - | + allow { | ||
| - | + ports = [ | ||
| - | + " | ||
| - | ] | ||
| - | + protocol = " | ||
| - | } | ||
| - | + allow { | ||
| - | + ports = [] | ||
| - | + protocol = " | ||
| - | } | ||
| - | } | ||
| - | |||
| - | Plan: 1 to add, 0 to change, 0 to destroy. | ||
| - | google_compute_firewall.allow-icmp-ssh: | ||
| - | google_compute_firewall.allow-icmp-ssh: | ||
| - | google_compute_firewall.allow-icmp-ssh: | ||
| - | |||
| - | Apply complete! Resources: 1 added, 0 changed, 0 destroyed. | ||
| - | </ | ||
| - | |||
| - | ===== Subnet ===== | ||
| - | |||
| - | <file tf subnet.tf> | ||
| - | resource " | ||
| - | name = " | ||
| - | ip_cidr_range | ||
| - | region | ||
| - | network | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ===== Instance ===== | ||
| - | <file tf instance.tf> | ||
| - | resource " | ||
| - | name = " | ||
| - | machine_type = " | ||
| - | zone = " | ||
| - | |||
| - | boot_disk { | ||
| - | initialize_params { | ||
| - | image = " | ||
| - | } | ||
| - | } | ||
| - | |||
| - | network_interface { | ||
| - | network = " | ||
| - | subnetwork = " | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ===== Disk ===== | ||
| - | <file tf disk.tf> | ||
| - | resource " | ||
| - | name = " | ||
| - | type = " | ||
| - | zone = " | ||
| - | size = 30 | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <file tf instance.tf> | ||
| - | resource " | ||
| - | name = " | ||
| - | machine_type = " | ||
| - | zone = " | ||
| - | |||
| - | boot_disk { | ||
| - | initialize_params { | ||
| - | image = " | ||
| - | } | ||
| - | } | ||
| - | |||
| - | network_interface { | ||
| - | network | ||
| - | subnetwork = " | ||
| - | } | ||
| - | |||
| - | attached_disk { | ||
| - | source = " | ||
| - | } | ||
| - | |||
| - | allow_stopping_for_update = true | ||
| - | } | ||
| - | </ | ||
| - | ===== Referências ===== | ||
| - | - [[https:// | ||
| - | - [[https:// | ||
| - | - [[https:// | ||
| - | - [[https:// | ||
| - | - [[https:// | ||
| - | - [[https:// | ||
lesson_terraform_cgp_01.1753560544.txt.gz · Last modified: by wikiadm
