Use StopInstances com um AWS SDK ou CLI - AWS SDKExemplos de código

Há mais AWS SDK exemplos disponíveis no GitHub repositório AWS Doc SDK Examples.

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Use StopInstances com um AWS SDK ou CLI

Os exemplos de código a seguir mostram como usar o StopInstances.

Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação no contexto no seguinte exemplo de código:

.NET
AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/// <summary> /// Stop an EC2 instance. /// </summary> /// <param name="ec2InstanceId">The instance Id of the EC2 instance to /// stop.</param> /// <returns>Async task.</returns> public async Task StopInstances(string ec2InstanceId) { try { var request = new StopInstancesRequest { InstanceIds = new List<string> { ec2InstanceId }, }; await _amazonEC2.StopInstancesAsync(request); Console.Write("Waiting for the instance to stop."); await WaitForInstanceState(ec2InstanceId, InstanceStateName.Stopped); Console.WriteLine("\nThe instance has stopped."); } catch (AmazonEC2Exception ec2Exception) { if (ec2Exception.ErrorCode == "InvalidInstanceId") { _logger.LogError( $"InstanceId is invalid, unable to stop. {ec2Exception.Message}"); } throw; } catch (Exception ex) { _logger.LogError( $"An error occurred while stopping the instance.: {ex.Message}"); throw; } } /// <summary> /// Wait until an EC2 instance is in a specified state. /// </summary> /// <param name="instanceId">The instance Id.</param> /// <param name="stateName">The state to wait for.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> WaitForInstanceState(string instanceId, InstanceStateName stateName) { var request = new DescribeInstancesRequest { InstanceIds = new List<string> { instanceId } }; // Wait until the instance is in the specified state. var hasState = false; do { // Wait 5 seconds. Thread.Sleep(5000); // Check for the desired state. var response = await _amazonEC2.DescribeInstancesAsync(request); var instance = response.Reservations[0].Instances[0]; hasState = instance.State.Name == stateName; Console.Write(". "); } while (!hasState); return hasState; }
  • Para API obter detalhes, consulte StopInstancesem AWS SDK for .NET APIReferência.

Bash
AWS CLI com script Bash
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

############################################################################### # function ec2_stop_instances # # This function stops one or more Amazon Elastic Compute Cloud (Amazon EC2) instances. # # Parameters: # -i instance_id - The ID(s) of the instance(s) to stop (comma-separated). # -h - Display help. # # Returns: # 0 - If successful. # 1 - If it fails. ############################################################################### function ec2_stop_instances() { local instance_ids local option OPTARG # Required to use getopts command in a function. # bashsupport disable=BP5008 function usage() { echo "function ec2_stop_instances" echo "Stops one or more Amazon Elastic Compute Cloud (Amazon EC2) instances." echo " -i instance_id - The ID(s) of the instance(s) to stop (comma-separated)." echo " -h - Display help." echo "" } # Retrieve the calling parameters. while getopts "i:h" option; do case "${option}" in i) instance_ids="${OPTARG}" ;; h) usage return 0 ;; \?) echo "Invalid parameter" usage return 1 ;; esac done export OPTIND=1 if [[ -z "$instance_ids" ]]; then errecho "ERROR: You must provide one or more instance IDs with the -i parameter." usage return 1 fi response=$(aws ec2 stop-instances \ --instance-ids "${instance_ids}") || { aws_cli_error_log ${?} errecho "ERROR: AWS reports stop-instances operation failed with $response." return 1 } return 0 }

As funções utilitárias usadas neste exemplo.

############################################################################### # 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 API obter detalhes, consulte StopInstancesna Referência de AWS CLI Comandos.

C++
SDKpara C++
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

