このトピックでは、Alibaba Cloud Python SDK を使用して、EIP を ECS インスタンスに関連付ける方法について説明します。

始める前に

EIP を ECS インスタンスに関連付ける前に、次の条件を満たす必要があります。
  • ECS インスタンスは VPC ネットワークタイプである。
  • ECS インスタンスと EIP は同じリージョンに属している必要がある。
  • ECS インスタンスは実行中または停止状態である。
  • ECS インスタンスは、パブリック IP アドレスまたは別の EIP に関連付けられていない。

このタスクについて

この例では、次のワークフローを使用して、EIP を ECS インスタンスに関連付けます。
  1. 中国 (杭州) リージョンで EIP を作成します。
  2. EIP を ECS インスタンスに関連付けます。
  3. ECS インスタンスに関連付けられている EIP をクエリします。
  4. EIP のピーク帯域幅と名前を変更します。
  5. 変更した EIP をクエリします。
  6. ECS インスタンスから EIP との関連付けを解除します。
  7. EIP をリリースします。

手順

  1. ダウンロードした SDK ディレクトリで、$aliyun-openapi-python-sdk-examples\sdk_examples\sdk_lib フォルダーを開きます。
  2. テキストエディターで consts.py ファイルを開き、認証用の ACS_CLIENT パラメーターを設定します。
  3. ダウンロードした SDK ディレクトリで、$aliyun-openapi-python-sdk-examples\sdk_examples\examples\eip フォルダーを開きます。
  4. エディターで eip_quick_start.py ファイルを開き、必要に応じてパラメーターを設定します。 その後、設定を保存してエディターを終了します。
    #encoding=utf-8
    import sys
    import json
    from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException
    from aliyunsdkvpc.request.v20160428 import AllocateEipAddressRequest
    from aliyunsdkvpc.request.v20160428 import AssociateEipAddressRequest
    from aliyunsdkvpc.request.v20160428 import DescribeEipAddressesRequest
    from aliyunsdkvpc.request.v20160428 import UnassociateEipAddressRequest
    from aliyunsdkvpc.request.v20160428 import ModifyEipAddressAttributeRequest
    from aliyunsdkvpc.request.v20160428 import ReleaseEipAddressRequest
    from sdk_lib.exception import ExceptionHandler
    from sdk_lib.check_status import CheckStatus
    from sdk_lib.consts import *
    from sdk_lib.common_util import CommonUtil
    
    """
    Create an EIP->Associate the EIP with an ECS instance->Query the EIP->Modify the configuration and name of the EIP->Query the modified EIP->Disassociate the EIP from the ECS instance->Release the EIP
    """
    class Eip(object):
        def __init__(self, client):
            self.client = client
    
        def allocate_eip_address(self, params):
            """
            allocate_eip_address: Create an EIP.
            API reference link: https://www.alibabacloud.com/help/doc-detail/36016.htm
            """
            try:
                request = AllocateEipAddressRequest.AllocateEipAddressRequest()
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME,
                                            self.describe_eip_status,
                                            AVAILABLE, response_json["AllocationId"]):
                    return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def associate_eip_address(self, params):
            """
            associate_eip_address: Associate the EIP with an instance in the same region.
            API reference link: https://www.alibabacloud.com/help/doc-detail/36017.htm
            """
            try:
                request = AssociateEipAddressRequest.AssociateEipAddressRequest()
                # The ID of the EIP
                request.set_AllocationId(params['allocation_id'])
                # The type of the instance with which the EIP is associated
                request.set_InstanceType(params['instance_type'])
                # The ID of the instance with which the EIP is associated
                request.set_InstanceId(params['instance_id'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME,
                                            self.describe_eip_status,
                                            InUse, params['allocation_id']):
                    return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def describe_eip_address(self, allocation_id):
            """
            describe_eip_status: Query the EIP created in the specified region.
            API reference link: https://www.alibabacloud.com/help/doc-detail/36018.htm
            """
            try:
                request = DescribeEipAddressesRequest.DescribeEipAddressesRequest()
                # The ID of the EIP
                request.set_AllocationId(allocation_id)
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def describe_eip_status(self, allocation_id):
            """
            describe_eip_status: Query the status of the EIP created in the specified region.
            API reference link: https://www.alibabacloud.com/help/doc-detail/36018.htm
            """
            # The ID of the EIP
            response = self.describe_eip_address(allocation_id)
            return response["EipAddresses"]["EipAddress"][0]["Status"]
    
    
        def unassociate_eip_address(self, params):
            """
            unassociate_eip_address: Disassociate the EIP from the the instance.
            API reference link: https://www.alibabacloud.com/help/doc-detail/36021.htm
            """
            try:
                request = UnassociateEipAddressRequest.UnassociateEipAddressRequest()
                # The ID of the EIP
                request.set_AllocationId(params['allocation_id'])
                # The type of the instance from which the EIP is disassociated
                request.set_InstanceType(params['instance_type'])
                # The ID of the instance from which the EIP is disassociated
                request.set_InstanceId(params['instance_id'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME,
                                            self.describe_eip_status,
                                            AVAILABLE, params['allocation_id']):
                    return response_json
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def modify_eip_address(self, params):
            """
            modify_eip_address: Modify the name, description, and peak bandwidth of the specified EIP.
            API reference link: https://www.alibabacloud.com/help/doc-detail/36019.htm
            """
            try:
                request = ModifyEipAddressAttributeRequest.ModifyEipAddressAttributeRequest()
                # The ID of the EIP
                request.set_AllocationId(params['allocation_id'])
                # The peak bandwidth of the EIP (Mbps)
                request.set_Bandwidth(params['bandwidth'])
                # The name of the EIP
                request.set_Name(params['name'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def release_eip_address(self, params):
            """
            release_eip_address: Release the specified EIP.
            API reference link: https://www.alibabacloud.com/help/doc-detail/36020.htm
            """
            try:
                request = ReleaseEipAddressRequest.ReleaseEipAddressRequest()
                # The ID of the EIP to be released
                request.set_AllocationId(params['allocation_id'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
    def main():
    
        client = ACS_CLIENT
        eip = Eip(client)
    
        params = {}
    
        # Create an EIP.
        eip_response_json = eip.allocate_eip_address(params)
        CommonUtil.log("allocate_eip_address", eip_response_json)
    
        # Associate the EIP with an ECS instance.
        params['allocation_id'] = eip_response_json["AllocationId"]
        params['instance_id'] = ECS_INSTANCE_ID
        params['instance_type'] = 'EcsInstance'
        eip_response_json = eip.associate_eip_address(params)
        CommonUtil.log("associate_eip_address", eip_response_json)
    
        # Query the EIP.
        eip_response_json = eip.describe_eip_address(params['allocation_id'])
        CommonUtil.log("describe_eip_address", eip_response_json)
    
        # Modify the configuration and name of the EIP.
        params['bandwidth'] = BANDWIDTH_50
        params['name'] = EIP_NEW_NAME
        eip_response_json = eip.modify_eip_address(params)
        CommonUtil.log("modify_eip_address", eip_response_json)
    
        # Query the EIP.
        eip_response_json = eip.describe_eip_address(params['allocation_id'])
        CommonUtil.log("describe_eip_address", eip_response_json)
    
        # Disassociate the EIP from the ECS instance.
        eip_response_json = eip.unassociate_eip_address(params)
        CommonUtil.log("unassociate_eip_address", eip_response_json)
    
        # Release the EIP.
        eip_response_json = eip.release_eip_address(params)
        CommonUtil.log("release_eip_address", eip_response_json)
    
    if __name__ == '__main__':
        sys.exit(main())
  5. eip_quick_start.py ファイルが存在するディレクトリに移動し、次のコマンドを実行して EIP を ECS インスタンスに関連付けます。
    python eip_quick_start.py
    次の出力が表示されます。
    ---------------------------allocate_eip_address---------------------------
    {
      "EipAddress": "47.xx.xx.23",
      "ResourceGroupId": "rg-acfm4odxxxxxxxx",
      "RequestId": "C438312E-F7A4-4A04-901F-D22FE23EDB4D",
      "AllocationId": "eip-bp1wybucvhhx5xxxxxxxx"
    }
    ---------------------------associate_eip_address---------------------------
    {
      "RequestId": "6EC6605E-3D2B-4EE8-BD13-F1964CD1EAB1"
    }
    ---------------------------describe_eip_address---------------------------
    {
      "TotalCount": 1,
      "PageNumber": 1,
      "PageSize": 10,
      "EipAddresses": {
        "EipAddress": [
          {
            "ISP": "BGP",
            "ExpiredTime": "",
            "InternetChargeType": "PayByBandwidth",
            "IpAddress": "47.xx.xx.23",
            "AllocationId": "eip-bp1wybucvhhx5xxxxxxxx",
            "PrivateIpAddress": "",
            "Status": "InUse",
            "BandwidthPackageId": "",
            "InstanceId": "i-bp1e82xlhob2xxxxxxxx",
            "InstanceRegionId": "cn-hangzhou",
            "RegionId": "cn-hangzhou",
            "AvailableRegions": {
              "AvailableRegion": [
                "cn-hangzhou"
              ]
            },
            "ResourceGroupId": "rg-acfm4odxxxxxxxx",
            "HasReservationData": false,
            "InstanceType": "EcsInstance",
            "AllocationTime": "2019-04-17T11:57:43Z",
            "Name": "",
            "OperationLocks": {
              "LockReason": []
            },
            "Mode": "NAT",
            "BandwidthPackageType": "",
            "BandwidthPackageBandwidth": "",
            "Bandwidth": "5",
            "HDMonitorStatus": "OFF",
            "ChargeType": "PostPaid",
            "SecondLimited": false,
            "Descritpion": ""
          }
        ]
      },
      "RequestId": "8715A878-A808-4CC4-AAD5-E414FDAB5B0E"
    }
    ---------------------------modify_eip_address---------------------------
    {
      "RequestId": "2108AE1C-94FB-475D-BFEE-EC88598BF6A6"
    }
    ---------------------------describe_eip_address---------------------------
    {
      "TotalCount": 1,
      "PageNumber": 1,
      "PageSize": 10,
      "EipAddresses": {
        "EipAddress": [
          {
            "ISP": "BGP",
            "ExpiredTime": "",
            "InternetChargeType": "PayByBandwidth",
            "IpAddress": "47.xx.xx.23",
            "AllocationId": "eip-bp1wybucvhhx5xxxxxxxx",
            "PrivateIpAddress": "",
            "Status": "InUse",
            "BandwidthPackageId": "",
            "InstanceId": "i-bp1e82xlhob2xxxxxxxx",
            "InstanceRegionId": "cn-hangzhou",
            "RegionId": "cn-hangzhou",
            "AvailableRegions": {
              "AvailableRegion": [
                "cn-hangzhou"
              ]
            },
            "ResourceGroupId": "rg-acfm4odxxxxxxxx",
            "HasReservationData": false,
            "InstanceType": "EcsInstance",
            "AllocationTime": "2019-04-17T11:57:43Z",
            "Name": "EIP_NEW_NAME",
            "OperationLocks": {
              "LockReason": []
            },
            "Mode": "NAT",
            "BandwidthPackageType": "",
            "BandwidthPackageBandwidth": "",
            "Bandwidth": "50",
            "HDMonitorStatus": "OFF",
            "ChargeType": "PostPaid",
            "SecondLimited": false,
            "Descritpion": ""
          }
        ]
      },
      "RequestId": "6694D35B-B5DD-4506-8AB1-2D16477646DE"
    }
    ---------------------------unassociate_eip_address---------------------------
    {
      "RequestId": "EDE86CF6-EE68-4922-B919-85A4F11BF668"
    }
    ---------------------------release_eip_address---------------------------
    {
      "RequestId": "53FEE062-B595-4D64-AB47-834015D32888"
    }