本文针对IaC(Infrastructure as Code)即基础设施即代码概念进行了简单介绍,同时通过Terraform工具去实战讲解如何远程操作我个人位于阿里云公有云以及VMware vSphere8私有云的的基础设施。在不久的将来,随着信创产业的不断壮大,信创云也会大量落地,由于信创的应用场景和客户的特殊性会要求集约化建设,而通过基础设施即代码IaC的理念去管理信创云基础设施全生命周期不失为一个好的数字化方案
Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Installing hashicorp/alicloud v1.211.0... - Installed hashicorp/alicloud v1.211.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.
╷ │ Warning: Additional provider information from registry │ │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud: │ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your sourcein required_providers. ╵
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 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:
# alicloud_instance.instance will be created + resource "alicloud_instance""instance" { + availability_zone = "cn-shanghai-b" + cpu = (known after apply) + credit_specification = (known after apply) + deletion_protection = false + deployment_set_group_no = (known after apply) + dry_run = false + host_name = (known after apply) + http_endpoint = (known after apply) + http_put_response_hop_limit = (known after apply) + http_tokens = (known after apply) + id = (known after apply) + image_id = "anolisos_8_8_x64_20G_rhck_uefi_alibase_20230308.vhd" + instance_charge_type = "PostPaid" + instance_name = "tf_test_instance" + instance_type = "ecs.n1.tiny" + internet_charge_type = "PayByTraffic" + internet_max_bandwidth_in = (known after apply) + internet_max_bandwidth_out = 1 + ipv6_address_count = (known after apply) + ipv6_addresses = (known after apply) + key_name = (known after apply) + maintenance_action = (known after apply) + memory = (known after apply) + network_interface_id = (known after apply) + os_name = (known after apply) + os_type = (known after apply) + password = (sensitive value) + primary_ip_address = (known after apply) + private_ip = (known after apply) + public_ip = (known after apply) + role_name = (known after apply) + secondary_private_ip_address_count = (known after apply) + secondary_private_ips = (known after apply) + security_groups = (known after apply) + spot_duration = (known after apply) + spot_strategy = (known after apply) + status = (known after apply) + stopped_mode = (known after apply) + subnet_id = (known after apply) + system_disk_category = "cloud_efficiency" + system_disk_description = "tf_system_disk_description" + system_disk_id = (known after apply) + system_disk_name = "tf_system_disk_name" + system_disk_performance_level = (known after apply) + system_disk_size = 40 + volume_tags = (known after apply) + vswitch_id = (known after apply) }
# alicloud_security_group.default will be created + resource "alicloud_security_group""default" { + description = "tf security group" + id = (known after apply) + inner_access = (known after apply) + inner_access_policy = (known after apply) + name = "default" + security_group_type = "normal" + vpc_id = (known after apply) }
# alicloud_security_group_rule.allow_22_tcp will be created + resource "alicloud_security_group_rule""allow_22_tcp" { + cidr_ip = "0.0.0.0/0" + id = (known after apply) + ip_protocol = "tcp" + nic_type = "intranet" + policy = "accept" + port_range = "22/22" + prefix_list_id = (known after apply) + priority = 1 + security_group_id = (known after apply) + type = "ingress" }
# alicloud_security_group_rule.allow_443_tcp will be created + resource "alicloud_security_group_rule""allow_443_tcp" { + cidr_ip = "0.0.0.0/0" + id = (known after apply) + ip_protocol = "tcp" + nic_type = "intranet" + policy = "accept" + port_range = "443/443" + prefix_list_id = (known after apply) + priority = 1 + security_group_id = (known after apply) + type = "ingress" }
# alicloud_security_group_rule.allow_80_tcp will be created + resource "alicloud_security_group_rule""allow_80_tcp" { + cidr_ip = "0.0.0.0/0" + id = (known after apply) + ip_protocol = "tcp" + nic_type = "intranet" + policy = "accept" + port_range = "80/80" + prefix_list_id = (known after apply) + priority = 1 + security_group_id = (known after apply) + type = "ingress" }
# alicloud_vpc.vpc will be created + resource "alicloud_vpc""vpc" { + cidr_block = "172.16.0.0/16" + create_time = (known after apply) + id = (known after apply) + ipv6_cidr_block = (known after apply) + ipv6_cidr_blocks = (known after apply) + name = (known after apply) + resource_group_id = (known after apply) + route_table_id = (known after apply) + router_id = (known after apply) + router_table_id = (known after apply) + secondary_cidr_blocks = (known after apply) + status = (known after apply) + user_cidrs = (known after apply) + vpc_name = "tf_test" }
# alicloud_vswitch.vsw will be created + resource "alicloud_vswitch""vsw" { + availability_zone = (known after apply) + cidr_block = "172.16.128.0/20" + create_time = (known after apply) + id = (known after apply) + ipv6_cidr_block = (known after apply) + ipv6_cidr_block_mask = (known after apply) + name = (known after apply) + status = (known after apply) + vpc_id = (known after apply) + vswitch_name = (known after apply) + zone_id = "cn-shanghai-b" }
会让用户进行进一步确认
1 2 3 4 5
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.
╷ │ Error: [ERROR] terraform-provider-alicloud/alicloud/resource_alicloud_vswitch.go:151: Resource alicloud_vswitch CreateVSwitch Failed!!! [SDK alibaba-cloud-sdk-go ERROR]: │ SDKError: │ StatusCode: 400 │ Code: ResourceNotAvailable │ Message: code: 400, Resource you requested is not available in this region or zone. request id: 1B7994FB-5E20-5F53-9925-80B2935A7529 │ Data: {"Code":"ResourceNotAvailable","HostId":"vpc.cn-shanghai.aliyuncs.com","Message":"Resource you requested is not available in this region or zone.","Recommend":"https://api.aliyun.com/troubleshoot?q=ResourceNotAvailable\u0026product=Vpc\u0026requestId=1B7994FB-5E20-5F53-9925-80B2935A7529","RequestId":"1B7994FB-5E20-5F53-9925-80B2935A7529"} │ │ │ with alicloud_vswitch.vsw, │ on aliyun.tf line 15, in resource "alicloud_vswitch""vsw": │ 15: resource "alicloud_vswitch""vsw" { │