使用 AWS SDK 释放弹性 IP 地址 - Amazon Elastic Compute Cloud

使用 AWS SDK 释放弹性 IP 地址

以下代码示例显示了如何释放弹性 IP 地址。

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
AWS SDK for .NET
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Release an Elastic IP address. /// </summary> /// <param name="allocationId">The allocation Id of the Elastic IP address.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> ReleaseAddress(string allocationId) { var request = new ReleaseAddressRequest { AllocationId = allocationId }; var response = await _amazonEC2.ReleaseAddressAsync(request); return response.HttpStatusCode == HttpStatusCode.OK; }
  • 有关 API 详细信息,请参阅《AWS SDK for .NET API 参考》中的 ReleaseAddress

C++
适用于 C++ 的 SDK
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

Aws::EC2::EC2Client ec2(clientConfiguration); Aws::EC2::Model::ReleaseAddressRequest request; request.SetAllocationId(allocationID); auto outcome = ec2.ReleaseAddress(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to release Elastic IP address " << allocationID << ":" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully released Elastic IP address " << allocationID << std::endl; }
  • 有关 API 详细信息,请参阅《AWS SDK for C++ API 参考》中的 ReleaseAddress

Java
SDK for Java 2.x
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

public static void releaseEC2Address(Ec2Client ec2,String allocId) { try { ReleaseAddressRequest request = ReleaseAddressRequest.builder() .allocationId(allocId) .build(); ec2.releaseAddress(request); System.out.printf("Successfully released elastic IP address %s", allocId); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • 有关 API 详细信息,请参阅《AWS SDK for Java 2.x API 参考》中的 ReleaseAddress

JavaScript
SDK for JavaScript (v3)
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

import { ReleaseAddressCommand } from "@aws-sdk/client-ec2"; import { client } from "../libs/client.js"; export const main = async () => { const command = new ReleaseAddressCommand({ // You can also use PublicIp, but that is for EC2 classic which is being retired. AllocationId: "ALLOCATION_ID", }); try { await client.send(command); console.log("Successfully released address."); } catch (err) { console.error(err); } };
  • 有关 API 详细信息,请参阅《AWS SDK for JavaScript API 参考》中的 ReleaseAddress

Kotlin
SDK for Kotlin
注意

这是适用于预览版中功能的预发行文档。本文档随时可能更改。

注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

suspend fun releaseEC2AddressSc(allocId: String?) { val request = ReleaseAddressRequest { allocationId = allocId } Ec2Client { region = "us-west-2" }.use { ec2 -> ec2.releaseAddress(request) println("Successfully released Elastic IP address $allocId") } }
  • 有关 API 详细信息,请参阅《AWS SDK for Kotlin API 参考》中的 ReleaseAddress

Python
适用于 Python (Boto3) 的 SDK
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

class ElasticIpWrapper: """Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) Elastic IP address actions.""" def __init__(self, ec2_resource, elastic_ip=None): """ :param ec2_resource: A Boto3 Amazon EC2 resource. This high-level resource is used to create additional high-level objects that wrap low-level Amazon EC2 service actions. :param elastic_ip: A Boto3 VpcAddress object. This is a high-level object that wraps Elastic IP actions. """ self.ec2_resource = ec2_resource self.elastic_ip = elastic_ip @classmethod def from_resource(cls): ec2_resource = boto3.resource("ec2") return cls(ec2_resource) def release(self): """ Releases an Elastic IP address. After the Elastic IP address is released, it can no longer be used. """ if self.elastic_ip is None: logger.info("No Elastic IP to release.") return try: self.elastic_ip.release() except ClientError as err: logger.error( "Couldn't release Elastic IP address %s. Here's why: %s: %s", self.elastic_ip.allocation_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • 有关 API 详细信息,请参阅《AWS SDK for Python(Boto3)API 参考》中的 ReleaseAddress

Ruby
SDK for Ruby
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

# Releases an Elastic IP address from an # Amazon Elastic Compute Cloud (Amazon EC2) instance. # # Prerequisites: # # - An Amazon EC2 instance with an associated Elastic IP address. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param allocation_id [String] The ID of the allocation corresponding to # the Elastic IP address. # @return [Boolean] true if the Elastic IP address was released; # otherwise, false. # @example # exit 1 unless elastic_ip_address_released?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'eipalloc-04452e528a66279EX' # ) def elastic_ip_address_released?(ec2_client, allocation_id) ec2_client.release_address(allocation_id: allocation_id) return true rescue StandardError => e puts("Error releasing Elastic IP address: #{e.message}") return false end
  • 有关 API 详细信息,请参阅《AWS SDK for Ruby API 参考》中的 ReleaseAddress

SAP ABAP
适用于 SAP ABAP 的 SDK
注意

在 GitHub 上查看更多内容。在 AWS 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

TRY. lo_ec2->releaseaddress( iv_allocationid = iv_allocation_id ). MESSAGE 'Elastic IP address released.' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY.
  • 有关 API 详细信息,请参阅《AWS SDK for SAP ABAP API 参考》中的 ReleaseAddress

有关 AWS 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 Amazon EC2 与 AWS SDK 结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。