//! Stop an EC2 instance. /*! \param instanceID: An EC2 instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::stopInstance(const Aws::String &instanceId, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::StopInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); Aws::EC2::Model::StopInstancesOutcome dryRunOutcome = ec2Client.StopInstances(request); if (dryRunOutcome.IsSuccess()) { std::cerr << "Failed dry run to stop instance. A dry run should trigger an error." << std::endl; return false; } else if (dryRunOutcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to stop instance " << instanceId << ": " << dryRunOutcome.GetError().GetMessage() << std::endl; return false; } request.SetDryRun(false); Aws::EC2::Model::StopInstancesOutcome outcome = ec2Client.StopInstances(request); if (!outcome.IsSuccess()) { std::cout << "Failed to stop instance " << instanceId << ": " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully stopped instance " << instanceId << std::endl; } return outcome.IsSuccess(); } void PrintUsage() { std::cout << "Usage: run_start_stop_instance <instance_id> <start|stop>" << std::endl; }
  • Para API obter detalhes, consulte StopInstancesem AWS SDK for C++ APIReferência.

CLI
AWS CLI

Exemplo 1: Para parar uma EC2 instância da Amazon

O stop-instances exemplo a seguir interrompe a instância especificada EBS com suporte da Amazon.

aws ec2 stop-instances \ --instance-ids i-1234567890abcdef0

Saída:

{ "StoppingInstances": [ { "InstanceId": "i-1234567890abcdef0", "CurrentState": { "Code": 64, "Name": "stopping" }, "PreviousState": { "Code": 16, "Name": "running" } } ] }

Para obter mais informações, consulte Interromper e iniciar sua instância no Guia do usuário do Amazon Elastic Compute Cloud.

Exemplo 2: Para hibernar uma instância da Amazon EC2

O stop-instances exemplo a seguir hiberna a instância EBS apoiada pela Amazon se a instância estiver habilitada para hibernação e atender aos pré-requisitos de hibernação. Depois de colocar a instância em hibernação, ela é interrompida.

aws ec2 stop-instances \ --instance-ids i-1234567890abcdef0 \ --hibernate

Saída:

{ "StoppingInstances": [ { "CurrentState": { "Code": 64, "Name": "stopping" }, "InstanceId": "i-1234567890abcdef0", "PreviousState": { "Code": 16, "Name": "running" } } ] }

Para obter mais informações, consulte Colocar em hibernação uma instância sob demanda do Linux no Guia do usuário do Amazon Elastic Cloud Compute.

  • Para API obter detalhes, consulte StopInstancesna Referência de AWS CLI Comandos.

Java
SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/** * Stops the EC2 instance with the specified ID asynchronously and waits for the instance to stop. * * @param instanceId the ID of the EC2 instance to stop * @return a {@link CompletableFuture} that completes when the instance has been stopped, or exceptionally if an error occurs */ public CompletableFuture<Void> stopInstanceAsync(String instanceId) { StopInstancesRequest stopRequest = StopInstancesRequest.builder() .instanceIds(instanceId) .build(); DescribeInstancesRequest describeRequest = DescribeInstancesRequest.builder() .instanceIds(instanceId) .build(); Ec2AsyncWaiter ec2Waiter = Ec2AsyncWaiter.builder() .client(getAsyncClient()) .build(); CompletableFuture<Void> resultFuture = new CompletableFuture<>(); logger.info("Stopping instance " + instanceId + " and waiting for it to stop."); getAsyncClient().stopInstances(stopRequest) .thenCompose(response -> { if (response.stoppingInstances().isEmpty()) { return CompletableFuture.failedFuture(new RuntimeException("No instances were stopped. Please check the instance ID: " + instanceId)); } return ec2Waiter.waitUntilInstanceStopped(describeRequest); }) .thenAccept(waiterResponse -> { logger.info("Successfully stopped instance " + instanceId); resultFuture.complete(null); }) .exceptionally(throwable -> { logger.error("Failed to stop instance " + instanceId + ": " + throwable.getMessage(), throwable); resultFuture.completeExceptionally(new RuntimeException("Failed to stop instance: " + throwable.getMessage(), throwable)); return null; }); return resultFuture; }
  • Para API obter detalhes, consulte StopInstancesem AWS SDK for Java 2.x APIReferência.

