4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Alibaba Cloud] VPC + ECS作成ハンズオン

Last updated at Posted at 2019-05-22

目標

主だったコマンドを使ってみる

サービスごとに[ Create ]から[ Delete ]まで複数のコマンドが存在しています。
さらにサービスの利用には他のサービスのリソースが必要でだったりと、
CLIを使うことはサービスの理解に繋がっているとも考えられます。
各種のサービスをコマンドを使いながらECSが起動するところまでやっていきましょう。

同じ内容のものを Terraform で作ったものが下記の URL にありますので、こちらもご参照ください

事前準備

[Alibaba Cloud] CLI の始め方

構成図

image.png

[作成していく順番]

目次
1. VPCの作成
   1-1. 環境変数の設定
   1-2. VPCの作成
   1-3. VPCの確認

2. VSwitchの作成
   2-1. 環境変数の設定
   2-2. VSwitchの作成
   2-3. VSwitchの確認

3. EIPの作成
   3-1. 環境変数の設定
   3-2. EIPの作成
   3-3. EIPの確認

4. SecurityGroupの作成
   4-1. 環境変数の設定
   4-2. SecurityGroupの作成
   4-3. SecurityGroupの確認

5. SecurityGroupの受信ルール追加
   5-1. 環境変数の設定
   5-2. SecurityGroupのルール関連付け
   5-3. SecurityGroupの確認

6. Instance(ECS)の作成
   6-1:a. データの用意
   6-1:b. 環境変数の設定
   6-2. Instanceの作成
   6-3. Instanceの確認

7. InstanceとEIPの紐付け
   7-1.環境変数の設定
   7-2. InstanceとEIPの紐付け
   7-3. 紐付けの確認

8. インスタンスの起動
   8-1.環境変数の設定
   8-2. Instanceの起動
   8-3. 起動確認

9. 画面確認
   9-1.環境変数の設定
   9-2. curlで確認
   9-2:おまけ. ブラウザでも確認
10.後片付け
   10-1.環境変数の設定

   10-2:a. Instanceの停止
   10-2:b. Instanceの確認
   10-2:c. Instanceの削除
   10-2:d. Instanceの確認

   10-3:a SecurityGroupの削除
   10-3:b SecurityGroupの確認

   10-4:a. EIPの確認
   10-4:b. EIPの削除

   10-5:a. VSwitchの削除
   10-5:b. VSwitchの確認

   10-6:a. VPCの削除
   10-6:b. VPCの確認

1. VPCの作成

VPC とは?

Alibaba Cloudに設置されたプライベートネットワーク。
VPCはAlibaba Cloudの他の仮想ネットワークと論理的に分離されており、
プライベートCIDRブロック、VRouter、及びVSwitchで構成されます。

VPCのドキュメントはこちら

https://jp.alibabacloud.com/help/doc-detail/34217.htm?spm=a21mg.p38356.b99.3.ba06354cEh1JZq

1-1.環境変数の設定

設定
VPC_NAME="VPC"
VPC_DESC="Aliyun VPC"
VPC_CIDR_BLOCK="172.16.0.0/16"
REGION_ID="ap-northeast-1"
確認
cat << EOF

  REGION_ID="ap-northeast-1"
  REGION_ID="${REGION_ID}"

  VPC_NAME="VPC"
  VPC_NAME="${VPC_NAME}"

  VPC_DESC="Aliyun VPC"
  VPC_DESC="${VPC_DESC}"

  VPC_CIDR_BLOCK="172.16.0.0/16"
  VPC_CIDR_BLOCK="${VPC_CIDR_BLOCK}"

EOF

1-2. VPCの作成

https://jp.alibabacloud.com/help/doc-detail/35737.htm?spm=a21mg.p38356.b99.55.715731c7Rs4pFJ

1.jpg
CreateVpc
aliyun vpc CreateVpc \
  --VpcName ${VPC_NAME} \
  --Description "${VPC_DESC}" \
  --CidrBlock ${VPC_CIDR}
応答
{
        "RequestId": "003639D9-3078-468B-80D7-79091989722D",
        "ResourceGroupId": "rg-XXXXXXXXXXXXXXX",
        "RouteTableId": "vtb-XXXXXXXXXXXXXXXXXXXXX",
        "VpcId": "vpc-XXXXXXXXXXXXXXXXXXXXX",
        "VRouterId": "vrt-XXXXXXXXXXXXXXXXXXXXX"
}

1-3. VPCの確認

https://jp.alibabacloud.com/help/doc-detail/35739.htm?spm=a21mg.p38356.b99.57.1eaa457brARskO

2.jpg
DescribeVpcs
aliyun vpc DescribeVpcs \
  --VpcName ${VPC_NAME}
応答
{
	"PageNumber": 1,
	"Vpcs": {
		"Vpc": [
			{
				"VpcName": "VPC",
				"Description": "Aliyun VPC",
				"IsDefault": false,
				"NatGatewayIds": {
					"NatGatewayIds": []
				},
				"ResourceGroupId": "rg-XXXXXXXXXXXXXXX",
				"UserCidrs": {
					"UserCidr": []
				},
				"NetworkAclNum": 0,
				"RouterTableIds": {
					"RouterTableIds": [
						"vtb-XXXXXXXXXXXXXXXXXXXXX"
					]
				},
				"VpcId": "vpc-XXXXXXXXXXXXXXXXXXXXX",
				"VRouterId": "vrt-XXXXXXXXXXXXXXXXXXXXX",
				"CreationTime": "2019-05-11T06:19:28Z",
				"Status": "Available",
				"CidrBlock": "172.16.0.0/16",
				"VSwitchIds": {
					"VSwitchId": []
				},
				"RegionId": "ap-northeast-1",
				"Ipv6CidrBlock": ""
			}
		]
	},
	"TotalCount": 1,
	"PageSize": 10,
	"RequestId": "003639D9-3078-468B-80D7-79091989722D"
}
3.jpg
  1. VSwitchの作成

VSwitch とは?

VSwitchはVPCネットワーク内の基本的なネットワークモジュールで、
VPC内の様々なクラウド製品のインスタンスへ接続するに使用される。

VSwitch は AWS で言うところの Subnet

VSwitchのドキュメントはこちら

https://jp.alibabacloud.com/help/doc-detail/65387.htm?spm=a21mg.p38356.b99.15.2bd33278HB4Rdb

2-1.環境変数の設定

設定
VPC_ID=$( \
  aliyun vpc DescribeVpcs \
    --VpcName ${VPC_NAME} \
    | jq '.Vpcs.Vpc[].VpcId' \
    | sed 's/"//g'
)
ZONE_ID="ap-northeast-1a"
VSWITCH_NAME="VSw"
VSWITCH_DESC="Aliyun VSwitch"
VSWITCH_CIDR="172.16.1.0/24"
確認
cat << EOF

  VPC_ID="vpc-XXXXXXXXXXXXXXXXXXXXX"
  VPC_ID="${VPC_ID}"

  ZONE_ID="ap-northeast-1a"
  ZONE_ID="${ZONE_ID}"

  VSWITCH_NAME="VSw"
  VSWITCH_NAME="${VSWITCH_NAME}"

  VSWITCH_DESC="Aliyun VSwitch"
  VSWITCH_DESC="${VSWITCH_DESC}"

  VSWITCH_CIDR="172.16.1.0/24"
  VSWITCH_CIDR="${VSWITCH_CIDR}"

EOF

2-2.VSwitchの作成

https://jp.alibabacloud.com/help/doc-detail/35745.htm?spm=a21mg.p38356.b99.60.74bd34812ACAMj

4.jpg
CreateVSwitch
aliyun vpc CreateVSwitch \
  --VpcId ${VPC_ID} \
  --ZoneId ${ZONE_ID} \
  --VSwitchName ${VSWITCH_NAME} \
  --Description "${VPC_DESC}" \
  --CidrBlock ${VSWITCH_CIDR}
応答
{
	"RequestId": "4A295D33-A5A2-40E5-96B8-FD680148EEEE",
	"VSwitchId": "vsw-XXXXXXXXXXXXXXXXXXXXX"
}

2-3.VSwitchの確認

https://jp.alibabacloud.com/help/doc-detail/35748.htm?spm=a21mg.p38356.b99.62.15103278Eb0FXc

5.jpg
DescribeVSwitches
aliyun vpc DescribeVSwitches \
  --VSwitchName ${VSWITCH_NAME} | jq .
応答
{
	"PageNumber": 1,
	"VSwitches": {
		"VSwitch": [
			{
				"RouteTable": {
					"RouteTableId": "vtb-XXXXXXXXXXXXXXXXXXXXX",
					"RouteTableType": "System"
				},
				"Description": "Aliyun VPC",
				"IsDefault": false,
				"AvailableIpAddressCount": 252,
				"ResourceGroupId": "rg-XXXXXXXXXXXXXXX",
				"ZoneId": "ap-northeast-1a",
				"VSwitchId": "vsw-XXXXXXXXXXXXXXXXXXXXX",
				"NetworkAclId": "",
				"VpcId": "vpc-XXXXXXXXXXXXXXXXXXXXX",
				"CreationTime": "2019-05-11T13:24:43Z",
				"Status": "Available",
				"CidrBlock": "172.16.1.0/24",
				"VSwitchName": "VSw",
				"Ipv6CidrBlock": ""
			}
		]
	},
	"TotalCount": 1,
	"PageSize": 10,
	"RequestId": "4A295D33-A5A2-40E5-96B8-FD680148EEEE"
}
6.jpg

3. EIPの関連付け

EIP とは?

個別に購入できるパブリック IP アドレスリソース。
VPCネットワーク の ECS、SLB インスタンスや、NATゲートウェイに EIP をバインドできる。

EIPのドキュメントはこちら

https://jp.alibabacloud.com/help/doc-detail/32321.htm?spm=a21mg.l28256.a3.2.541b483fdcq9wC

3-1.環境変数の設定

設定
EIP_BANDWIDTH="1"
EIP_INTERNET_CHARGE_TYPE="PayByTraffic"
EIP_INSTANCE_CHARGE_TYPE="PostPaid"
確認
cat << EOF

  EIP_BANDWIDTH="1"
  EIP_BANDWIDTH="${EIP_BANDWIDTH}"

  EIP_INTERNET_CHARGE_TYPE="PayByTraffic"
  EIP_INTERNET_CHARGE_TYPE="${EIP_INTERNET_CHARGE_TYPE}"

  EIP_INSTANCE_CHARGE_TYPE="PostPaid"
  EIP_INSTANCE_CHARGE_TYPE="${EIP_INSTANCE_CHARGE_TYPE}"

EOF

3-2.EIPの作成

https://jp.alibabacloud.com/help/doc-detail/36016.htm?spm=a21mg.p38356.b99.137.38214153UlZJRz

7.jpg
AllocateEipAddress
aliyun vpc AllocateEipAddress \
  --Bandwidth ${EIP_BANDWIDTH} \
  --InternetChargeType ${EIP_INTERNET_CHARGE_TYPE} \
  --InstanceChargeType ${EIP_INSTANCE_CHARGE_TYPE} | jq .
応答
{
	"ResourceGroupId": "rg-XXXXXXXXXXXXXXX",
	"RequestId": "F175D5ED-AB55-4D48-8B13-97E318D97187",
	"EipAddress": "XXX.XXX.XXX.XXX",
	"AllocationId": "eip-XXXXXXXXXXXXXXXXXXXXX"
}

3-3.EIPの確認

https://jp.alibabacloud.com/help/doc-detail/36018.htm?spm=a21mg.p38356.b99.142.2757701addDjwM

8.jpg
DescribeEipAddresses
aliyun vpc DescribeEipAddresses | jq .
応答
{
	"PageNumber": 1,
	"EipAddresses": {
		"EipAddress": [
			{
				"BandwidthPackageBandwidth": "",
				"HasReservationData": false,
				"HDMonitorStatus": "OFF",
				"InstanceId": "",
				"ISP": "BGP",
				"InternetChargeType": "PayByTraffic",
				"BandwidthPackageType": "",
				"PrivateIpAddress": "",
				"SecondLimited": false,
				"Bandwidth": "1",
				"ChargeType": "PostPaid",
				"ResourceGroupId": "rg-XXXXXXXXXXXXXXX",
				"AllocationTime": "2019-05-11T13:31:40Z",
				"Descritpion": "",
				"IpAddress": "XXX.XXX.XXX.XXX",
				"AllocationId": "eip-XXXXXXXXXXXXXXXXXXXXX",
				"Mode": "NAT",
				"InstanceType": "",
				"Name": "",
				"Status": "Available",
				"InstanceRegionId": "",
				"BandwidthPackageId": "",
				"RegionId": "ap-northeast-1",
				"OperationLocks": {
					"LockReason": []
				},
				"AvailableRegions": {
					"AvailableRegion": [
						"ap-northeast-1"
					]
				},
				"ExpiredTime": ""
			}
		]
	},
	"TotalCount": 1,
	"PageSize": 10,
	"RequestId": "F175D5ED-AB55-4D48-8B13-97E318D97187"
}

ポイント:
この作成した EIP を使い画面を表示します

9.jpg

4. SecurityGroupの作成

SecurityGroup とは?

同じセキュリティ要件と相互信頼を持つ同じリージョンのインスタンスをグループ化する論理グループ。
セキュリティの分離の重要な手段として、セキュリティグループは
クラウド上のセキュリティドメインを分割するために使用される。

SecurityGroupのドキュメントはこちら

https://jp.alibabacloud.com/help/doc-detail/25387.htm?spm=a21mg.p38356.b99.129.5dda2f99bpdh1N

4-1.環境変数の設定

設定
VPC_ID=$( \
  aliyun vpc DescribeVpcs \
  --VpcName ${VPC_NAME} \
  | jq '.Vpcs.Vpc[].VpcId' \
  | sed 's/"//g'
)
ECS_SG_NAME="SG"
ECS_SG_DESC="Aliyun SG"
確認
cat << EOF

  VPC_ID="vpc-XXXXXXXXXXXXXXXXXXXXX"
  VPC_ID="${VPC_ID}"

  ECS_SG_NAME="SG"
  ECS_SG_NAME="${ECS_SG_NAME}"

  ECS_SG_DESC="Aliyun SG"
  ECS_SG_DESC="${ECS_SG_DESC}"

EOF

4-2.SecurityGroupの作成

https://jp.alibabacloud.com/help/doc-detail/25553.htm?spm=a21mg.l28256.b99.265.7fb958a7JwaeUe

10.jpg
CreateSecurityGroup
aliyun ecs CreateSecurityGroup \
  --VpcId ${VPC_ID} \
  --SecurityGroupName ${ECS_SG_NAME} \
  --Description "${ECS_SG_DESC}" | jq .
応答
{
	"SecurityGroupId": "sg-XXXXXXXXXXXXXXXXXXXX",
	"RequestId": "B07D892F-0100-46CB-8F32-E1BB36552447"
}

4-3.SecurityGroupの確認

https://jp.alibabacloud.com/help/doc-detail/25556.htm?spm=a21mg.p38356.b99.274.13af1e909EgZVT

11.jpg
DescribeSecurityGroups
aliyun ecs DescribeSecurityGroups \
  --SecurityGroupName ${ECS_SG_NAME} | jq .
応答
{
	"PageNumber": 1,
	"TotalCount": 1,
	"PageSize": 10,
	"RegionId": "ap-northeast-1",
	"RequestId": "C99C9DB4-4A38-4989-B488-6826D5C8AC3D",
	"SecurityGroups": {
		"SecurityGroup": [
			{
				"CreationTime": "2019-05-11T13:44:08Z",
				"Tags": {
					"Tag": []
				},
				"SecurityGroupId": "sg-XXXXXXXXXXXXXXXXXXXX",
				"SecurityGroupName": "SG",
				"Description": "Aliyun SG",
				"ResourceGroupId": "",
				"SecurityGroupType": "normal",
				"VpcId": "vpc-XXXXXXXXXXXXXXXXXXXXX"
			}
		]
	}
}
12.jpg

5. SecurityGroupのルールの追加

5-1.環境変数の設定

設定
SG_ID=$( \
  aliyun ecs DescribeSecurityGroups \
    --SecurityGroupName ${SG_GROUP_NAME} \
    | jq '.SecurityGroups.SecurityGroup[].SecurityGroupId' \
    | sed 's/"//g'
)
SG_DESC="Aliyun SG"
IP_PROTOCOL="tcp"
PORT_RANGE="80/80"
NIC_TYPE="intranet"
POLICY="accept"
DEST_CIDR_IP="0.0.0.0/0"
確認
cat << EOF

  SG_ID="sg-XXXXXXXXXXXXXXXXXXXX"
  SG_ID="${SG_ID}"

  SG_DESC="Aliyun SG"
  SG_DESC="${SG_DESC}"

  IP_PROTOCOL="tcp"
  IP_PROTOCOL="${IP_PROTOCOL}"

  PORT_RANGE="80/80"
  PORT_RANGE="${PORT_RANGE}"

  NIC_TYPE="intranet"
  NIC_TYPE="${NIC_TYPE}"

  POLICY="accept"
  POLICY="${POLICY}"

  DEST_CIDR_IP="0.0.0.0/0"
  DEST_CIDR_IP="${DEST_CIDR_IP}"

EOF

5-2.SecurityGroupの受信ルール追加

https://jp.alibabacloud.com/help/doc-detail/25554.htm?spm=a21mg.p38356.b99.266.71fc141cuulF0d

13.jpg
AuthorizeSecurityGroup
aliyun ecs AuthorizeSecurityGroup \
  --SecurityGroupId ${SG_ID} \
  --Description "${SG_DESC}" \
  --IpProtocol ${IP_PROTOCOL} \
  --PortRange ${PORT_RANGE} \
  --NicType ${NIC_TYPE} \
  --Policy ${POLICY} \
  --SourceCidrIp ${DEST_CIDR_IP} | jq .
応答
{"RequestId": "BE7592D8-3FB2-4EED-B3DB-67637EE00DD7"}

5-3.SecurityGroupのルール確認

https://jp.alibabacloud.com/help/doc-detail/25555.htm?spm=a21mg.p38356.b99.273.666c30cfq8eIJP

14.jpg
DescribeSecurityGroupAttribute
aliyun ecs DescribeSecurityGroupAttribute \
  --SecurityGroupId ${SG_ID} | jq .
応答
{
	"SecurityGroupId": "sg-XXXXXXXXXXXXXXXXXXXX",
	"InnerAccessPolicy": "Accept",
	"SecurityGroupName": "SG",
	"Description": "Aliyun SG",
	"RegionId": "ap-northeast-1",
	"RequestId": "BE7592D8-3FB2-4EED-B3DB-67637EE00DD7",
	"Permissions": {
		"Permission": [
			{
				"SourceCidrIp": "0.0.0.0/0",
				"Description": "Aliyun SG",
				"DestCidrIp": "",
				"NicType": "intranet",
				"DestGroupName": "",
				"PortRange": "80/80",
				"DestGroupId": "",
				"Ipv6DestCidrIp": "",
				"Direction": "ingress",
				"Priority": 1,
				"IpProtocol": "TCP",
				"SourcePortRange": "",
				"SourceGroupOwnerAccount": "",
				"Policy": "Accept",
				"CreateTime": "2019-05-11T13:54:06Z",
				"SourceGroupId": "",
				"DestGroupOwnerAccount": "",
				"Ipv6SourceCidrIp": "",
				"SourceGroupName": ""
			}
		]
	},
	"VpcId": "vpc-XXXXXXXXXXXXXXXXXXXXX"
}
15.jpg

6. Instance(ECS)の作成

ECS とは?

ECS インスタンスは、CPU、メモリ、およびその他の基本的な
コンピューティングコンポーネントを含む仮想コンピューティング環境。

ECSのドキュメントはこちら

https://jp.alibabacloud.com/help/doc-detail/25367.htm?spm=a21mg.l28256.a3.2.3da858a7KBNJv4

6-1:a. データの用意

今回は事前に Apache を起動する UserData を[Base64]にエンコードしたものを用意しました。

UserDataを用いる場合は公式で「平文のまま送信する」ことを推奨していないためです。

UserData.jpg

元データは以下の通りです。

Data
#!/bin/sh
yum install -y httpd
systemctl start httpd
systemctl enable httpd

6-1:b. 環境変数の設定

設定
INSTANCE_NAME="INSTANCE"
ECS_DESC="Alicloud CLI INSTANCE"
ZONE_ID="ap-northeast-1a"

VSWITCH_ID=$( \
  aliyun ecs DescribeVSwitches \
    --VpcId ${VPC_ID} \
    | jq ".VSwitches.VSwitch[].VSwitchId" \
    | sed 's/"//g'
)
SG_ID=$( \
  aliyun ecs DescribeSecurityGroups \
    --SecurityGroupName ${SG_GROUP_NAME} \
    | jq '.SecurityGroups.SecurityGroup[].SecurityGroupId' \
    | sed 's/"//g'
)

IMAGE_ID="alinux_17_01_64_20G_cloudinit_20171222.vhd"
INSTANCE_TYPE="ecs.n4.small"
INTERNET_CHARGE_TYPE="PayByTraffic"
INSTANCE_CHARGE_TYPE="PostPaid"

SYSTEM_DISK_CATEGORY="cloud_efficiency"
SYSTEM_DISK_SIZE=40
SYSTEM_DISK_NAME="SYS_DISK"
SYSTEM_DISK_DESC="AliCloud CLI System Disk"

USER_DATA="IyEvYmluL3NoDQp5dW0gaW5zdGFsbCAteSBodHRwZA0Kc3lzdGVtY3RsIHN0YXJ0IGh0dHBkDQpzeXN0ZW1jdGwgZW5hYmxlIGh0dHBk"
確認
cat << EOF

  INSTANCE_NAME="INSTANCE"
  INSTANCE_NAME="${INSTANCE_NAME}"

  ECS_DESC="Alicloud CLI INSTANCE"
  ECS_DESC="${ECS_DESC}"

  ZONE_ID="ap-northeast-1a"
  ZONE_ID="${ZONE_ID}"

  VSWITCH_ID="vsw-XXXXXXXXXXXXXXXXXXXXX"
  VSWITCH_ID="${VSWITCH_ID}"

  SG_ID="sg-XXXXXXXXXXXXXXXXXXXX"
  SG_ID="${SG_ID}"

  IMAGE_ID="alinux_17_01_64_20G_cloudinit_20171222.vhd"
  IMAGE_ID="${IMAGE_ID}"

  INSTANCE_TYPE="ecs.n4.small"
  INSTANCE_TYPE="${INSTANCE_TYPE}"

  INTERNET_CHARGE_TYPE="PayByTraffic"
  INTERNET_CHARGE_TYPE="${INTERNET_CHARGE_TYPE}"

  INSTANCE_CHARGE_TYPE="PostPaid"
  INSTANCE_CHARGE_TYPE="${INSTANCE_CHARGE_TYPE}"

  SYSTEM_DISK_CATEGORY="cloud_efficiency"
  SYSTEM_DISK_CATEGORY="${SYSTEM_DISK_CATEGORY}"

  SYSTEM_DISK_SIZE=40
  SYSTEM_DISK_SIZE=${SYSTEM_DISK_SIZE}

  SYSTEM_DISK_NAME="SYS_DISK"
  SYSTEM_DISK_NAME="${SYSTEM_DISK_NAME}"

  SYSTEM_DISK_DESC="AliCloud CLI System Disk"
  SYSTEM_DISK_DESC="${SYSTEM_DISK_DESC}"

  USER_DATA="IyEvYmluL3NoDQp5dW0gaW5zdGFsbCAteSBodHRwZA0Kc3lzdGVtY3RsIHN0YXJ0IGh0dHBkDQpzeXN0ZW1jdGwgZW5hYmxlIGh0dHBk"
  USER_DATA="${USER_DATA}"

EOF

6-2. Instanceの作成

https://jp.alibabacloud.com/help/doc-detail/25499.htm?spm=a21mg.p38356.b99.203.3ffa1cc86ZS4x6

16.jpg
CreateInstance
aliyun ecs CreateInstance \
  --InstanceName ${INSTANCE_NAME} \
  --Description "${ECS_DESC}" \
  --ZoneId ${ZONE_ID} \
  --VSwitchId ${VSWITCH_ID} \
  --SecurityGroupId ${SG_ID} \
  --ImageId ${IMAGE_ID} \
  --InstanceType ${INSTANCE_TYPE} \
  --InternetChargeType ${INTERNET_CHARGE_TYPE} \
  --SystemDisk.Category ${SYSTEM_DISK_CATEGORY} \
  --SystemDisk.Size ${SYSTEM_DISK_SIZE} \
  --SystemDisk.DiskName ${SYSTEM_DISK_NAME} \
  --SystemDisk.Description "${SYSTEM_DISK_DESC}" \
  --UserData ${USER_DATA} | jq .
応答
{
	"InstanceId": "i-XXXXXXXXXXXXXXXXXXXX",
	"RequestId": "673749DF-E556-4081-9CDA-A5EEE50CF51D"
}

6-3. Instanceの確認

https://jp.alibabacloud.com/help/doc-detail/25507.htm?spm=a21mg.p38356.b99.207.455f27e06fvYvv

17.jpg
DescribeInstances
aliyun ecs DescribeInstances \
  --InstanceName ${INSTANCE_NAME} | jq .
応答
{
	"PageNumber": 1,
	"TotalCount": 1,
	"PageSize": 10,
	"RequestId": "75DEBB7E-28E4-4C02-831F-5A9FABE51C79",
	"Instances": {
		"Instance": [
			{
				"ImageId": "alinux_17_01_64_20G_cloudinit_20171222.vhd",
				"VlanId": "",
				"EipAddress": {
					"IpAddress": "",
					"AllocationId": "",
					"InternetChargeType": ""
				},
				"ZoneId": "ap-northeast-1a",
				"IoOptimized": true,
				"SerialNumber": "65658306-2fe9-4fb5-a8f8-8590981cecb4",
				"Cpu": 1,
				"Memory": 2048,
				"DeviceAvailable": true,
				"SecurityGroupIds": {
					"SecurityGroupId": [
						"sg-XXXXXXXXXXXXXXXXXXXX"
					]
				},
				"SaleCycle": "",
				"AutoReleaseTime": "",
				"ResourceGroupId": "",
				"OSType": "linux",
				"OSName": "Aliyun Linux  17.1 64位",
				"InstanceNetworkType": "vpc",
				"HostName": "iZ6we5c4xroi7inustzcxzZ",
				"CreationTime": "2019-05-11T14:05Z",
				"EcsCapacityReservationAttr": {
					"CapacityReservationPreference": "",
					"CapacityReservationId": ""
				},
				"RegionId": "ap-northeast-1",
				"DeletionProtection": false,
				"OperationLocks": {
					"LockReason": []
				},
				"ExpiredTime": "2099-12-31T15:59Z",
				"InnerIpAddress": {
					"IpAddress": []
				},
				"InstanceTypeFamily": "ecs.n4",
				"InstanceId": "i-XXXXXXXXXXXXXXXXXXXX",
				"NetworkInterfaces": {
					"NetworkInterface": [
						{
							"MacAddress": "00:16:3e:00:40:9d",
							"PrimaryIpAddress": "172.16.1.11",
							"NetworkInterfaceId": "eni-XXXXXXXXXXXXXXXX"
						}
					]
				},
				"InternetMaxBandwidthIn": 0,
				"CreditSpecification": "",
				"InternetChargeType": "PayByTraffic",
				"SpotStrategy": "NoSpot",
				"StoppedMode": "KeepCharging",
				"InternetMaxBandwidthOut": 0,
				"VpcAttributes": {
					"NatIpAddress": "",
					"PrivateIpAddress": {
						"IpAddress": [
							"172.16.1.11"
						]
					},
					"VSwitchId": "vsw-XXXXXXXXXXXXXXXXXXXXX",
					"VpcId": "vpc-XXXXXXXXXXXXXXXXXXXXX"
				},
				"SpotPriceLimit": 0,
				"StartTime": "2019-05-11T14:05Z",
				"InstanceName": "INSTANCE",
				"Description": "Alicloud CLI INSTANCE",
				"OSNameEn": "Aliyun Linux 17.1 64 bit",
				"PublicIpAddress": {
					"IpAddress": []
				},
				"InstanceType": "ecs.n4.small",
				"Status": "Stopped",
				"Recyclable": false,
				"ClusterId": "",
				"GPUSpec": "",
				"InstanceChargeType": "PostPaid",
				"GPUAmount": 0,
				"DedicatedHostAttribute": {
					"DedicatedHostId": "",
					"DedicatedHostName": ""
				},
				"DedicatedInstanceAttribute": {
					"Affinity": "",
					"Tenancy": ""
				},
				"DeploymentSetId": ""
			}
		]
	}
}
18.jpg

ポイント:
この作業はあくまで作成です。
起動はしていません

7. InstanceとEIPの紐付け

7-1.環境変数の設定

設定
EIP_ID=$( \
  aliyun vpc DescribeEipAddresses \
    --RegionId ${REGION_ID} \
    | jq ".EipAddresses.EipAddress[].AllocationId" \
    | sed 's/"//g'
)
INSTANCE_ID=$( \
  aliyun ecs DescribeInstances \
    --InstanceName ${INSTANCE_NAME} \
    | jq ".Instances.Instance[].InstanceId" \
    | sed 's/"//g'
)
INSTANCE_TYPE="EcsInstance"
REGION_ID="ap-northeast-1"
確認
cat << EOF

  EIP_ID="eip-XXXXXXXXXXXXXXXXXXXXX"
  EIP_ID="${EIP_ID}"

  INSTANCE_ID="i-XXXXXXXXXXXXXXXXXXXX"
  INSTANCE_ID="${INSTANCE_ID}"

  INSTANCE_TYPE="EcsInstance"
  INSTANCE_TYPE="${INSTANCE_TYPE}"

EOF

7-2. InstanceとEIPの紐付け

https://jp.alibabacloud.com/help/doc-detail/36016.htm?spm=a21mg.p38356.a3.1.4efb47a6avi26i

19.jpg
AssociateEipAddress
aliyun vpc AssociateEipAddress \
  --AllocationId ${EIP_ID} \
  --InstanceId ${INSTANCE_ID} \
  --InstanceType ${INSTANCE_TYPE} | jq .
応答
{"RequestId":"67EA281F-4167-4541-8047-51F219EC3AED"}

7-3. 紐付けの確認

20.jpg
DescribeInstances
aliyun vpc DescribeEipAddresses \
  --RegionId ${REGIO_ID} \
  --AssociatedInstanceId ${INSTANCE_ID} \
  --AssociatedInstanceType ${INSTANCE_TYPE} \
  | jq ".EipAddresses.EipAddress[].InstanceId" \
  | sed 's/"//g'
応答
i-XXXXXXXXXXXXXXXXXXXX
21.jpg

8. インスタンスの起動

8-1.環境変数の設定

設定
INSTANCE_ID=$( \
  aliyun ecs DescribeInstances \
    --InstanceName ${INSTANCE_NAME} \
    | jq ".Instances.Instance[].InstanceId" \
    | sed 's/"//g'
)
REGION_ID="ap-northeast-1"
確認
cat << EOF

  INSTANCE_ID="i-XXXXXXXXXXXXXXXXXXXX"
  INSTANCE_ID="${INSTANCE_ID}"

EOF

8-2. Instanceの起動

https://jp.alibabacloud.com/help/doc-detail/25500.htm?spm=a21mg.p38356.b99.204.3c6f3fd6lNLbeK

22.jpg
StartInstance
aliyun ecs StartInstance \
  --InstanceId ${INSTANCE_ID} | jq .
応答
{"RequestId":"1A4A6334-19AE-45C8-B40B-33FA4A37128B"}

8-3. 起動確認

DescribeInstanceStatusを使ってみる!!!

https://jp.alibabacloud.com/help/doc-detail/25505.htm?spm=a21mg.p38356.b99.208.33d03f354G0NCX

23.jpg
DescribeInstanceStatus
aliyun ecs DescribeInstanceStatus \
  | jq ".InstanceStatuses.InstanceStatus[].Status" \
  | sed 's/"//g'
応答
Running

(確認が早いと)
Starting
24.jpg

ちなみに今回はインスタンスの作成・起動を別々で分けましたが、
[ RunInstance ]のコマンドを使えば作成・起動を同時にできます。

https://jp.alibabacloud.com/help/doc-detail/63440.htm?spm=a21mg.p38356.b99.202.29aad8b3v47zUd

9. 画面確認

最後にサービスが確かに動いていることを確認しましょう。

9-1.環境変数の設定

設定
ACCESS_EIP=$( \
  aliyun ecs DescribeInstances \
    --InstanceName ${INSTANCE_NAME} \
    | jq ".Instances.Instance[].EipAddress.IpAddress" \
    | sed 's/"//g'
)
REGION_ID="ap-northeast-1"
確認
cat << EOF

  ACCESS_EIP="XXX.XXX.XXX.XXX"
  ACCESS_EIP="${ACCESS_EIP}"

EOF

9-2. curlで確認

curl
curl http://${ACCESS_EIP}
応答
<!DOCTYPE html PUBLIC ...><html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Apache HTTP Server Test Page powered by Alinux</title>
<body>
<p>Hello Apache</p>
</body></html>

Instance が動いていることを確認できました!!

9-2:おまけ. ブラウザでも確認

DescribeInstances
aliyun ecs DescribeInstances \
  --InstanceName ${INSTANCE_NAME} \
  | jq ".Instances.Instance[].EipAddress.IpAddress" \
  | sed 's/"//g'
応答
xxx.xxx.xxx.xxx

と表示されるので、この[xxx.xxx.xxx.xxx]にアクセスすれば

25.jpg

このように表示されます。

10.後片付け

最後はしっかり環境を片付けましょう。
アクセスする以外にも、ECSが起動していることでお金がかかります

ECS は Instance 停止をしていれば課金されないようにも設定できますが、基本的に使い終わったら削除しましょう。

10-1.環境変数の設定

設定
VPC_ID=$( \
  aliyun vpc DescribeVpcs \
    --VpcName ${VPC_NAME} \
    | jq '.Vpcs.Vpc[].VpcId' \
    | sed 's/"//g'
)
VSWITCH_ID=$( \
  aliyun vpc DescribeVSwitches \
    --VSwitchName ${VSWITCH_NAME} \
    | jq '.VSwitches.VSwitch[].VSwitchId' \
    | sed 's/"//g'
)
EIP_ID=$( \
  aliyun vpc DescribeEipAddresses \
    --RegionId ${REGION_ID} \
    | jq ".EipAddresses.EipAddress[].AllocationId" \
    | sed 's/"//g'
)
SG_ID=$( \
  aliyun ecs DescribeSecurityGroups \
    --SecurityGroupName ${SG_GROUP_NAME} \
    | jq '.SecurityGroups.SecurityGroup[].SecurityGroupId' \
    | sed 's/"//g'
)
INSTANCE_ID=$( \
  aliyun ecs DescribeInstances \
    --InstanceName ${INSTANCE_NAME} \
    | jq ".Instances.Instance[].InstanceId" \
    | sed 's/"//g'
)
VPC_CIDR_BLOCK="172.16.0.0/16"
REGION_ID="ap-northeast-1"
確認
cat << EOF

  VPC_ID="vpc-XXXXXXXXXXXXXXXXXXXXX"
  VPC_ID="${VPC_ID}"

  VSWITCH_ID="vsw-XXXXXXXXXXXXXXXXXXXXX"
  VSWITCH_ID="${VSWITCH_ID}"

  EIP_ID="eip-XXXXXXXXXXXXXXXXXXXXX"
  EIP_ID="${EIP_ID}"

  SG_ID="sg-XXXXXXXXXXXXXXXXXXXX"
  SG_ID="${SG_ID}"

  INSTANCE_ID="i-XXXXXXXXXXXXXXXXXXXX"
  INSTANCE_ID="${INSTANCE_ID}"

  DEST_CIDR_IP="0.0.0.0/0"
  DEST_CIDR_IP="${DEST_CIDR_IP}"

EOF

10-2:a. Instanceの停止

まずは停止

https://jp.alibabacloud.com/help/doc-detail/25501.htm?spm=a21mg.p38356.b99.205.455f27e06fvYvv

26.jpg
StopInstance
aliyun ecs StopInstance \
  --InstanceId ${INSTANCE_ID} | jq .
応答
{"RequestId":"9B34901C-5E04-4FA1-BE91-FDDD20A99EB5"}

10-2:b. Instanceの確認

DescribeInstanceStatus
aliyun ecs DescribeInstanceStatus \
  | jq ".InstanceStatuses.InstanceStatus[].Status" \
  | sed 's/"//g'
応答
Stopped
28.jpg

ポイント
停止したのを確認してから削除

10-2:c. Instanceの削除

https://jp.alibabacloud.com/help/doc-detail/25507.htm?spm=a21mg.p38356.b99.207.19f41b84kDElB8

27.jpg
DeleteInstance
aliyun ecs DeleteInstance \
  --InstanceId ${INSTANCE_ID} | jq .
応答
{"RequestId":"9AEBAD7F-3C80-4C8E-8C55-2465046D12BD"}

10-2:d. Instanceの確認

DescribeInstances
aliyun ecs DescribeInstances \
  --InstanceName ${INSTANCE_NAME} \
  | jq ".Instances.Instance[]" | jq .
応答
何もなければOK!!
29.jpg

10-3:a SecurityGroupの受信ルール削除

https://jp.alibabacloud.com/help/doc-detail/25557.htm?spm=a21mg.p38356.b99.268.4a583070IRAjNC

31.jpg
RevokeSecurityGroup
aliyun ecs RevokeSecurityGroup \
  --SecurityGroupId ${SG_ID} \
  --PortRange ${PORT_RANGE} \
  --IpProtocol ${IP_PROTOCOL} \
  --SourceCidrIp "${DEST_CIDR_IP}" | jq .
応答
{"RequestId":"564ACC57-0AB9-477C-B81C-7ACA2AC69855"}
32.jpg

ポイント
RevokeSecurityGroup の場合、Source(VPC)関連の
情報が( Optional 設定なのに)ないとエラーが起こるので注意。

10-3:b SecurityGroupのルール確認

DescribeSecurityGroupAttribute
aliyun ecs DescribeSecurityGroupAttribute \
  --SecurityGroupId ${SG_ID}  | jq .
応答
何もなければOK!!

10-3:a SecurityGroupの削除

https://jp.alibabacloud.com/help/doc-detail/25558.htm?spm=a21mg.p38356.b99.272.7ffd20288rU02Z

30.jpg
DeleteSecurityGroup
aliyun ecs DeleteSecurityGroup \
  --SecurityGroupId ${SG_ID} | jq .
応答
{"RequestId":"C58C700E-E667-422A-8CAC-E34D9F997268"}

10-3:b SecurityGroupの確認

DescribeSecurityGroups
aliyun ecs DescribeSecurityGroups \
  --SecurityGroupId ${SG_ID} \
  | jq ".SecurityGroups.SecurityGroup[]"
応答
何もなければOK!!
33.jpg

10-4:a. EIPのリリース

https://jp.alibabacloud.com/help/doc-detail/36020.htm?spm=a21mg.p38356.b99.140.58762e52nOrG31

34.jpg
ReleaseEipAddress
aliyun vpc ReleaseEipAddress \
  --AllocationId ${EIP_ID} | jq .
応答
{"RequestId":"7EC283AE-874E-41AD-B18C-97D7B464FB75"}

10-4:b. EIPの確認

DescribeEipAddresses
aliyun vpc DescribeEipAddresses \
  --AllocationId ${EIP_ID} \
  | jq ".EipAddresses.EipAddress[]"
応答
何もなければOK!!
35.jpg

10-5:a. VSwitchの削除

https://jp.alibabacloud.com/help/doc-detail/35746.htm?spm=a21mg.p38356.b99.61.7fc758c1ochqJr

36.jpg
DeleteVSwitch
aliyun vpc DeleteVSwitch \
  --VSwitchId ${VSWITCH_ID} | jq .
応答
{"RequestId":"1CD83405-2DAA-4D13-9A8A-154426C4B8A8"}

10-5:b. VSwitchの確認

DescribeVSwitches
aliyun vpc DescribeVSwitches \
  --VSwitchName ${VSWITCH_NAME} \
  | jq ".VSwitches.VSwitch[]"
応答
何もなければOK!!
37.jpg

10-6:a. VPCの削除

https://jp.alibabacloud.com/help/doc-detail/35738.htm?spm=a21mg.p38356.b99.56.2556457bwGZ64s

38.jpg
DeleteVpc
aliyun vpc DeleteVpc \
  --VpcId ${VPC_ID} | jq .
応答
{"RequestId":"42A57098-1027-4D92-B262-408207F4750C"}

10-6:b. VPCの確認

DescribeVpcs
aliyun vpc DescribeVpcs \
  --VpcName ${VPC_NAME} \
  | jq ".Vpcs.Vpc[]"
応答
何もなければOK!!
39.jpg

作業は以上になります。お疲れ様でした!!

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?