云命令行(Cloud Shell)中预装了Terraform。Terraform是一种开源工具,用于安全高效地预配和管理云基础结构。您可以通过Terraform管理阿里云资源。
本教程管理云资源示例代码支持一键运行,您可以直接运行代码。一键运行
前提条件
为运行Terraform命令的RAM用户绑定以下最小权限策略,以获取管理本示例所涉及资源的权限。更多信息,请参见管理RAM用户的权限。
该自定义权限策略允许用户启动、停止、获取和列出Cloud Shell会话,并且可以创建、修改、删除和管理ECS实例及其相关资源。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "cloudshell:StartSession", "cloudshell:StopSession", "cloudshell:GetSession", "cloudshell:ListSessions", "ecs:CreateInstance", "ecs:RunInstances", "ecs:StartInstance", "ecs:StopInstance", "ecs:RebootInstance", "ecs:TerminateInstance", "ecs:ModifyInstanceAttribute", "ecs:DescribeInstances", "ecs:AllocatePublicIpAddress", "ecs:AssignPrivateIpAddresses", "ecs:UnassignPrivateIpAddresses", "ecs:ModifyInstanceVpcAttribute", "ecs:ResetInstance", "ecs:AttachKeyPair", "ecs:DetachKeyPair", "ecs:CreateSecurityGroup", "ecs:DeleteSecurityGroup", "ecs:AuthorizeSecurityGroup", "ecs:RevokeSecurityGroup", "ecs:CreateVpc", "ecs:DeleteVpc", "ecs:CreateVSwitch", "ecs:DeleteVSwitch", "ecs:CreateDisk", "ecs:DeleteDisk", "ecs:AttachDisk", "ecs:DetachDisk", "ecs:CreateSnapshot", "ecs:DeleteSnapshot", "ecs:CreateImage", "ecs:DeleteImage" ], "Resource": "*" } ] }
使用的资源
alicloud_vpc:用户自定义的虚拟网络环境,用户可以在其中启动阿里云资源。
alicloud_vswitch:是在VPC内定义的逻辑交换机,用于将流量转发到VPC内的不同部分。
alicloud_security_group:安全组是用于控制网络流量进出云服务器的安全策略集合。
alicloud_instance:用户可以按需创建、管理和销毁ECS实例,以满足计算需求。
alicloud_security_group_rule:安全组规则定义了允许通过安全组的流量类型。
操作步骤
启动云命令行
在启动云命令行之前,您需要使用已授予权限的可用RAM身份登录阿里云主页。云命令行将使用当前登录态为您自动配置身份凭证,无需手动操作。为避免自动配置凭证功能失效,建议您在操作时避免对初始配置文件default进行修改或删除。
您可以通过以下方式启动云命令行:
在控制台中运行
单击控制台首页顶部导航栏中的云命令行按钮,启动云命令行。
独立运行
在浏览器中输入https://shell.aliyun.com,打开云命令行操作界面。
云命令行启动时会为您创建一台虚拟机,这个过程将消耗一些时间,最长不超过30秒。
您可以根据实际需要打开多个云命令行窗口,最多可同时打开5个云命令行窗口。多个云命令行窗口会连接到同一台虚拟机,虚拟机数量不会因为您打开新的命令行窗口而增加。
更多信息,请参见什么是云命令行。
创建云资源
在Cloud Shell中编写Terraform模板。
您可以使用
vim命令直接编写模板。执行如下命令创建一个工程目录及模板文件:
mkdir terraform-project cd terraform-project touch main.tf以下代码示例是一个创建ECS实例的Terraform模板,请将内容粘贴到
main.tf中。Cloud Shell 可自动获取登录账号的身份认证信息,无需额外设置环境变量。provider "alicloud" { region = var.region_id } variable "region_id" { default = "cn-shanghai" } variable "available_disk_category" { default = "cloud_efficiency" } variable "available_resource_creation" { default = "VSwitch" } variable "vpc_name" { default = "tf_test_fofo" } variable "vpc_cidr_block" { default = "172.16.0.0/12" } variable "vswitch_cidr_block" { default = "172.16.0.0/21" } variable "security_group_name" { default = "default" } variable "instance_type" { default = "ecs.n4.large" } variable "image_id" { default = "ubuntu_18_04_64_20G_alibase_20190624.vhd" } variable "instance_name" { default = "test_fofo" } variable "internet_max_bandwidth_out" { default = 10 } variable "port_range" { default = "1/65535" } variable "priority" { default = 1 } variable "cidr_ip" { default = "0.0.0.0/0" } data "alicloud_zones" "default" { available_disk_category = var.available_disk_category available_resource_creation = var.available_resource_creation } resource "alicloud_vpc" "vpc" { vpc_name = var.vpc_name cidr_block = var.vpc_cidr_block } resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = var.vswitch_cidr_block zone_id = data.alicloud_zones.default.zones[0].id } resource "alicloud_security_group" "default" { name = var.security_group_name vpc_id = alicloud_vpc.vpc.id } resource "alicloud_instance" "instance" { availability_zone = data.alicloud_zones.default.zones[0].id security_groups = [alicloud_security_group.default.id] instance_type = var.instance_type system_disk_category = var.available_disk_category image_id = var.image_id instance_name = var.instance_name vswitch_id = alicloud_vswitch.vsw.id internet_max_bandwidth_out = var.internet_max_bandwidth_out } resource "alicloud_security_group_rule" "allow_all_tcp" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = var.port_range priority = var.priority security_group_id = alicloud_security_group.default.id cidr_ip = var.cidr_ip }执行
init命令初始化Terraform。terraform init预期结果:
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.执行
apply命令创建ECS实例,在执行过程中,根据提示输入yes并按下Enter键,等待命令执行完成,若出现以下信息,则表示创建抢占式实例完成。terraform apply预期结果:
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
操作验证
执行terraform show命令
您可以使用以下命令查询Terraform已创建的资源详细信息:
terraform show# alicloud_instance.instance:
resource "alicloud_instance" "instance" {
availability_zone = "cn-shanghai-a"
cpu = 2
create_time = "2024-11-13T08:52Z"
deletion_protection = false
dry_run = false
enable_jumbo_frame = false
expired_time = "2099-12-31T15:59Z"
host_name = "iZuxxx"
http_put_response_hop_limit = 0
id = "i-ufxxx"
image_id = "ubuntu_18_04_64_20G_alibase_xxx vhd"
instance_charge_type = "PostPaid"
instance_name = "test_fofo"
instance_type = "ecs.n4.large"
internet_charge_type = "PayByTraffic"
internet_max_bandwidth_in = 500
internet_max_bandwidth_out = 10
ipv6_address_count = 0
ipv6_addresses = []
maintenance_action = "AutoRecover"
maintenance_notify = false
memory = 4096
network_interface_id = "eni-uf6ixxx"
network_interface_traffic_mode = "Standard"
os_name = "Ubuntu 18.04 64位"
os_type = "linux"
primary_ip_address = "xxx"
private_ip = "xxx"
public_ip = "xxx"
queue_pair_number = 0云服务器 ECS控制台截图
登录云服务器ECS控制台,查看创建实例。
在左侧导航栏单击实例,进入实例列表页面,确认目标实例的实例 ID 及运行状态。
清理资源
当您不再需要上述通过terraform创建或管理的资源时,运行下面的命令释放资源。关于terraform destroy的更多信息,请参见Terraform常用命令。
terraform destroy完整示例
当前示例代码支持一键运行,您可以直接运行代码。一键运行
示例代码
切换 Terraform 版本
Cloudshell 中默认的 Terraform 版本为 0.12.31,如果需要更高的版本可以通过tfenv来切换。
查看Cloud Shell中内置的Terraform版本。
tfenv list切换到需要的Terraform版本。
tfenv use <terraform_version>相关文档
Terrafrom介绍,请参见了解阿里云Terraform。
当您遇到由于网络延迟等原因造成的 terraform init 超时,导致无法正常下载 Provider 等情况时,请参见Terraform Init 加速方案配置。
关于如何配置Terraform的身份认证信息,请参见身份认证。