日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

通過Terraform創建彈性伸縮資源

本文為您介紹在首次使用ESS時,如何通過Terraform開通創建伸縮組、伸縮配置、伸縮規則等彈性伸縮資源。

說明

本教程所含示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行

前提條件

  • 由于阿里云賬號(主賬號)具有資源的所有權限,一旦發生泄露將面臨重大風險。建議您使用RAM用戶,并為該RAM用戶創建AccessKey,具體操作方式請參見創建RAM用戶創建AccessKey

  • 通過RAM授權,阿里云用戶可以有效地管理其云資源訪問權限,適應多用戶協同工作的需求,并且能夠按需為用戶分配最小權限,避免權限過大導致的安全漏洞。具體操作方式請參見為RAM用戶授權

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "ecs:CreateSecurityGroup",
                    "ecs:ModifySecurityGroupPolicy",
                    "ecs:DescribeSecurityGroups",
                    "ecs:ListTagResources",
                    "ecs:DeleteSecurityGroup",
                    "ecs:DescribeSecurityGroupAttribute",
                    "ecs:AuthorizeSecurityGroup",
                    "ecs:RevokeSecurityGroup"
                ],
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "vpc:DescribeVpcAttribute",
                    "vpc:DescribeRouteTableList",
                    "vpc:DescribeVSwitchAttributes",
                    "vpc:DeleteVpc",
                    "vpc:DeleteVSwitch",
                    "vpc:CreateVpc",
                    "vpc:CreateVSwitch"
                ],
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "ess:CreateScalingGroup",
                    "ess:DescribeScalingGroups",
                    "ess:ListTagResources",
                    "ess:DeleteScalingGroup",
                    "ess:ModifyScalingGroup",
                    "ess:CreateScalingConfiguration",
                    "ess:DescribeScalingConfigurations",
                    "ess:ModifyScalingConfiguration",
                    "ess:CreateScalingRule",
                    "ess:DescribeScalingRules",
                    "ess:DeleteScalingRule"
                ],
                "Resource": "*"
            }
        ]
    }
  • 準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。

    • Terraform Explorer中使用Terraform:阿里云提供了Terraform的在線運行環境,您無需安裝Terraform,登錄后即可在線使用和體驗Terraform。適用于零成本、快速、便捷地體驗和調試Terraform的場景。

    • Cloud Shell:阿里云Cloud Shell中預裝了Terraform的組件,并已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用于低成本、快速、便捷地訪問和使用Terraform的場景。

    • 在本地安裝和配置Terraform:適用于網絡連接較差或需要自定義開發環境的場景。

使用的資源

使用Terraform創建彈性伸縮資源

