## 创建EIP ## 1. ECS公网IP 与eip 区别 序号 |功能| ECS公网IP | EIP :-: | :-: | :-: | :-: 1 |支持的网络环境 |专有网络 |专有网络 经典网络 2|是否能够单独持有|否|是 3|是否支持在ECS上的弹性插拔|否|是 4|ECS实例网卡上是否能看到该IP|经典网络:能看到;专有网络VPC:看不到|多EIP网卡可见模式下可见 2. 创建Eip EIP是一种NAT IP。它实际位于阿里云的公网网关上,通过NAT方式映射到了被绑定的云资源上。和云资源绑定后,云资源可以通过EIP与公网通信。 ● 可独立持有的公网IP ● 可用于VPC中的 ECS实例、NAT网关、私网负载均衡SLB等产品 ● 可动态绑定和解绑 ● 可自带IP地址上云,系统架构变动小,IP品牌信誉不变 ``` resource "alicloud_eip" "eip" { bandwidth = "10" # Internet charge type of the EIP, Valid values are PayByBandwidth, PayByTraffic. Default to PayByBandwidth. From version 1.7.1, default to PayByTraffic. It is only PayByBandwidth when instance_charge_type is PrePaid internet_charge_type = "PayByTraffic" # Elastic IP instance charge type. Valid values are "PrePaid" and "PostPaid". Default to "PostPaid". instance_charge_type = "PostPaid" } resource "alicloud_eip_association" "eip_asso" { allocation_id = alicloud_eip.eip.id instance_id = alicloud_instance.server.id } ``` 出现执行错误的问题,这是因为eip之前服务器已经有了ip了 ``` Error: [ERROR] terraform-provider-alicloud/alicloud/resource_alicloud_eip_association.go:99: Resource alicloud_eip_association AssociateEipAddress Failed!!! [SDK alibaba-cloud-sdk-go ERROR]: │ SDK.ServerError │ ErrorCode: EIP_CAN_NOT_ASSOCIATE_WITH_PUBLIC_IP │ Recommend: https://error-center.aliyun.com/status/search?Keyword=EIP_CAN_NOT_ASSOCIATE_WITH_PUBLIC_IP&source=PopGw │ RequestId: F0830361-B3B9-4049-9724-482EC5E2C526 │ Message: instance already bind natpublicip,cannot bind eip. │ │ with alicloud_eip_association.eip_asso, │ on eip.tf line 12, in resource "alicloud_eip_association" "eip_asso": │ 12: resource "alicloud_eip_association" "eip_asso" { ``` 首先要更改internet_max_bandwidth_out,让ecs主机创建的时候不要分配公网IP ``` variable "internet_max_bandwidth_out" { default = 0 } ``` 然后更改 ``` # 使用file provisioner 拷贝文件到服务器 resource "null_resource" "copy" { # 等待server eip eip绑定完毕后再执行拷贝文件的操作 depends_on = [alicloud_instance.server,alicloud_eip.eip] triggers = { key = "${uuid()}" } provisioner "file" { source = "./html/" destination = "/usr/share/nginx/html/" connection { type = "ssh" user = "root" password = var.ecs_password host = "${alicloud_eip.eip.ip_address}" } } } ```