{Alibaba ECS} インスタンスタイプの変更

 


https://www.alibabacloud.com/help/ja/ecs/user-guide/overview-of-instance-configuration-changes

 

 

-- 1. VPC、VSwitch作成

 

cat <<-'EOF' > variables.tf

locals {
  availability_zone1 = "ap-northeast-1a"
}

 

EOF

 

cat <<-'EOF' > main.tf

terraform {
  required_version = ">= 1.0.0, < 2.0.0"
  required_providers {
    alicloud = {
       source  = "aliyun/alicloud"
       version = "= 1.217.0"
    }
  }
}

provider "alicloud" {
    region  = "ap-northeast-1"
}


resource "alicloud_vpc" "vpc01" {
  vpc_name          = "vpc01"
  description       = "vpc01"
  cidr_block        = "10.0.0.0/16"
}

 

resource "alicloud_vswitch" "sw01" {
  vswitch_name      = "sw01"
  description       = "sw01"
  vpc_id            = alicloud_vpc.vpc01.id
  cidr_block        = "10.0.1.0/24"
  zone_id           = local.availability_zone1
}


resource "alicloud_security_group" "sg01" {
  name                = "sg01"
  description         = "sg01"
  vpc_id              = alicloud_vpc.vpc01.id
  security_group_type = "normal"
}


resource "alicloud_security_group_rule" "sg0101" {
  type              = "ingress"
  ip_protocol       = "tcp"
  port_range        = "22/22"
  security_group_id = alicloud_security_group.sg01.id
  nic_type          = "intranet"
  policy            = "accept"
  priority          = 10
  cidr_ip           = "0.0.0.0/0"
  description       = "sg0101"
}

resource "alicloud_security_group_rule" "sg0102" {
  type              = "ingress"
  ip_protocol       = "icmp"
  port_range        = "-1/-1"
  security_group_id = alicloud_security_group.sg01.id
  nic_type          = "intranet"
  policy            = "accept"
  priority          = 10
  cidr_ip           = "10.0.1.0/24"
  description       = "sg0102"
}

 

 

resource "alicloud_vpc_ipv4_gateway" "gw01" {
  ipv4_gateway_name        = "gw01"
  ipv4_gateway_description = "gw01"
  vpc_id                   = alicloud_vpc.vpc01.id
  enabled                  = true
}

 

 

# ルートテーブルへvSwitchの関連付け
resource "alicloud_route_table_attachment" "sw01_rt01" {
  vswitch_id     = alicloud_vswitch.sw01.id
  route_table_id = alicloud_vpc.vpc01.route_table_id
}


# ルートテーブルへカスタムルート追加
resource "alicloud_route_entry" "rt0101" {
  name                  = "rt0101"
  route_table_id        = alicloud_vpc.vpc01.route_table_id
  destination_cidrblock = "0.0.0.0/0"
  nexthop_type          = "Ipv4Gateway"
  nexthop_id            = alicloud_vpc_ipv4_gateway.gw01.id
}

 

resource "alicloud_eip_address" "eip01" {
  address_name              = "eip01"
  bandwidth                 = 1
  deletion_protection       = false
  description               = "eip01"
  internet_charge_type      = "PayByTraffic"
  isp                       = "BGP"
  payment_type              = "PayAsYouGo"
  netmode                   = "public"
}

 

EOF

 

cat <<-'EOF' > outputs.tf

 

output "vpc01_id" {
  value = alicloud_vpc.vpc01.id
  description = "vpc01.id"
}

output "vpc01_route_table_id" {
  value = alicloud_vpc.vpc01.route_table_id
  description = "vpc01.route_table_id"
}

output "sw01_id" {
  value = alicloud_vswitch.sw01.id
  description = "sw01.id"
}

output "sg01_id" {
  value = alicloud_security_group.sg01.id
  description = "sg01.id"
}

output "gw01_id" {
  value = alicloud_vpc_ipv4_gateway.gw01.id
  description = "gw01.id"
}

 

output "eip01_id" {
  value = alicloud_eip_address.eip01.id
  description = "eip01.id"
}

 

EOF

 


terraform init
terraform fmt
terraform -version

 

terraform plan

 

terraform apply -auto-approve


# terraform destroy -auto-approve

 

 


-- 2. ECSインスタンスの作成

aliyun ecs DescribeInstances 


aliyun ecs DescribeInstances \
| jq ".Instances.Instance | [.InstanceId, .InstanceName, .Status]"


aliyun ecs CreateInstance \
--InstanceType ecs.e-c4m1.large \
--CreditSpecification Standard \
--DeletionProtection false \
--Description instance01 \
--HostName instance01 \
--ImageId aliyun_2_1903_x64_20G_alibase_20231221.vhd \
--InstanceChargeType PostPaid \
--InstanceName instance01 \
--KeyPairName alibabakey01 \
--SecurityGroupId sg-11111111111111111111 \
--SystemDisk.Category cloud_essd \
--SystemDisk.Size 20 \
--VSwitchId vsw-111111111111111111111 \
--ZoneId ap-northeast-1a 


ECSインスタンスの起動
aliyun ecs StartInstance \
--InstanceId i-11111111111111111111 


ECSインスタンスの削除
aliyun ecs DeleteInstance \
--InstanceId i-11111111111111111111 \
--Force true 

 


-- 3. EIP割り当て

aliyun vpc DescribeEipAddresses 

aliyun vpc AssociateEipAddress \
--AllocationId eip-111111111111111111111 \
--InstanceId i-11111111111111111111 \
--InstanceType EcsInstance


EIP割り当て解除
aliyun vpc UnassociateEipAddress \
--AllocationId eip-111111111111111111111 \
--InstanceId i-11111111111111111111 \
--InstanceType EcsInstance 

 

-- 4. インスタンスタイプの変更

aliyun ecs DescribeInstanceTypeFamilies 

aliyun ecs DescribeInstanceTypes 

aliyun ecs DescribeInstanceTypes \
| jq -c '.InstanceTypes.InstanceType | [ .InstanceTypeId, .CpuCoreCount, .MemorySize, .CpuArchitecture ]' 


aliyun ecs DescribeInstances \
| jq ".Instances.Instance[] | [.InstanceId, .InstanceName, .Status]"

 

※事前にインスタンス停止とEIP割り当て解除必要


EIP割り当て解除
aliyun vpc UnassociateEipAddress \
--AllocationId eip-111111111111111111111 \
--InstanceId i-11111111111111111111 \
--InstanceType EcsInstance 


ECSインスタンスの停止
aliyun ecs StopInstance \
--InstanceId i-11111111111111111111 \
--StoppedMode StopCharging 

 

 

aliyun ecs ModifyInstanceSpec \
--InstanceId i-11111111111111111111 \
--InstanceType ecs.e-c2m1.large 


ECSインスタンスの起動
aliyun ecs StartInstance \
--InstanceId i-11111111111111111111 


EIP割り当て
aliyun vpc AssociateEipAddress \
--AllocationId eip-111111111111111111111 \
--InstanceId i-11111111111111111111 \
--InstanceType EcsInstance