Ricerca nel sito web

Installa Terraform su Windows 10/Windows Server 2019


D: Come posso installare Terraform su Windows 10/Windows Server 2019?. Questa guida ti guiderà attraverso i passaggi per installare Terraform su Windows 10 e Windows Server 2019. Terraform è uno strumento di automazione dell'infrastruttura indipendente dal cloud utilizzato per gestire le risorse cloud e locali nel codice. Terraform può creare, modificare e modificare le versioni dell'infrastruttura distribuita sui provider di servizi più diffusi.

Con Terraform puoi gestire risorse di calcolo, rete, DNS, database e molte altre utilizzando un semplice linguaggio di programmazione dichiarativo. Visualizza l'elenco completo dei provider Terraform.

Installa Terraform sul desktop Windows/Windows Server.

In questa guida utilizzeremo il programma di installazione da riga di comando Scoop per Windows per configurare Terraform su Windows. Prima di procedere, avrai bisogno di Scoop installato sul tuo computer Windows, puoi utilizzare la nostra guida di seguito.

  • Come installare le applicazioni dalla riga di comando di Windows

Una volta installato Scoop, utilizzalo per installare terraform.

PS C:\Users\Administrator> scoop install terraform which vim touch
Installing 'terraform' (1.5.5) [64bit] from main bucket                                                                 Starting download with aria2 ...                                                                                        Download: Download Results:                                                                                             Download: gid   |stat|avg speed  |path/URI
Download: ======+====+===========+=======================================================
Download: 04e241|OK  |   1.7MiB/s|C:/Users/kipla/scoop/cache/terraform#1.5.5#https_releases.hashicorp.com_terraform_1.5.5_terraform_1.5.5_windows_amd64.zip
Download: Status Legend:
Download: (OK):download completed.
Checking hash of terraform_1.5.5_windows_amd64.zip ... ok.
Extracting terraform_1.5.5_windows_amd64.zip ... done.
Running pre_install script...
Linking ~\scoop\apps\terraform\current => ~\scoop\apps\terraform\1.5.5
Creating shim for 'terraform'.
'terraform' (1.5.5) was installed successfully!
Installing 'touch' (0.2018.07.25) [64bit] from main bucket
Starting download with aria2 ...
.....

Il file exe terraform si troverà nella directory ~/scoop/.

PS C:\Users\Administrator> which terraform
 C:\Users\Administrator\scoop\shims\terraform.EXE

Configura Terraform su desktop Windows/Windows Server

Ora che Terraform è installato, creiamo un progetto di prova.

 > mkdir projects


    Directory: C:\Users\kipla


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         8/17/2023  11:31 AM                projects

Crea la cartella terraform all'interno della directory dei progetti.

PS C:\Users\Administrator> cd .\projects\
 PS C:\Users\Administrator\projects> mkdir terraform
 Directory: C:\Users\Administrator\projects
 Mode                LastWriteTime         Length Name
 ----                -------------         ------ ----
 d-----          8/17/2023  11:32 AM                terraform
 PS C:\Users\Administrator\projects> cd .\terraform\

Crea il file di configurazione principale di Terraform.

touch main.tf

Sto eseguendo un test con il provider AWS ma puoi utilizzare altri provider per i tuoi progetti. La sezione del mio provider di configurazione Terraform è la seguente

PS C:\Users\Administrator\projects\terraform> vim .\main.tf
# Provider
 provider "aws" {
   access_key = ""
   secret_key = ""
   region = "us-west-1"
 }

Incolla la chiave di accesso AWS e la chiave segreta rispettivamente nelle sezioni access_key e secret_key. Al termine, esegui terraform init per inizializzare una directory di lavoro Terraform.

