SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 1 Java
2024-08-02 10:24:50 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/AssociateAddressRequest.h>
#include <iostream>
#include "ec2_samples.h"
// snippet-start:[cpp.example_code.ec2.AssociateAddress]
//! Associate an Elastic IP address with an EC2 instance.
/*!
\param instanceId: An EC2 instance ID.
\param allocationId: An Elastic IP allocation ID.
\param[out] associationID: String to receive the association ID.
\param clientConfiguration: AWS client configuration.
\return bool: True if the address was associated with the instance; otherwise, false.
*/
bool AwsDoc::EC2::associateAddress(const Aws::String &instanceId, const Aws::String &allocationId,
Aws::String &associationID,
const Aws::Client::ClientConfiguration &clientConfiguration) {
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::AssociateAddressRequest request;
request.SetInstanceId(instanceId);
request.SetAllocationId(allocationId);
Aws::EC2::Model::AssociateAddressOutcome outcome = ec2Client.AssociateAddress(request);
if (!outcome.IsSuccess()) {
std::cerr << "Failed to associate address " << allocationId <<
" with instance " << instanceId << ": " <<
outcome.GetError().GetMessage() << std::endl;
} else {
std::cout << "Successfully associated address " << allocationId <<
" with instance " << instanceId << std::endl;
associationID = outcome.GetResult().GetAssociationId();
}
return outcome.IsSuccess();
}
// snippet-end:[cpp.example_code.ec2.AssociateAddress]
/*
* main function
*
* Usage:
* run_associate_address <instance ID> <allocation ID>
*
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc != 3) {
std::cout << "Usage: run_associate_address <instance ID> <allocation ID>" << std::endl;
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::String instanceId = argv[1];
Aws::String allocationId = argv[2];
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
Aws::String associationID;
AwsDoc::EC2::associateAddress(instanceId, allocationId, associationID, clientConfig);
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD