Post

AWS - boto3 - boto3.resource('ec2').NetworkInterface('id')


EC2 - client

Table of Contents

  • EC2
    1. Client
    2. Paginators
    3. Waiters
    4. Service Resource
    5. ClassicAddress
    6. DhcpOptions
    7. Image
    8. Instance
    9. InternetGateway
    10. KeyPair
    11. KeyPairInfo
    12. NetworkAcl
    13. NetworkInterface
    14. NetworkInterfaceAssociation
    15. PlacementGroup
    16. Route
    17. RouteTable
    18. RouteTableAssociation
    19. SecurityGroup
    20. Snapshot
    21. Subnet
    22. Tag
    23. Volume
    24. Vpc
    25. VpcPeeringConnection
    26. VpcAddress

EC2 - NetworkInterface

class EC2.NetworkInterface(id)

A resource representing an Amazon Elastic Compute Cloud (EC2) NetworkInterface:

1
2
3
4
import boto3

ec2resource = boto3.resource('ec2')
ec2network_interface = ec2resource.NetworkInterface('id')

available actions:

  • assign_private_ip_addresses()
  • attach()
  • create_tags()
  • delete()
  • describe_attribute()
  • detach()
  • get_available_subresources()
  • load()
  • modify_attribute()
  • reload()
  • reset_attribute()
  • unassign_private_ip_addresses()

Actions


describe_attribute(kwargs_)

  • Describes a network interface attribute.
  • You can specify only one attribute at a time.

Request Syntax

1
2
3
4
5
6
response = network_interface.describe_attribute(
    Attribute='description'|'groupSet'|'sourceDestCheck'|'attachment',
    # (_string_) -- The attribute of the network interface. This parameter is required.
    DryRun=True|False,

)

Return:

  • Return type: dict
  • Response Syntax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    'Attachment': {
        'AttachTime': datetime(2015, 1, 1),
        'AttachmentId': 'string',
        'DeleteOnTermination': True|False,
        'DeviceIndex': 123,
        'NetworkCardIndex': 123,
        'InstanceId': 'string',
        'InstanceOwnerId': 'string',
        'Status': 'attaching'|'attached'|'detaching'|'detached'
    },
    'Description': {
        'Value': 'string'
    },
    'Groups': [
        {
            'GroupName': 'string',
            'GroupId': 'string'
        },
    ],
    'NetworkInterfaceId': 'string',
    'SourceDestCheck': {
        'Value': True|False
    }
}

assign_private_ip_addresses/unassign_private_ip_addresses(kwargs_) Assign/unassign private IP to network interface

  1. assign_private_ip_addresses()
    • Assigns one or more secondary private IP addresses to the specified network interface.
      • specify one or more specific secondary IP addresses,
      • specify the number of secondary IP addresses to be automatically assigned within the subnet’s CIDR block range.
      • The number of secondary IP addresses that you can assign to an instance varies by instance type.
    • When you move a secondary private IP address to another network interface, any Elastic IP address that is associated with the IP address is also moved.

    • Remapping an IP address is an asynchronous operation.
    • When you move an IP address from one network interface to another, check network/interfaces/macs/mac/local-ipv4s in the instance metadata to confirm that the remapping is complete.

    • must specify either the IP addresses or the IP address count in the request.
  2. unassign_private_ip_addresses()
    • Unassigns one or more secondary private IP addresses from a network interface.

Request Syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import boto3

ec2network_interface = boto3.resource('ec2').NetworkInterface('id')


response = ec2network_interface.assign_private_ip_addresses(
    AllowReassignment=True|False,
    # (_boolean_) -- Indicates whether to allow an IP address that is already assigned to another network interface or instance to be reassigned to the specified network interface.
    PrivateIpAddresses=[
        # (_list_) --
        # One or more IP addresses to be assigned as a secondary private IP address to the network interface.
        # You can't specify this parameter when also specifying a number of secondary IP addresses.
        # If you don't specify an IP address, Amazon EC2 automatically selects an IP address within the subnet range.
        'string',
    ],
    SecondaryPrivateIpAddressCount=123
    # (_integer_) -- The number of secondary IP addresses to assign to the network interface.
    # You can't specify this parameter when also specifying private IP addresses.
)



response = ec2network_interface.unassign_private_ip_addresses(
    PrivateIpAddresses=[
        'string',
        # _list_) -- **[REQUIRED]** The secondary private IP addresses to unassign from the network interface. You can specify this option multiple times to unassign more than one IP address.
    ]
)

Return:

  • Return type: dict
  • Response Syntax
1
2
3
4
5
6
7
8
{
    'NetworkInterfaceId': 'string',
    'AssignedPrivateIpAddresses': [
        {
            'PrivateIpAddress': 'string'
        },
    ]
}

attach/delete/detach(kwargs_) Attache/delete/detach a network interface to an instance

Request Syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import boto3

ec2network_interface = boto3.resource('ec2').NetworkInterface('id')

response = ec2network_interface.attach(
    DeviceIndex=123,
    # (_integer_) -- **[REQUIRED]** The index of the device for the network interface attachment.
    DryRun=True|False,
    InstanceId='string',
    # (_string_) -- **[REQUIRED]** The ID of the instance.
    NetworkCardIndex=123
    # (_integer_) -- The index of the network card. Some instance types support multiple network cards.
    # The primary network interface must be assigned to network card index 0.
    # The default is network card index 0.
)



response = ec2network_interface.delete(
    DryRun=True|False,
)



response = ec2network_interface.delete(
    DryRun=True|False,
    Force=True|False
    # (_boolean_) -- Specifies whether to force a detachment.
)
# Note
# - Use the Force parameter only as a last resort to detach a network interface from a failed instance.
# - use the Force parameter to detach a network interface,
#   - you might not be able to attach a different network interface to the same index on the instance without first stopping and starting the instance.
#   - the instance metadata might not get updated.
#     - the attributes associated with the detached network interface might still be visible.
#     - The instance metadata will get updated when you stop and start the instance.
  1. response = ec2network_interface.attach() Return:
    • Return type: dict
    • Response Syntax

      1
      2
      3
      4
      5
      6
      7
      
         # the output of AttachNetworkInterface.
         {
             'AttachmentId': 'string',
             # The ID of the network interface attachment.
             'NetworkCardIndex': 123
             # _(integer) -- The index of the network card.
         }
      
  2. response = ec2network_interface.delete() Return:
    • None
  3. response = ec2network_interface.detach() Return:
    • None

create_tags(kwargs_)

  • Adds or overwrites only the specified tags for the specified Amazon EC2 resource or resources.
  • When you specify an existing tag key, the value is overwritten with the new value. Each resource can have a maximum of 50 tags. Each tag consists of a key and optional value. Tag keys must be unique per resource.

Request Syntax

1
2
3
4
5
6
7
8
9
tag = network_interface.create_tags(
    DryRun=True|False,
    Tags=[
        {
            'Key': 'string',
            'Value': 'string'
        },
    ]
)
  • Return type: list(ec2.Tag)
  • A list of Tag resources

get_available_subresources()

Returns a list of all the available sub-resources for this Resource.

  • A list containing the name of each sub-resource for this resource Return:
  • Return type: list of str

load() and reload()

  • Calls EC2.Client.describe_network_interfaces()to update the attributes of the NetworkInterface resource.
  • Note that the load and reload methods are the same method and can be used interchangeably.

Request Syntax

1
2
ec2network_interface.load()
ec2network_interface.reload()

Return

  • None

modify_attribute(kwargs_)

  • Modifies the specified network interface attribute.
  • You can specify only one attribute at a time.
  • You can use this action to attach and detach security groups from an existing EC2 instance.

Request Syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
response = ec2network_interface.modify_attribute(
    Attachment={
        'AttachmentId': 'string',
        'DeleteOnTermination': True|False
    },
    Description={
        'Value': 'string'
    },
    DryRun=True|False,
    Groups=[
        'string',
        # Changes the security groups for the network interface.
        # The new set of groups you specify replaces the current set.
        # You must specify at least one group, even if it's just the default security group in the VPC.
        # You must specify the ID of the security group, not the name.
    ],
    SourceDestCheck={
        'Value': True|False
        # (_dict_) -- Indicates whether source/destination checking is enabled.
        # A value of true means checking is enabled, and false means checking is disabled.
        # This value must be false for a NAT instance to perform NAT.
    }
)

return

  • None

reset_attribute(kwargs_)

Resets a network interface attribute. You can specify only one attribute at a time.

Request Syntax

1
2
3
4
5
response = network_interface.reset_attribute(
    DryRun=True|False,
    SourceDestCheck='string'
    # (_string_) -- The source/destination checking attribute. Resets the value to true .
)

return

  • None

EC2 - NetworkInterfaceAssociation

class EC2.NetworkInterfaceAssociation(id)

A resource representing an Amazon Elastic Compute Cloud (EC2) NetworkInterfaceAssociation:

1
2
3
4
import boto3

ec2resource = boto3.resource('ec2')
ec2network_interface_association = ec2resource.NetworkInterfaceAssociation('id')

available actions:

  • delete()
  • get_available_subresources()
  • load()
  • reload()

Actions


delete(kwargs_) Disassociates an Elastic IP from the instance or network interface

  • An Elastic IP address is for use in either the EC2-Classic platform or in a VPC.
  • This is an idempotent operation.
    • If you perform the operation more than once, Amazon EC2 doesn’t return an error.

Request Syntax

1
2
3
4
5
response = ec2network_interface_association.delete(
    PublicIp='string',
    # _string_) -- EC2-Classic] The Elastic IP address. Required for EC2-Classic.
    DryRun=True|False
)

Return

  • None

get_available_subresources()

Returns a list of all the available sub-resources for this Resource.

Return:

  • Return type: list of str
  • A list containing the name of each sub-resource for this resource

load() and reload()

Calls EC2.Client.describe_network_interfaces() to update the attributes of the NetworkInterfaceAssociation resource.

  • Note that the load and reload methods are the same method and can be used interchangeably.

Request Syntax

1
2
ec2network_interface_association.load()
ec2network_interface_association.reload()

Return

  • None
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.