在Windows 10/Windows Server 2019上安装Terraform
问:如何在Windows 10/Windows Server 2019上安装Terraform?
本教程将在Windows 10和Windows Server 2019上安装TerraForm的步骤.Terraform是一种云可靠的基础架构自动化工具,用于管理代码中的云和内部部署资源。
Terraform可以构建,更改和版本在流行的服务提供商上部署的基础架构。
使用Terraform,我们可以使用简单的声明性编程语言管理计算,网络,DNS,数据库资源和许多其他人。
请参阅Terraform提供程序的完整列表。
在Windows桌面/Windows Server上安装Terraform。
在本教程中,我们将使用Scoop命令行安装程序进行Windows以在Windows上设置Terraform。
在继续之前,我们需要在Windows计算机上安装Scoop,我们可以使用下面的教程。
如何从Windows命令行安装应用程序
安装勺后,使用它来安装Terraform。
PS C:\Users\Administrator> scoop install terraform which vim touch WARN Scoop uses 'aria2c' for multi-connection downloads. WARN Should it cause issues, run 'scoop config aria2-enabled false' to disable it. Installing 'terraform' (0.11.12) [64bit] Starting download with aria2 … Download: [#3633c5 96KiB/20MiB(0%) CN:4 DL:245KiB ETA:1m24s] Download: [#3633c5 1.0MiB/20MiB(5%) CN:4 DL:698KiB ETA:28s] Download: [#3633c5 1.7MiB/20MiB(8%) CN:4 DL:688KiB ETA:27s] Download: [#3633c5 2.0MiB/20MiB(10%) CN:4 DL:577KiB ETA:32s] Download: [#3633c5 2.1MiB/20MiB(10%) CN:4 DL:393KiB ETA:47s] Download: [#3633c5 2.4MiB/20MiB(12%) CN:4 DL:384KiB ETA:47s] Download: [#3633c5 2.5MiB/20MiB(12%) CN:4 DL:352KiB ETA:51s] Download: [#3633c5 2.7MiB/20MiB(13%) CN:4 DL:325KiB ETA:55s] .....
Terraform EXE文件将位于内部 ~/scoop/directory
。
PS C:\Users\Administrator> which terraform C:\Users\Administrator\scoop\shims\terraform.EXE
在Windows桌面/Windows Server上配置Terraform
现在安装了Terraform,让我们创建一个测试项目。
> mkdir projects Directory: C:\Users\Administrator Mode LastWriteTime Length Name ---- ------------- ------ --- d----- 3/9/2019 1:57 AM projects PS C:\Users\Administrator> mkdir 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----- 3/9/2019 1:58 AM terraform PS C:\Users\Administrator\projects> cd .\terraform\
创建Terraform Main配置文件。
touch main.tf
我正在使用AWS提供商进行测试,但我们可以使用其他提供商为项目进行。
我的Terraform配置提供商部分如下
PS C:\Users\Administrator\projects\terraform> cat .\main.tf # Provider provider "aws" { access_key = "" secret_key = "" region = "us-west-1" }
粘贴AWS访问密钥和密钥 access_key
和 secret_key
分别部分。
完成后,运行 terraform init
初始化Terraform Working目录。
PS C:\Users\Administrator\projects\terraform> terraform init Initializing provider plugins… Checking for available provider plugins on https://releases.hashicorp.com… Downloading plugin for provider "aws" (2.1.0)… The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that Jan contain breaking changes, it is recommended to add version = "…" constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. provider.aws: version = "~> 2.1" Terraform has been successfully initialized! You Jan 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----- 3/9/2019 2:06 AM .terraform -a---- 3/9/2019 2:04 AM 42358 .main.tf.un~ -a---- 3/9/2019 2:04 AM 99 main.tf -a---- 3/9/2019 2:03 AM 59 main.tf~
内部还有另一个文件夹,该文件夹由Terraform下载的插件。
现在让我们通过编辑来添加资源部分来创建AWS VPC和子网资源 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" } }
添加资源定义后保存文件并设置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构建基础架构 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}" ...........................
确认要进行的更改,然后执行"是"以启动修改。
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运行应该在最后打印成功消息。
Terraform状态被保存到 .\terraform.tfstate
但是可以改变后端。
我们可以从AWS控制台确认基础架构更改。
摧毁地带基础设施
我们已确认我们在Windows上的Terraform安装按预期工作。
通过运行步摧毁Terraform-Managed基础架构 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
如果我们不想确认提示,请使用:
terraform destroy -auto-approve
登录AWS控制台并确认资源删除。