This topic describes how to use Alibaba Cloud SDK for Python to associate an elastic IP address (EIP) with a NAT gateway and disassociate an EIP from a NAT gateway.

Prerequisites

Before you use Alibaba Cloud SDK for Python, make sure that the following requirements are met:
  • An Alibaba Cloud account and the AccessKey pair are obtained. You can create and view your AccessKey pair on the Security Management page of the Alibaba Cloud Management Console.
  • Alibaba Cloud SDK for Python is installed.
  • The VPC Python example library is downloaded.
    Go to the directory where the setup.py file is stored, and run the following command to initialize the environment:
    python setup.py install

Procedure

  1. In the directory of downloaded SDK files, open the aliyun-openapi-python-sdk-examples\sdk_examples\examples\natgw folder.
  2. Open the natgw_associate_eip.py file in your text editor. Set the required parameters, save the configurations, and then exit the editor.
    The sample code in this topic includes the following operations:
    1. Create a virtual private cloud (VPC) in the China (Shanghai) region.
    2. Create a vSwitch in the VPC.
    3. Create a NAT gateway in the VPC.
    4. Create an EIP in the China (Shanghai) region.
    5. Associate the EIP with the NAT gateway.
    6. Query the EIP that is associated with the NAT gateway.
    7. Create an EIP bandwidth plan in the China (Shanghai) region.
    8. Associate the EIP with the EIP bandwidth plan.
    9. Query the NAT gateway.
    10. Disassociate the EIP from the NAT gateway.
    11. Disassociate the EIP from the EIP bandwidth plan.
    12. Delete the EIP bandwidth plan.
    13. Delete the NAT gateway.
    14. Release the EIP.
    15. Delete the vSwitch.
    16. Delete the VPC.
    #encoding=utf-8
    import sys
    import json
    import time
    
    from alibabacloud_credentials.client import Client as CredClient
    from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException
    from aliyunsdkvpc.request.v20160428 import CreateNatGatewayRequest
    from aliyunsdkvpc.request.v20160428 import DeleteNatGatewayRequest
    from aliyunsdkvpc.request.v20160428 import DescribeNatGatewaysRequest
    from sdk_lib.sdk_vpc import Vpc
    from sdk_lib.sdk_vswitch import VSwitch
    from sdk_lib.sdk_eip import Eip
    from sdk_lib.sdk_cbwp import CommonBandwidthPackage
    from sdk_lib.common_util import CommonUtil
    from sdk_lib.check_status import CheckStatus
    from sdk_lib.exception import ExceptionHandler
    from sdk_lib.consts import *
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    # We do not recommend that you save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of your resources may be compromised. 
    # In this example, the AccessKey pair is obtained by using the Alibaba Cloud Credentials tool to authenticate API access. For more information about how to configure the variables, see https://www.alibabacloud.com/help/alibaba-cloud-sdk-262060/latest/configure-credentials-378659. 
    cred = CredClient()
    access_key_id = cred.get_access_key_id()
    access_key_secret = cred.get_access_key_secret()
    
    # Create an AcsClient instance.
    client = AcsClient(access_key_id, access_key_secret, '<your-region-id>')
    
    
    class NatGateway(object):
        def __init__(self, client):
            self.client = client
    
        def create_nat_gateway(self, params):
            """
            create_nat_gateway: Create a NAT gateway.
            """
            try:
                request = CreateNatGatewayRequest.CreateNatGatewayRequest()
                request.set_VpcId(params['vpc_id'])
                response = client.do_action_with_exception(request)
                response_json = json.loads(response)
                # Check whether the NAT gateway is in the Available state.
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME,
                                            self.describe_nat_gateway_status,
                                            AVAILABLE, response_json['NatGatewayId']):
                    return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def describe_nat_gateway(self, nat_gateway_id):
            """
            describe_nat_gateway: Query information about the NAT gateway in a specific region.
            """
            try:
                request = DescribeNatGatewaysRequest.DescribeNatGatewaysRequest()
                request.set_NatGatewayId(nat_gateway_id)
                response = 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 delete_nat_gateway(self, params):
            """
            delete_nat_gateway: Delete the NAT gateway.
            """
            try:
                request = DeleteNatGatewayRequest.DeleteNatGatewayRequest()
                request.set_NatGatewayId(params['nat_gateway_id'])
                response = client.do_action_with_exception(request)
                response_json = json.loads(response)
                # Check whether the NAT gateway is in the Available state.
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME * 5,
                                            self.describe_nat_gateway_status,
                                            '', params['nat_gateway_id']):
                    return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def describe_nat_gateway_status(self, nat_gateway_id):
            """
            describe_nat_gateway_status: Query the state of the NAT gateway in a specific region.
            """
            response = self.describe_nat_gateway(nat_gateway_id)
            if len(response["NatGateways"]["NatGateway"]) == 0:
                return ''
            return response["NatGateways"]["NatGateway"][0]['Status']
    
    
    def main():
        vpc = Vpc(client)
        vswitch = VSwitch(client)
        eip = Eip(client)
        cbwp = CommonBandwidthPackage(client)
        nat_gateway = NatGateway(client)
    
        params = {}
    
        # Create a VPC.
        vpc_json = vpc.create_vpc()
        CommonUtil.log("create_vpc", vpc_json)
    
        # Create a vSwitch.
        params['vpc_id'] = vpc_json['VpcId']
        params['zone_id'] = "cn-shanghai-d"
        params['cidr_block'] = "172.16.1.0/24"
        vswitch_json = vswitch.create_vswitch(params)
        CommonUtil.log("create_vswitch", vswitch_json)
    
        # Create a NAT gateway.
        nat_gateway_json = nat_gateway.create_nat_gateway(params)
        CommonUtil.log("create_nat_gateway", nat_gateway_json)
    
        # Create an EIP.
        eip_response_json = eip.allocate_eip_address(params)
        CommonUtil.log("allocate_eip_address", eip_response_json)
        params['allocation_id'] = eip_response_json["AllocationId"]
    
        # Associate the EIP with the NAT gateway.
        params['instance_id'] = nat_gateway_json['NatGatewayId']
        params['allocation_id'] = eip_response_json["AllocationId"]
        params['instance_type'] = 'Nat'
        eip_response_json = eip.associate_eip_address(params)
        CommonUtil.log("associate_eip_address eip", eip_response_json)
    
        # Query the EIP.
        eip_response_json = eip.describe_eip_address(params['allocation_id'])
        CommonUtil.log("describe_eip_address", eip_response_json)
    
        # Create an EIP bandwidth plan.
        params['bandwidth'] = BANDWIDTH_10
        cbwp_repsonse_json = cbwp.create_common_bandwidth_package(params)
        CommonUtil.log("create_common_bandwidth_package", cbwp_repsonse_json)
    
        # Associate the EIP with the EIP bandwidth plan.
        params['ip_instance_id'] = params['allocation_id']
        params['bandwidth_package_id'] = cbwp_repsonse_json['BandwidthPackageId']
        cbwp_repsonse_json = cbwp.add_common_bandwidth_packageIp(params)
        CommonUtil.log("add_common_bandwidth_packageIp", cbwp_repsonse_json)
    
        # Query the NAT gateway.
        params['nat_gateway_id'] = nat_gateway_json['NatGatewayId']
        nat_gateway_json = nat_gateway.describe_nat_gateway(params['nat_gateway_id'])
        CommonUtil.log("describe_nat_gateway", nat_gateway_json)
    
        # Disassociate the EIP from the NAT gateway.
        eip_response_json = eip.unassociate_eip_address(params)
        CommonUtil.log("unassociate_eip_address nat", eip_response_json)
    
        # Disassociate the EIP from the EIP bandwidth plan.
        cbwp_repsonse_json = cbwp.remove_common_bandwidth_packageIp(params)
        CommonUtil.log("remove_common_bandwidth_packageIp", cbwp_repsonse_json)
    
        # Delete the EIP bandwidth plan.
        params['force'] = True
        cbwp_repsonse_json = cbwp.delete_common_bandwidth_package(params)
        CommonUtil.log("delete_common_bandwidth_package", cbwp_repsonse_json)
    
        # Delete the NAT gateway.
        nat_gateway_json = nat_gateway.delete_nat_gateway(params)
        CommonUtil.log("delete_nat_gateway", nat_gateway_json)
    
        # Release the EIP.
        eip_response_json = eip.release_eip_address(params)
        CommonUtil.log("release_eip_address", eip_response_json)
    
        # Delete the vSwitch.
        params['vswitch_id'] = vswitch_json['VSwitchId']
        vswitch_json = vswitch.delete_vswitch(params)
        CommonUtil.log("delete_vswitch", vswitch_json)
    
        # Delete the VPC.
        vpc_json = vpc.delete_vpc(params)
        CommonUtil.log("delete_vpc", vpc_json)
    
    
    if __name__ == "__main__":
        sys.exit(main())
  3. Go to the directory where the natgw_associate_eip.py file is stored, and run the following command to associate and disassociate the EIP:
    python natgw_associate_eip.py

Result

The following output is returned:
---------------------------create_vpc---------------------------
{
  "ResourceGroupId": "rg-acfmxaz****",
  "RouteTableId": "vtb-uf6agemvkcmd8****",
  "VRouterId": "vrt-uf6r7lqtsv65d****",
  "VpcId": "vpc-uf6mqfqx8vjmo****",
  "RequestId": "ADF806C6-FCD6-4E46-B8E3-72C2BE895344"
}

---------------------------create_vswitch---------------------------
{
  "VSwitchId": "vsw-uf6rm6add6w89****",
  "RequestId": "897FFCC1-E6BA-484E-A245-C5DAEBBA269C"
}

---------------------------create_nat_gateway---------------------------
{
  "NatGatewayId": "ngw-uf681h38pbvly****",
  "BandwidthPackageIds": {
    "BandwidthPackageId": []
  },
  "ForwardTableIds": {
    "ForwardTableId": [
      "ftb-uf6jd0vbyao2d****"
    ]
  },
  "RequestId": "7C7CD3CB-041A-4B80-80B4-8BF8D5EF0D26",
  "SnatTableIds": {
    "SnatTableId": [
      "stb-uf6uj997htg3u****"
    ]
  }
}

---------------------------allocate_eip_address---------------------------
{
  "EipAddress": "106.XX.XX.129",
  "ResourceGroupId": "rg-acfmxaz****",
  "RequestId": "DB795B99-1CEA-4FC1-9CE3-9DE2B977BF02",
  "AllocationId": "eip-uf62tf8y4uyac****"
}

---------------------------associate_eip_address eip---------------------------
{
  "RequestId": "443D7060-B716-4193-A44D-23FE762004F8"
}

---------------------------describe_eip_address---------------------------
{
  "TotalCount": 1,
  "PageNumber": 1,
  "PageSize": 10,
  "EipAddresses": {
    "EipAddress": [
      {
        "ISP": "BGP",
        "ExpiredTime": "",
        "InternetChargeType": "PayByBandwidth",
        "IpAddress": "106.XX.XX.129",
        "AllocationId": "eip-uf62tf8y4uyac****",
        "PrivateIpAddress": "",
        "Status": "InUse",
        "BandwidthPackageId": "",
        "InstanceId": "ngw-uf681h38pbvly****",
        "InstanceRegionId": "cn-shanghai",
        "RegionId": "cn-shanghai",
        "AvailableRegions": {
          "AvailableRegion": [
            "cn-shanghai"
          ]
        },
        "ResourceGroupId": "rg-acfmxaz****",
        "HasReservationData": false,
        "InstanceType": "Nat",
        "AllocationTime": "2019-04-24T10:03:08Z",
        "Name": "",
        "OperationLocks": {
          "LockReason": []
        },
        "Mode": "NAT",
        "BandwidthPackageType": "",
        "BandwidthPackageBandwidth": "",
        "Bandwidth": "5",
        "HDMonitorStatus": "OFF",
        "ChargeType": "PostPaid",
        "SecondLimited": false,
        "Descritpion": ""
      }
    ]
  },
  "RequestId": "F0AEE605-14AD-4ADD-980C-4B508CE7EE4B"
}

---------------------------create_common_bandwidth_package----------------------
-----
{
  "ResourceGroupId": "rg-acfmxaz****",
  "BandwidthPackageId": "cbwp-uf6dmfvq0gzzg****",
  "RequestId": "D5E02777-2A72-42EC-8308-5D4B6E56D900"
}

---------------------------add_common_bandwidth_packageIp-----------------------
----
{
  "RequestId": "3B4CD99C-6E59-4DA2-9256-EACF3F699412"
}

---------------------------describe_nat_gateway---------------------------
{
  "TotalCount": 1,
  "PageNumber": 1,
  "RequestId": "A07498A0-4D60-4E0F-A7DE-3A832B174D59",
  "PageSize": 10,
  "NatGateways": {
    "NatGateway": [
      {
        "Status": "Available",
        "BandwidthPackageIds": {
          "BandwidthPackageId": []
        },
        "VpcId": "vpc-uf6mqfqx8vjmo****",
        "Description": "",
        "ForwardTableIds": {
          "ForwardTableId": [
            "ftb-uf6jd0vbyao2d****"
          ]
        },
        "IpLists": {
          "IpList": [
            {
              "UsingStatus": "Idle",
              "IpAddress": "106.XX.XX.129",
              "AllocationId": "eip-uf62tf8y4uyac****"
            }
          ]
        },
        "BusinessStatus": "Normal",
        "RegionId": "cn-shanghai",
        "CreationTime": "2019-04-24T10:03:05Z",
        "NatGatewayId": "ngw-uf681h38pbvly****",
        "SnatTableIds": {
          "SnatTableId": [
            "stb-uf6uj997htg3u****"
          ]
        },
        "AutoPay": false,
        "InstanceChargeType": "PostPaid",
        "ExpiredTime": "",
        "Spec": "Small",
        "Name": ""
      }
    ]
  }
}

---------------------------unassociate_eip_address nat--------------------------
-
{
  "RequestId": "92CB670E-239D-4659-B91F-E0565D5C0F2D"
}

---------------------------remove_common_bandwidth_packageIp--------------------
-------
{
  "RequestId": "A58E9647-6761-4CA3-8786-3CD4E7D2A7AB"
}

---------------------------delete_common_bandwidth_package----------------------
-----
{
  "RequestId": "4AA428BC-B72F-4567-94B8-AC2398EF7529"
}

---------------------------delete_nat_gateway---------------------------
{
  "RequestId": "EC6C5D04-AF7D-4560-A30E-80EC141D174D"
}

---------------------------release_eip_address---------------------------
{
  "RequestId": "9B1380B3-EE97-49BD-88FE-DBF356304208"
}

---------------------------delete_vswitch---------------------------
{
  "RequestId": "A9A1D63E-5709-4B98-90BF-9069AA264230"
}

---------------------------delete_vpc---------------------------
{
  "RequestId": "3B687C37-5315-4E0B-BE13-103BB287A80D"
}