PS C:\Users\Administrator\projects\terraform> terraform init
Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v5.12.0...
- Installed hashicorp/aws v5.12.0 (signed by 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 init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
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.

Terraform scaricherà automaticamente il provider configurato nella directory .terraform.

PS C:\Users\Administrator\projects\terraform> ls
Directory: C:\Users\Administrator\projects\terraform
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         8/17/2023  11:35 AM                .terraform
-a----         8/17/2023  11:35 AM           1377 .terraform.lock.hcl
-a----         8/17/2023  11:34 AM            100 main.tf

All'interno c'è un'altra cartella che memorizza i plugin scaricati da Terraform.

Aggiungiamo ora la sezione delle risorse per creare risorse AWS VPC e sottorete modificando il file main.tf.

# Provider
 provider "aws" {
   access_key = ""
   secret_key = ""
   region = ""
 }

# Retrieve the AZ where we want to create network resources
data "aws_availability_zones" "available" {}

# VPC Resource
resource "aws_vpc" "main" {
  cidr_block = "10.11.0.0/16"
  enable_dns_support = true
  enable_dns_hostnames = true
  tags {
    Name = "Test-VPC"
  }
  tags {
    Environment = "Test"
  }
}

# AWS subnet resource
resource "aws_subnet" "test" {
 vpc_id = "${aws_vpc.main.id}"
 cidr_block = "10.11.1.0/24"
 availability_zone = "${data.aws_availability_zones.available.names[0]}"
 map_public_ip_on_launch = "false"
 tags {
   Name = "Test_subnet1"
 }
}

Salva il file dopo aver aggiunto le definizioni delle risorse e impostato le variabili AWS, quindi genera e mostra un piano di esecuzione.

PS C:\Users\Administrator\projects\terraform> terraform plan

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.aws_availability_zones.available: Refreshing state...

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_subnet.test
      id:                               <computed>
      arn:                              <computed>
      assign_ipv6_address_on_creation:  "false"
      availability_zone:                "us-east-1a"
      availability_zone_id:             <computed>
      cidr_block:                       "10.11.1.0/24"
      ipv6_cidr_block:                  <computed>
      ipv6_cidr_block_association_id:   <computed>
      map_public_ip_on_launch:          "false"
      owner_id:                         <computed>
      tags.%:                           "1"
      tags.Name:                        "Test_subnet1"
      vpc_id:                           "${aws_vpc.main.id}"

  + aws_vpc.main
      id:                               <computed>
      arn:                              <computed>
      assign_generated_ipv6_cidr_block: "false"
      cidr_block:                       "10.11.0.0/16"
      default_network_acl_id:           <computed>
      default_route_table_id:           <computed>
      default_security_group_id:        <computed>
      dhcp_options_id:                  <computed>
      enable_classiclink:               <computed>
      enable_classiclink_dns_support:   <computed>
      enable_dns_hostnames:             "true"
      enable_dns_support:               "true"
      instance_tenancy:                 "default"
      ipv6_association_id:              <computed>
      ipv6_cidr_block:                  <computed>
      main_route_table_id:              <computed>
      owner_id:                         <computed>
      tags.%:                           "2"
      tags.Environment:                 "Test"
      tags.Name:                        "Test-VPC"


Plan: 2 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

PS C:\Users\Administrator\projects\terraform>

Infine costruisci la tua infrastruttura con Terraform utilizzando terraform apply.

PS C:\Users\Administrator\projects\terraform> terraform apply

data.aws_availability_zones.available: Refreshing state...

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_subnet.test
      id:                               <computed>
      arn:                              <computed>
      assign_ipv6_address_on_creation:  "false"
      availability_zone:                "us-east-1a"
      availability_zone_id:             <computed>
      cidr_block:                       "10.11.1.0/24"
      ipv6_cidr_block:                  <computed>
      ipv6_cidr_block_association_id:   <computed>
      map_public_ip_on_launch:          "false"
      owner_id:                         <computed>
      tags.%:                           "1"
      tags.Name:                        "Test_subnet1"
      vpc_id:                           "${aws_vpc.main.id}"
...........................

Conferma le modifiche da apportare e digita "" per avviare le modifiche.

Plan: 2 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
Enter a value: yes

Un'esecuzione di terraform riuscita dovrebbe stampare un messaggio di successo alla fine.

Lo stato di Terraform viene salvato in .\terraform.tfstate ma il backend può essere modificato.

Puoi confermare le modifiche all'infrastruttura dalla console AWS.

Distruggere l'infrastruttura Terraform

Abbiamo confermato che la nostra installazione Terraform su Windows funziona come previsto. distruggi l'infrastruttura gestita da Terraform eseguendo il comando terraform destroy.

PS C:\Users\Administrator\projects\terraform> terraform destroy

aws_vpc.main: Refreshing state... (ID: vpc-0e94a7d72c02dab2b)
data.aws_availability_zones.available: Refreshing state...
aws_subnet.test: Refreshing state... (ID: subnet-0ad06c2e86542ddc1)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - aws_subnet.test

  - aws_vpc.main


Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

Se non vuoi la richiesta di conferma, usa:

terraform destroy -auto-approve

Vedi sotto l'output:

Accedi alla console AWS e conferma l'eliminazione delle risorse.

Installazione di Terraform su altri sistemi:

  • Come installare Terraform su Fedora
  • Come installare Terraform su Ubuntu/CentOS

Altre guide su Windows:

  • Come consentire la risposta eco ICMP su Windows Server
  • Come eseguire i contenitori Docker su Windows Server
  • La migliore applicazione di backup sicura per Linux, macOS e Windows
  • Come installare le applicazioni dalla riga di comando di Windows
  • Come abilitare il protocollo RDP (Remote Desktop Protocol) su Windows Server
  • Come eseguire Linux su Windows Server con WSL

Articoli correlati: