Uso de DisassociateAddress con un SDK de AWS o la CLI - Amazon Elastic Compute Cloud

Uso de DisassociateAddress con un SDK de AWS o la CLI

Los siguientes ejemplos de código muestran cómo utilizar DisassociateAddress.

Los ejemplos de acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Puede ver esta acción en contexto en el siguiente ejemplo de código:

.NET
SDK para .NET
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

/// <summary> /// Disassociate an Elastic IP address from an EC2 instance. /// </summary> /// <param name="associationId">The association Id.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> DisassociateIp(string associationId) { try { var response = await _amazonEC2.DisassociateAddressAsync( new DisassociateAddressRequest { AssociationId = associationId }); return response.HttpStatusCode == HttpStatusCode.OK; } catch (AmazonEC2Exception ec2Exception) { if (ec2Exception.ErrorCode == "InvalidAssociationID.NotFound") { _logger.LogError( $"AssociationId is invalid, unable to disassociate address. {ec2Exception.Message}"); } return false; } catch (Exception ex) { _logger.LogError( $"An error occurred while disassociating the Elastic IP.: {ex.Message}"); return false; } }
  • Para obtener información sobre las API, consulte DissasociateAddress en la Referencia de la API de AWS SDK para .NET.

Bash
AWS CLI con Bash script
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

############################################################################### # function ec2_disassociate_address # # This function disassociates an Elastic IP address from an Amazon Elastic Compute Cloud (Amazon EC2) instance. # # Parameters: # -a association_id - The association ID that represents the association of the Elastic IP address with an instance. # # And: # 0 - If successful. # 1 - If it fails. # ############################################################################### function ec2_disassociate_address() { local association_id response # Function to display usage information function usage() { echo "function ec2_disassociate_address" echo "Disassociates an Elastic IP address from an Amazon Elastic Compute Cloud (Amazon EC2) instance." echo " -a association_id - The association ID that represents the association of the Elastic IP address with an instance." echo "" } # Parse the command-line arguments while getopts "a:h" option; do case "${option}" in a) association_id="${OPTARG}" ;; h) usage return 0 ;; \?) echo "Invalid parameter" usage return 1 ;; esac done export OPTIND=1 # Validate the input parameters if [[ -z "$association_id" ]]; then errecho "ERROR: You must provide an association ID with the -a parameter." return 1 fi response=$(aws ec2 disassociate-address \ --association-id "$association_id") || { aws_cli_error_log ${?} errecho "ERROR: AWS reports disassociate-address operation failed." errecho "$response" return 1 } return 0 }

Las funciones de utilidad utilizadas en este ejemplo.

############################################################################### # function errecho # # This function outputs everything sent to it to STDERR (standard error output). ############################################################################### function errecho() { printf "%s\n" "$*" 1>&2 } ############################################################################## # function aws_cli_error_log() # # This function is used to log the error messages from the AWS CLI. # # The function expects the following argument: # $1 - The error code returned by the AWS CLI. # # Returns: # 0: - Success. # ############################################################################## function aws_cli_error_log() { local err_code=$1 errecho "Error code : $err_code" if [ "$err_code" == 1 ]; then errecho " One or more S3 transfers failed." elif [ "$err_code" == 2 ]; then errecho " Command line failed to parse." elif [ "$err_code" == 130 ]; then errecho " Process received SIGINT." elif [ "$err_code" == 252 ]; then errecho " Command syntax invalid." elif [ "$err_code" == 253 ]; then errecho " The system environment or configuration was invalid." elif [ "$err_code" == 254 ]; then errecho " The service returned an error." elif [ "$err_code" == 255 ]; then errecho " 255 is a catch-all error." fi return 0 }
  • Para obtener información sobre la API, consulte DisassociateAddress en la Referencia de comandos de la AWS CLI.

CLI
AWS CLI

Desasociar una dirección IP elástica en EC2-Classic

En este ejemplo, se desasocia una dirección IP elástica de una instancia en EC2-Classic. Si el comando se ejecuta correctamente, no se muestra ningún resultado.

Comando:

aws ec2 disassociate-address --public-ip 198.51.100.0

Desasociar una dirección IP elástica en EC2-VPC

En este ejemplo, se desasocia una dirección IP elástica de una instancia en una VPC. Si el comando se ejecuta correctamente, no se muestra ningún resultado.

Comando:

aws ec2 disassociate-address --association-id eipassoc-2bebb745
  • Para obtener información sobre la API, consulte DisassociateAddress en la Referencia de comandos de la AWS CLI.

Java
SDK para Java 2.x
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

/** * Disassociates an Elastic IP address from an instance asynchronously. * * @param associationId The ID of the association you want to disassociate. * @return a {@link CompletableFuture} representing the asynchronous operation of disassociating the address. The * {@link CompletableFuture} will complete with a {@link DisassociateAddressResponse} when the operation is * finished. * @throws RuntimeException if the disassociation of the address fails. */ public CompletableFuture<DisassociateAddressResponse> disassociateAddressAsync(String associationId) { Ec2AsyncClient ec2 = getAsyncClient(); DisassociateAddressRequest addressRequest = DisassociateAddressRequest.builder() .associationId(associationId) .build(); // Disassociate the address asynchronously. CompletableFuture<DisassociateAddressResponse> response = ec2.disassociateAddress(addressRequest); response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to disassociate address", ex); } }); return response; }
  • Para obtener información sobre las API, consulte DissasociateAddress en la Referencia de la API de AWS SDK for Java 2.x.

JavaScript
SDK para JavaScript (v3)
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

import { DisassociateAddressCommand, EC2Client } from "@aws-sdk/client-ec2"; /** * Disassociate an Elastic IP address from an instance. * @param {{ associationId: string }} options */ export const main = async ({ associationId }) => { const client = new EC2Client({}); const command = new DisassociateAddressCommand({ // You can also use PublicIp, but that is for EC2 classic which is being retired. AssociationId: associationId, }); try { await client.send(command); console.log("Successfully disassociated address"); } catch (caught) { if ( caught instanceof Error && caught.name === "InvalidAssociationID.NotFound" ) { console.warn(`${caught.message}.`); } else { throw caught; } } };
  • Para obtener información sobre las API, consulte DissasociateAddress en la Referencia de la API de AWS SDK para JavaScript.

Kotlin
SDK para Kotlin
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

suspend fun disassociateAddressSc(associationIdVal: String?) { val addressRequest = DisassociateAddressRequest { associationId = associationIdVal } Ec2Client.fromEnvironment { region = "us-west-2" }.use { ec2 -> ec2.disassociateAddress(addressRequest) println("You successfully disassociated the address!") } }
  • Para obtener información sobre la API, consulte DissasociateAddress en la Referencia de la API de SDK de AWS para Kotlin.

PowerShell
Herramientas para PowerShell V4

Ejemplo 1: En este ejemplo se desvincula la dirección IP elástica especificada de la instancia especificada en una VPC.

Unregister-EC2Address -AssociationId eipassoc-12345678

Ejemplo 2: En este ejemplo se desvincula la dirección IP elástica especificada de la instancia especificada en EC2-Classic.

Unregister-EC2Address -PublicIp 203.0.113.17
  • Para obtener información sobre la API, consulte DisassociateAddress en la Referencia de cmdlets de Herramientas de AWS para PowerShell (V4).

Herramientas para PowerShell V5

Ejemplo 1: En este ejemplo se desvincula la dirección IP elástica especificada de la instancia especificada en una VPC.

Unregister-EC2Address -AssociationId eipassoc-12345678

Ejemplo 2: En este ejemplo se desvincula la dirección IP elástica especificada de la instancia especificada en EC2-Classic.

Unregister-EC2Address -PublicIp 203.0.113.17
  • Para obtener información sobre la API, consulte DisassociateAddress en la Referencia de cmdlets de Herramientas de AWS para PowerShell (V5).

Python
SDK para Python (Boto3)
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class ElasticIpWrapper: """Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) Elastic IP address actions using the client interface.""" class ElasticIp: """Represents an Elastic IP and its associated instance.""" def __init__( self, allocation_id: str, public_ip: str, instance_id: Optional[str] = None ) -> None: """ Initializes the ElasticIp object. :param allocation_id: The allocation ID of the Elastic IP. :param public_ip: The public IP address of the Elastic IP. :param instance_id: The ID of the associated EC2 instance, if any. """ self.allocation_id = allocation_id self.public_ip = public_ip self.instance_id = instance_id def __init__(self, ec2_client: Any) -> None: """ Initializes the ElasticIpWrapper with an EC2 client. :param ec2_client: A Boto3 Amazon EC2 client. This client provides low-level access to AWS EC2 services. """ self.ec2_client = ec2_client self.elastic_ips: List[ElasticIpWrapper.ElasticIp] = [] @classmethod def from_client(cls) -> "ElasticIpWrapper": """ Creates an ElasticIpWrapper instance with a default EC2 client. :return: An instance of ElasticIpWrapper initialized with the default EC2 client. """ ec2_client = boto3.client("ec2") return cls(ec2_client) def disassociate(self, allocation_id: str) -> None: """ Removes an association between an Elastic IP address and an instance. When the association is removed, the instance is assigned a new public IP address. :param allocation_id: The allocation ID of the Elastic IP to disassociate. :raises ClientError: If the disassociation fails, such as when the association ID is not found. """ elastic_ip = self.get_elastic_ip_by_allocation(self.elastic_ips, allocation_id) if elastic_ip is None or elastic_ip.instance_id is None: logger.info( f"No association found for Elastic IP with allocation ID {allocation_id}." ) return try: # Retrieve the association ID before disassociating response = self.ec2_client.describe_addresses(AllocationIds=[allocation_id]) association_id = response["Addresses"][0].get("AssociationId") if association_id: self.ec2_client.disassociate_address(AssociationId=association_id) elastic_ip.instance_id = None # Remove the instance association else: logger.info( f"No Association ID found for Elastic IP with allocation ID {allocation_id}." ) except ClientError as err: if err.response["Error"]["Code"] == "InvalidAssociationID.NotFound": logger.error( f"Failed to disassociate Elastic IP {allocation_id} " "because the specified association ID for the Elastic IP address was not found. " "Verify the association ID and ensure the Elastic IP is currently associated with a " "resource before attempting to disassociate it." ) raise
  • Para obtener información sobre las API, consulte DisassociateAddress en la Referencia de la API de AWS SDK para Python (Boto3).

Rust
SDK para Rust
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

pub async fn disassociate_ip_address(&self, association_id: &str) -> Result<(), EC2Error> { self.client .disassociate_address() .association_id(association_id) .send() .await?; Ok(()) }
  • Para obtener información sobre la API, consulte DisassociateAddress en la Referencia de la API de AWS SDK para Rust.

Swift
SDK para Swift
nota

Hay más en GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

import AWSEC2 /// Disassociate an Elastic IP. /// /// - Parameter associationId: The ID of the association to end. func disassociateAddress(associationId: String?) async { do { _ = try await ec2Client.disassociateAddress( input: DisassociateAddressInput( associationId: associationId ) ) } catch { print("*** Unable to disassociate the IP address: \(error.localizedDescription)") } }
  • Para obtener información sobre la API, consulte DisassociateAddress en la Referencia de la API de AWS SDK para Swift.

Para obtener una lista completa de las guías para desarrolladores de AWS SDK y ejemplos de código, consulte Creación de recursos de Amazon EC2 mediante una AWS SDK. En este tema también se incluye información sobre cómo comenzar a utilizar el SDK y detalles sobre sus versiones anteriores.