当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 Windows 10/Windows Server 2019 上安装 Terraform

作者:精品下载站 日期:2024-12-14 12:23:34 浏览:15 分类:玩电脑

在 Windows 10/Windows Server 2019 上安装 Terraform


问:如何在 Windows 10 上安装 Terraform/Windows Server 2019?本指南将引导您完成在 Windows 10 和 Windows Server 2019 上安装 Terraform 的步骤。Terraform 是一种与云无关的基础设施自动化工具,用于管理代码中的云和本地资源。 Terraform 可以构建、更改和版本化部署在流行服务提供商上的基础设施。

借助 Terraform,您可以使用简单的声明性编程语言来管理计算、网络、DNS、数据库资源和许多其他资源。查看 Terraform 提供商的完整列表。

在 Windows 桌面/Windows 服务器上安装 Terraform。

在本指南中,我们将使用适用于 Windows 的 Scoop 命令行安装程序在 Windows 上安装 terraform。在继续之前,您需要在 Windows 计算机上安装 scoop,您可以使用下面的指南。

  • 如何从 Windows 命令行安装应用程序

安装 Scoop 后,使用它来安装 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.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 ...
.....

[玩转系统] 在 Windows 10/Windows Server 2019 上安装 Terraform

terraform exe 文件将位于 ~/scoop/ 目录 内。

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

在 Windows 桌面/Windows 服务器上配置 Terraform

现在 terraform 已安装,让我们创建一个测试项目。

 > mkdir projects


    Directory: C:\Users\kipla


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

在项目目录中创建 terraform 文件夹。

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\

创建 Terraform 主配置文件。

touch main.tf

我正在使用 AWS 提供商进行测试,但您可以为您的项目使用其他提供商。我的 terraform 配置提供程序部分如下

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

将您的 AWS 访问密钥和秘密密钥分别粘贴到 access_keysecret_key 部分中。完成后,运行 terraform init 来初始化 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 将自动下载配置到 .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

里面还有另一个文件夹,用于存储 Terraform 下载的插件。

[玩转系统] 在 Windows 10/Windows Server 2019 上安装 Terraform

现在,我们通过编辑 main.tf 文件添加资源部分以创建 AWS VPC 和子网资源。

# 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"
 }
}

添加资源定义并设置 AWS 变量后保存文件,然后生成并显示执行计划。

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>

最后使用 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}"
...........................

确认要进行的更改并输入“yes”以启动修改。

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

成功的 terraform 运行应该在最后打印成功消息。

[玩转系统] 在 Windows 10/Windows Server 2019 上安装 Terraform

Terraform 状态保存到 .\terraform.tfstate 但后端可以更改。

您可以从 AWS 控制台确认基础设施更改。

[玩转系统] 在 Windows 10/Windows Server 2019 上安装 Terraform

[玩转系统] 在 Windows 10/Windows Server 2019 上安装 Terraform

摧毁 Terraform 基础设施

我们已确认 Windows 上的 Terraform 安装按预期工作。通过运行 terraform destroy 命令来销毁 Terraform 管理的基础设施。

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

如果您不想要确认提示,请使用:

terraform destroy -auto-approve

请参阅下面的输出:

[玩转系统] 在 Windows 10/Windows Server 2019 上安装 Terraform

登录AWS控制台并确认资源删除。

[玩转系统] 在 Windows 10/Windows Server 2019 上安装 Terraform

在其他系统上安装 terraform:

  • 如何在 Fedora 上安装 Terraform
  • 如何在 Ubuntu/CentOS 上安装 Terraform

更多 Windows 指南:

  • 如何在 Windows Server 上允许 ICMP 回显回复
  • 如何在 Windows Server 上运行 Docker 容器
  • 适用于 Linux、macOS 和 Windows 的最佳安全备份应用程序
  • 如何从 Windows 命令行安装应用程序
  • 如何在 Windows Server 上启用远程桌面协议 (RDP)
  • 如何使用 WSL 在 Windows Server 上运行 Linux

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