Skip to content

Commit f89e2b0

Browse files
lsy1968shanye997
authored andcommitted
201-use-case-bind-eip-to-ecs-eni
1 parent 886b4eb commit f89e2b0

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## Introduction
2+
3+
<!-- DOCS_DESCRIPTION_CN -->
4+
本示例用于在阿里云上为ECS弹性网卡绑定EIP。
5+
详情可查看[为弹性网卡绑定EIP](https://help.aliyun.com/document_detail/156980.html)
6+
<!-- DOCS_DESCRIPTION_CN -->
7+
8+
<!-- DOCS_DESCRIPTION_EN -->
9+
This example is used to bind EIPs to ECS elastic network interface on Alibaba Cloud.
10+
More details in [How to bind EIPs to ECS elastic network interface](https://help.aliyun.com/document_detail/156980.html).
11+
<!-- DOCS_DESCRIPTION_EN -->
12+
13+
<!-- BEGIN_TF_DOCS -->
14+
## Providers
15+
16+
| Name | Version |
17+
|------|---------|
18+
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
19+
20+
## Modules
21+
22+
No modules.
23+
24+
## Resources
25+
26+
| Name | Type |
27+
|------|------|
28+
| [alicloud_eip.eip](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/eip) | resource |
29+
| [alicloud_eip_association.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/eip_association) | resource |
30+
| [alicloud_network_interface.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/network_interface) | resource |
31+
| [alicloud_security_group.group](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group) | resource |
32+
| [alicloud_security_group_rule.allow_80_tcp](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule) | resource |
33+
| [alicloud_vpc.vpc](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
34+
| [alicloud_vswitch.vswitch](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
35+
| [alicloud_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/zones) | data source |
36+
37+
## Inputs
38+
39+
| Name | Description | Type | Default | Required |
40+
|------|-------------|------|---------|:--------:|
41+
| <a name="input_private_ip"></a> [private\_ip](#input\_private\_ip) | The primary private IP address of the ENI. The specified IP address must be available within the CIDR block of the VSwitch. If this parameter is not specified, an available IP address is assigned from the VSwitch CIDR block at random. | `string` | `""` | no |
42+
| <a name="input_region"></a> [region](#input\_region) | The region where the resources will be created. | `string` | `"cn-beijing"` | no |
43+
| <a name="input_source_ip"></a> [source\_ip](#input\_source\_ip) | The IP address you used to access the ENI. | `string` | `"0.0.0.0/0"` | no |
44+
| <a name="input_vpc_cidr_block"></a> [vpc\_cidr\_block](#input\_vpc\_cidr\_block) | Specify the CIDR block of the VPC. If the vpc\_id is provided, this value can be left blank. | `string` | `"192.168.0.0/16"` | no |
45+
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | When binding an ENI to an existing ECS instance, this value is required and must be the VPC associated with the ECS instance. | `string` | `""` | no |
46+
| <a name="input_vswitch_cidr_block"></a> [vswitch\_cidr\_block](#input\_vswitch\_cidr\_block) | Specify the CIDR block of the VSwitch. The CIDR block must be within the range of the VPC CIDR block. | `string` | `"192.168.0.0/24"` | no |
47+
| <a name="input_zone_id"></a> [zone\_id](#input\_zone\_id) | When binding an ENI to an existing ECS instance, this value is required and must be the zone where the ECS instance is located. | `string` | `""` | no |
48+
<!-- END_TF_DOCS -->
49+
50+
<!-- docs-link -->
51+
52+
The template is based on Aliyun document: [How to bind EIPs to ECS elastic network interface](https://help.aliyun.com/document_detail/156980.html)
53+
54+
<!-- docs-link -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
provider "alicloud" {
2+
region = var.region
3+
}
4+
5+
# 资源将要创建的地域
6+
variable "region" {
7+
default = "cn-beijing"
8+
description = "The region where the resources will be created."
9+
}
10+
11+
# 输入已有的VPC ID,当为已有ECS实例绑定弹性网卡时,该值必填,且值为ECS实例所对应的VPC。
12+
variable "vpc_id" {
13+
default = ""
14+
description = "When binding an ENI to an existing ECS instance, this value is required and must be the VPC associated with the ECS instance."
15+
}
16+
17+
# 指定VPC的CIDR块,当填入vpc_id时,该值可不填。
18+
variable "vpc_cidr_block" {
19+
default = "192.168.0.0/16"
20+
description = "Specify the CIDR block of the VPC. If the vpc_id is provided, this value can be left blank."
21+
}
22+
23+
# 输入可用区,当为已有ECS实例绑定弹性网卡时,该值必填,且值为ECS实例所在可用区。
24+
variable "zone_id" {
25+
default = ""
26+
description = "When binding an ENI to an existing ECS instance, this value is required and must be the zone where the ECS instance is located."
27+
}
28+
29+
# 指定VSwitch的CIDR块,CIDR块需在VPC CIDR块的范围内
30+
variable "vswitch_cidr_block" {
31+
default = "192.168.0.0/24"
32+
description = "Specify the CIDR block of the VSwitch. The CIDR block must be within the range of the VPC CIDR block."
33+
}
34+
35+
# 访问弹性网卡的源地址
36+
variable "source_ip" {
37+
description = "The IP address you used to access the ENI."
38+
type = string
39+
default = "0.0.0.0/0"
40+
}
41+
42+
# 指定弹性网卡的私网IP地址
43+
variable "private_ip" {
44+
description = "The primary private IP address of the ENI. The specified IP address must be available within the CIDR block of the VSwitch. If this parameter is not specified, an available IP address is assigned from the VSwitch CIDR block at random."
45+
type = string
46+
default = ""
47+
}
48+
49+
locals {
50+
new_zone_id = var.zone_id == ""
51+
create_vpc = var.vpc_id == ""
52+
}
53+
54+
resource "alicloud_eip" "eip" {
55+
address_name = "test_eip"
56+
}
57+
58+
resource "alicloud_vpc" "vpc" {
59+
count = local.create_vpc ? 1 : 0
60+
vpc_name = "test_vpc"
61+
cidr_block = var.vpc_cidr_block
62+
}
63+
64+
data "alicloud_zones" "default" {
65+
count = local.new_zone_id ? 1 : 0
66+
available_resource_creation = "VSwitch"
67+
}
68+
69+
resource "alicloud_vswitch" "vswitch" {
70+
vswitch_name = "test_vswitch"
71+
cidr_block = var.vswitch_cidr_block
72+
zone_id = local.new_zone_id ? data.alicloud_zones.default[0].zones.0.id : var.zone_id
73+
vpc_id = local.create_vpc ? alicloud_vpc.vpc[0].id : var.vpc_id
74+
}
75+
76+
resource "alicloud_security_group" "group" {
77+
security_group_name = "test_sg"
78+
vpc_id = local.create_vpc ? alicloud_vpc.vpc[0].id : var.vpc_id
79+
}
80+
81+
# 添加允许TCP 80端口入方向流量的规则
82+
resource "alicloud_security_group_rule" "allow_80_tcp" {
83+
type = "ingress"
84+
ip_protocol = "tcp"
85+
nic_type = "intranet"
86+
policy = "accept"
87+
port_range = "80/80"
88+
priority = 1
89+
security_group_id = alicloud_security_group.group.id
90+
cidr_ip = var.source_ip
91+
}
92+
93+
resource "alicloud_network_interface" "default" {
94+
network_interface_name = "test_eni"
95+
vswitch_id = alicloud_vswitch.vswitch.id
96+
security_group_ids = [alicloud_security_group.group.id]
97+
primary_ip_address = var.private_ip
98+
secondary_private_ip_address_count = 1
99+
}
100+
101+
resource "alicloud_eip_association" "default" {
102+
allocation_id = alicloud_eip.eip.id
103+
instance_type = "NetworkInterface"
104+
instance_id = alicloud_network_interface.default.id
105+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
required_providers {
3+
alicloud = {
4+
source = "aliyun/alicloud"
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)