JavaScript
SDKpara JavaScript (v3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

import { EC2Client, StopInstancesCommand } from "@aws-sdk/client-ec2"; import { fileURLToPath } from "node:url"; import { parseArgs } from "node:util"; /** * Stop one or more EC2 instances. * @param {{ instanceIds: string[] }} options */ export const main = async ({ instanceIds }) => { const client = new EC2Client({}); const command = new StopInstancesCommand({ InstanceIds: instanceIds, }); try { const { StoppingInstances } = await client.send(command); const instanceIdList = StoppingInstances.map( (instance) => ` • ${instance.InstanceId}`, ); console.log("Stopping instances:"); console.log(instanceIdList.join("\n")); } catch (caught) { if ( caught instanceof Error && caught.name === "InvalidInstanceID.NotFound" ) { console.warn(`${caught.message}`); } else { throw caught; } } };
  • Para API obter detalhes, consulte StopInstancesem AWS SDK for JavaScript APIReferência.

Kotlin
SDKpara Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

suspend fun stopInstanceSc(instanceId: String) { val request = StopInstancesRequest { instanceIds = listOf(instanceId) } Ec2Client { region = "us-west-2" }.use { ec2 -> ec2.stopInstances(request) println("Waiting until instance $instanceId stops. This will take a few minutes.") ec2.waitUntilInstanceStopped { // suspend call instanceIds = listOf(instanceId) } println("Successfully stopped instance $instanceId") } }
  • Para API obter detalhes, consulte a StopInstancesreferência AWS SDKdo Kotlin API.

PowerShell
Ferramentas para PowerShell

Exemplo 1: Esse exemplo interrompe a instância especificada.

Stop-EC2Instance -InstanceId i-12345678

Saída:

CurrentState InstanceId PreviousState ------------ ---------- ------------- Amazon.EC2.Model.InstanceState i-12345678 Amazon.EC2.Model.InstanceState
  • Para API obter detalhes, consulte StopInstancesem Referência de AWS Tools for PowerShell cmdlet.

Python
SDKpara Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

class EC2InstanceWrapper: """Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) instance actions using the client interface.""" def __init__( self, ec2_client: Any, instances: Optional[List[Dict[str, Any]]] = None ) -> None: """ Initializes the EC2InstanceWrapper with an EC2 client and optional instances. :param ec2_client: A Boto3 Amazon EC2 client. This client provides low-level access to AWS EC2 services. :param instances: A list of dictionaries representing Boto3 Instance objects. These are high-level objects that wrap instance actions. """ self.ec2_client = ec2_client self.instances = instances or [] @classmethod def from_client(cls) -> "EC2InstanceWrapper": """ Creates an EC2InstanceWrapper instance with a default EC2 client. :return: An instance of EC2InstanceWrapper initialized with the default EC2 client. """ ec2_client = boto3.client("ec2") return cls(ec2_client) def stop(self) -> Optional[Dict[str, Any]]: """ Stops instances and waits for them to be in a stopped state. :return: The response to the stop request, or None if there are no instances to stop. """ if not self.instances: logger.info("No instances to stop.") return None instance_ids = [instance["InstanceId"] for instance in self.instances] try: # Attempt to stop the instances stop_response = self.ec2_client.stop_instances(InstanceIds=instance_ids) waiter = self.ec2_client.get_waiter("instance_stopped") waiter.wait(InstanceIds=instance_ids) except ClientError as err: logger.error( f"Failed to stop instance(s): {','.join(map(str, instance_ids))}" ) error_code = err.response["Error"]["Code"] if error_code == "IncorrectInstanceState": logger.error( "Couldn't stop instance(s) because they are in an incorrect state. " "Ensure the instances are in a running state before stopping them." ) raise return stop_response
  • Para API obter detalhes, consulte a StopInstancesReferência AWS SDK do Python (Boto3). API

Ruby
SDKpara Ruby
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

require 'aws-sdk-ec2' # Prerequisites: # # - The Amazon EC2 instance. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param instance_id [String] The ID of the instance. # @return [Boolean] true if the instance was stopped; otherwise, false. # @example # exit 1 unless instance_stopped?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'i-123abc' # ) def instance_stopped?(ec2_client, instance_id) response = ec2_client.describe_instance_status(instance_ids: [instance_id]) if response.instance_statuses.count.positive? state = response.instance_statuses[0].instance_state.name case state when 'stopping' puts 'The instance is already stopping.' return true when 'stopped' puts 'The instance is already stopped.' return true when 'terminated' puts 'Error stopping instance: ' \ 'the instance is terminated, so you cannot stop it.' return false end end ec2_client.stop_instances(instance_ids: [instance_id]) ec2_client.wait_until(:instance_stopped, instance_ids: [instance_id]) puts 'Instance stopped.' true rescue StandardError => e puts "Error stopping instance: #{e.message}" false end # Example usage: def run_me instance_id = '' region = '' # Print usage information and then stop. if ARGV[0] == '--help' || ARGV[0] == '-h' puts 'Usage: ruby ec2-ruby-example-stop-instance-i-123abc.rb ' \ 'INSTANCE_ID REGION ' # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts 'Example: ruby ec2-ruby-example-start-instance-i-123abc.rb ' \ 'i-123abc us-west-2' exit 1 # If no values are specified at the command prompt, use these default values. # Replace us-west-2 with the AWS Region you're using for Amazon EC2. elsif ARGV.count.zero? instance_id = 'i-123abc' region = 'us-west-2' # Otherwise, use the values as specified at the command prompt. else instance_id = ARGV[0] region = ARGV[1] end ec2_client = Aws::EC2::Client.new(region: region) puts "Attempting to stop instance '#{instance_id}' " \ '(this might take a few minutes)...' return if instance_stopped?(ec2_client, instance_id) puts 'Could not stop instance.' end run_me if $PROGRAM_NAME == __FILE__
  • Para API obter detalhes, consulte StopInstancesem AWS SDK for Ruby APIReferência.

Rust
SDKpara Rust
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

pub async fn stop_instance(&self, instance_id: &str) -> Result<(), EC2Error> { tracing::info!("Stopping instance {instance_id}"); self.client .stop_instances() .instance_ids(instance_id) .send() .await?; self.wait_for_instance_stopped(instance_id, None).await?; tracing::info!("Stopped instance."); Ok(()) }

Aguarde até que uma instância esteja no estado interrompido, usando os WaitersAPI. Usar os Waiters API requer `use aws_sdk_ec2: :client: :Waiters` no arquivo rust.

pub async fn stop_instance(&self, instance_id: &str) -> Result<(), EC2Error> { tracing::info!("Stopping instance {instance_id}"); self.client .stop_instances() .instance_ids(instance_id) .send() .await?; self.wait_for_instance_stopped(instance_id, None).await?; tracing::info!("Stopped instance."); Ok(()) }
  • Para API obter detalhes, consulte StopInstancesa AWS SDKAPIreferência do Rust.

SAP ABAP
SDKpara SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

DATA lt_instance_ids TYPE /aws1/cl_ec2instidstringlist_w=>tt_instanceidstringlist. APPEND NEW /aws1/cl_ec2instidstringlist_w( iv_value = iv_instance_id ) TO lt_instance_ids. "Perform dry run" TRY. " DryRun is set to true. This checks for the required permissions to stop the instance without actually making the request. " lo_ec2->stopinstances( it_instanceids = lt_instance_ids iv_dryrun = abap_true ). CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). " If the error code returned is `DryRunOperation`, then you have the required permissions to stop this instance. " IF lo_exception->av_err_code = 'DryRunOperation'. MESSAGE 'Dry run to stop instance completed.' TYPE 'I'. " DryRun is set to false to stop instance. " oo_result = lo_ec2->stopinstances( " oo_result is returned for testing purposes. " it_instanceids = lt_instance_ids iv_dryrun = abap_false ). MESSAGE 'Successfully stopped the EC2 instance.' TYPE 'I'. " If the error code returned is `UnauthorizedOperation`, then you don't have the required permissions to stop this instance. " ELSEIF lo_exception->av_err_code = 'UnauthorizedOperation'. MESSAGE 'Dry run to stop instance failed. User does not have permissions to stop the instance.' TYPE 'E'. ELSE. DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDIF. ENDTRY.
  • Para API obter detalhes, consulte StopInstances AWSSDKpara SAP ABAP API referência.