阿里云创建ECS实例管理云资源

启动Cloud Shell

选择一种方式启动云命令行:

在控制台中运行

单击控制台首页头部导航的命令行按钮,启动云命令行。

独立运行

在浏览器中输入

https://shell.aliyun.com

打开云命令行操作界面。

您可以根据实际需要打开多个命令行窗口,但最多可同时打开5个云命令行窗口。

在启动云命令时,请注意:
第一次连接云命令行时会为您创建虚拟机,会消耗一些时间,最长不超过30秒。

打开多个云命令行窗口时,所有窗口都会连接到同一台虚拟机。虚拟机数量不会因您打开新的命令行窗口而增加。

在Cloud Shell中编写Terraform模板管理资源。

您可以使用vim命令直接编写模板。

  • 执行如下命令创建一个工程目录及模板文件:

    1
    2
    3
    mkdir terraform-project
    cd terraform-project
    touch main.tf
  • 编辑main.tf文件,添加如下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    provider "alicloud" {
    region = "cn-huhehaote"
    }

    data "alicloud_zones" "default" {
    available_disk_category = "cloud_efficiency"
    available_resource_creation = "VSwitch"
    }

    resource "alicloud_vpc" "vpc" {
    vpc_name = "tf_test_foo"
    cidr_block = "172.16.0.0/12"
    }

    resource "alicloud_vswitch" "vsw" {
    vpc_id = alicloud_vpc.vpc.id
    cidr_block = "172.16.0.0/21"
    zone_id = data.alicloud_zones.default.zones.0.id
    }

    resource "alicloud_security_group" "default" {
    name = "default"
    vpc_id = alicloud_vpc.vpc.id
    }

    resource "alicloud_instance" "instance" {
    # cn-beijing
    availability_zone = data.alicloud_zones.default.zones.0.id
    security_groups = alicloud_security_group.default.*.id
    # series III
    instance_type = "ecs.e-c1m1.large"
    # system_disk_category = "cloud_efficiency"
    image_id = "centos_7_9_x64_20G_alibase_20240628.vhd"
    instance_name = "test_foo"
    vswitch_id = alicloud_vswitch.vsw.id
    internet_max_bandwidth_out = 10
    }

    resource "alicloud_security_group_rule" "allow_all_tcp" {
    type = "ingress"
    ip_protocol = "tcp"
    nic_type = "intranet"
    policy = "accept"
    port_range = "1/65535"
    priority = 1
    security_group_id = alicloud_security_group.default.id
    cidr_ip = "0.0.0.0/0"
    }
  • 执行init命令初始化Terraform。

1
terraform init
  • 执行plan命令预览配置。
1
terraform plan

配置示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.233.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 may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.alicloud: version = "~> 1.233"


Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in 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.
shell@Alicloud:~/terraform-project$ 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.alicloud_zones.default: 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:

# alicloud_instance.instance will be created
+ resource "alicloud_instance" "instance" {
+ availability_zone = "cn-huhehaote-a"
+ cpu = (known after apply)
+ create_time = (known after apply)
+ credit_specification = (known after apply)
+ deletion_protection = false
+ deployment_set_group_no = (known after apply)
+ description = (known after apply)
+ dry_run = false
+ enable_jumbo_frame = (known after apply)
+ expired_time = (known after apply)
+ 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 = "centos_7_9_x64_20G_alibase_20240628.vhd"
+ instance_charge_type = (known after apply)
+ instance_name = "test_foo"
+ instance_type = "ecs.e-c1m1.large"
+ internet_charge_type = (known after apply)
+ internet_max_bandwidth_in = (known after apply)
+ internet_max_bandwidth_out = 10
+ io_optimized = (known after apply)
+ 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)
+ network_interface_traffic_mode = (known after apply)
+ os_name = (known after apply)
+ os_type = (known after apply)
+ period = (known after apply)
+ primary_ip_address = (known after apply)
+ private_ip = (known after apply)
+ public_ip = (known after apply)
+ resource_group_id = (known after apply)
+ role_name = (known after apply)
+ secondary_private_ip_address_count = (known after apply)
+ secondary_private_ips = (known after apply)
+ security_enhancement_strategy = (known after apply)
+ security_groups = (known after apply)
+ spot_duration = (known after apply)
+ spot_price_limit = (known after apply)
+ spot_strategy = (known after apply)
+ start_time = (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 = (known after apply)
+ system_disk_encrypted = (known after apply)
+ system_disk_id = (known after apply)
+ system_disk_name = (known after apply)
+ system_disk_performance_level = (known after apply)
+ system_disk_size = (known after apply)
+ volume_tags = (known after apply)
+ vpc_id = (known after apply)
+ vswitch_id = (known after apply)

+ network_interfaces {
+ network_card_index = (known after apply)
+ network_interface_id = (known after apply)
+ network_interface_traffic_mode = (known after apply)
+ queue_pair_number = (known after apply)
+ security_group_ids = (known after apply)
+ vswitch_id = (known after apply)
}
}

# alicloud_security_group.default will be created
+ resource "alicloud_security_group" "default" {
+ id = (known after apply)
+ inner_access = (known after apply)
+ inner_access_policy = (known after apply)
+ name = "default"
+ security_group_type = (known after apply)
+ vpc_id = (known after apply)
}

# alicloud_security_group_rule.allow_all_tcp will be created
+ resource "alicloud_security_group_rule" "allow_all_tcp" {
+ cidr_ip = "0.0.0.0/0"
+ id = (known after apply)
+ ip_protocol = "tcp"
+ nic_type = "intranet"
+ policy = "accept"
+ port_range = "1/65535"
+ 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/12"
+ 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_foo"
}

# alicloud_vswitch.vsw will be created
+ resource "alicloud_vswitch" "vsw" {
+ availability_zone = (known after apply)
+ cidr_block = "172.16.0.0/21"
+ 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-huhehaote-a"
}
  • 执行apply命令创建ECS实例。

    1
    terraform apply

    执行结果如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    Plan: 5 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.


    Plan: 5 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

    alicloud_vpc.vpc: Creating...
    alicloud_vpc.vpc: Creation complete after 6s [id=vpc-hp31tfk65xzegpb8tsyuh]
    alicloud_security_group.default: Creating...
    alicloud_vswitch.vsw: Creating...
    alicloud_security_group.default: Creation complete after 1s [id=sg-hp30eo4i39oj1ke5gnr5]
    alicloud_security_group_rule.allow_all_tcp: Creating...
    alicloud_security_group_rule.allow_all_tcp: Creation complete after 1s [id=sg-hp30eo4i39oj1ke5gnr5:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
    alicloud_vswitch.vsw: Creation complete after 4s [id=vsw-hp32rwyzfj6r21d7bupme]
    alicloud_instance.instance: Creating...
  • 如遇报错,请检查账号权限是否开通