本示例將創建伸縮組、伸縮配置和伸縮規則等彈性伸縮資源

  1. 創建一個工作目錄,并在該工作目錄中創建名為main.tf的配置文件,然后將以下代碼復制到main.tf中。

    variable "region" {
      default = "cn-heyuan"
    }
    
    variable "instance_type" {
      default = "ecs.hfc7.xlarge"
    }
    
    variable "image_id" {
      default = "aliyun_2_1903_x64_20G_alibase_20210120.vhd"
    }
    
    variable "zone_id" {
      default = "cn-heyuan-b"
    }
    
    provider "alicloud" {
      region = var.region
    }
    
    #創建專有網絡VPC。
    resource "alicloud_vpc" "vpc" {
      vpc_name   = "tf-test-vpc-wjt"
      cidr_block = "172.16.0.0/12" #規劃專有網絡VPC的私網網段。
    }
    
    #創建專有網絡交換機。
    resource "alicloud_vswitch" "vsw" {
      vpc_id       = alicloud_vpc.vpc.id
      cidr_block   = "172.16.0.0/21" #規劃交換機的私網網段。
      zone_id      = var.zone_id     #選擇交換機的可用區。
      vswitch_name = "tf-test-vswitch-wjt"
    }
    
    #創建一個安全組。
    resource "alicloud_security_group" "security" {
      name        = "tf_test_security"
      description = "New security group"
      vpc_id      = alicloud_vpc.vpc.id
    }
    
    #并為該安全組添加一個允許任何地址訪問的安全組規則。
    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.security.id
      cidr_ip           = "0.0.0.0/0"
    }
    
    #本示例以創建最大實例數為100的伸縮組為例。
    resource "alicloud_ess_scaling_group" "group" {
      scaling_group_name = "tf_test_scalinggroup"
      min_size           = 0
      max_size           = 100
      vswitch_ids        = [alicloud_vswitch.vsw.id]
    }
    
    #本示例以創建ECS類型的伸縮配置為例。
    resource "alicloud_ess_scaling_configuration" "configuration" {
      scaling_group_id           = alicloud_ess_scaling_group.group.id
      instance_type              = var.instance_type
      image_id                   = var.image_id
      security_group_id          = alicloud_security_group.security.id
      scaling_configuration_name = "tf_test_scalingconfiguration"
      system_disk_category       = "cloud_essd"
      spot_strategy              = "SpotWithPriceLimit"
      active                     = true
      force_delete               = true
    }
    
    #創建伸縮規則
    resource "alicloud_ess_scaling_rule" "rule" {
      scaling_group_id = alicloud_ess_scaling_group.group.id
      adjustment_type  = "QuantityChangeInCapacity"
      adjustment_value = 1
    }
  2. 執行以下命令,初始化Terraform運行環境。

    terraform init

    返回如下信息,表示Terraform初始化成功。

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    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.
  3. 創建執行計劃,并預覽變更。

    terraform plan
  4. 執行以下命令,創建資源。

    terraform apply

    在執行過程中,根據提示輸入yes并按下Enter鍵,等待命令執行完成,若出現以下信息,則表示彈性伸縮資源創建成功。

    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_security_group_rule.allow_all_tcp: Creation complete after 0s [id=sg-f8z1mes******:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
    alicloud_vswitch.vsw: Creation complete after 3s [id=vsw-f8ztgc4******]
    alicloud_ess_scaling_group.group: Creating...
    alicloud_ess_scaling_group.group: Creation complete after 2s [id=asg-f8z9mo******]
    ...
    
    Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
  5. 驗證結果

    執行terraform show命令

    您可以使用以下命令查詢Terraform已創建的資源詳細信息:

    terraform show

    image

    登錄彈性伸縮管理控制臺

    登錄彈性伸縮管理控制臺,查看已創建的彈性伸縮組。

    image

清理資源

當您不再需要上述通過Terraform創建或管理的資源時,請運行以下命令以釋放資源。關于terraform destroy的更多信息,請參見Terraform常用命令

terraform destroy

完整示例

說明

當前示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行

示例代碼

variable "region" {
  default = "cn-heyuan"
}

variable "instance_type" {
  default = "ecs.hfc7.xlarge"
}

variable "image_id" {
  default = "aliyun_2_1903_x64_20G_alibase_20210120.vhd"
}

variable "zone_id" {
  default = "cn-heyuan-b"
}

provider "alicloud" {
  region = var.region
}

#創建專有網絡VPC。
resource "alicloud_vpc" "vpc" {
  vpc_name   = "tf-test-vpc-wjt"
  cidr_block = "172.16.0.0/12" #規劃專有網絡VPC的私網網段。
}

#創建專有網絡交換機。
resource "alicloud_vswitch" "vsw" {
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = "172.16.0.0/21" #規劃交換機的私網網段。
  zone_id      = var.zone_id     #選擇交換機的可用區。
  vswitch_name = "tf-test-vswitch-wjt"
}

#創建一個安全組。
resource "alicloud_security_group" "security" {
  name        = "tf_test_security"
  description = "New security group"
  vpc_id      = alicloud_vpc.vpc.id
}

#并為該安全組添加一個允許任何地址訪問的安全組規則。
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.security.id
  cidr_ip           = "0.0.0.0/0"
}

#本示例以創建最大實例數為100的伸縮組為例。
resource "alicloud_ess_scaling_group" "group" {
  scaling_group_name = "tf_test_scalinggroup"
  min_size           = 0
  max_size           = 100
  vswitch_ids        = [alicloud_vswitch.vsw.id]
}

#本示例以創建ECS類型的伸縮配置為例。
resource "alicloud_ess_scaling_configuration" "configuration" {
  scaling_group_id           = alicloud_ess_scaling_group.group.id
  instance_type              = var.instance_type
  image_id                   = var.image_id
  security_group_id          = alicloud_security_group.security.id
  scaling_configuration_name = "tf_test_scalingconfiguration"
  system_disk_category       = "cloud_essd"
  spot_strategy              = "SpotWithPriceLimit"
  active                     = true
  force_delete               = true
}

#創建伸縮規則
resource "alicloud_ess_scaling_rule" "rule" {
  scaling_group_id = alicloud_ess_scaling_group.group.id
  adjustment_type  = "QuantityChangeInCapacity"
  adjustment_value = 1
}