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
// 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.
*
**/
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.allocate_address.inc]
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/AllocateAddressRequest.h>
#include <aws/ec2/model/AssociateAddressRequest.h>
#include <iostream>
// snippet-end:[ec2.cpp.allocate_address.inc]
#include "ec2_samples.h"
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.AllocateAddress]
//! Allocate an Elastic IP address and associate it with an Amazon Elastic Compute Cloud
//! (Amazon EC2) instance.
/*!
\param instanceID: An EC2 instance ID.
2024-08-02 10:24:50 -04:00
\param[out] publicIPAddress: String to return the public IP address.
\param[out] allocationID: String to return the allocation ID.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc::EC2::allocateAndAssociateAddress(const Aws::String &instanceId, Aws::String &publicIPAddress,
Aws::String &allocationID,
const Aws::Client::ClientConfiguration &clientConfiguration) {
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.allocate_address.code]
// snippet-start:[cpp.example_code.ec2.allocate_address.client]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
// snippet-end:[cpp.example_code.ec2.allocate_address.client]
2021-11-15 13:15:43 +00:00
Aws::EC2::Model::AllocateAddressRequest request;
request.SetDomain(Aws::EC2::Model::DomainType::vpc);
const Aws::EC2::Model::AllocateAddressOutcome outcome =
ec2Client.AllocateAddress(request);
if (!outcome.IsSuccess()) {
std::cerr << "Failed to allocate Elastic IP address:" <<
outcome.GetError().GetMessage() << std::endl;
return false;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
const Aws::EC2::Model::AllocateAddressResponse &response = outcome.GetResult();
allocationID = response.GetAllocationId();
publicIPAddress = response.GetPublicIp();
2021-11-15 13:15:43 +00:00
// snippet-end:[cpp.example_code.ec2.AllocateAddress]
2021-11-15 13:15:43 +00:00
Aws::EC2::Model::AssociateAddressRequest associate_request;
associate_request.SetInstanceId(instanceId);
2024-08-02 10:24:50 -04:00
associate_request.SetAllocationId(allocationID);
const Aws::EC2::Model::AssociateAddressOutcome associate_outcome =
ec2Client.AssociateAddress(associate_request);
if (!associate_outcome.IsSuccess()) {
2024-08-02 10:24:50 -04:00
std::cerr << "Failed to associate Elastic IP address " << allocationID
<< " with instance " << instanceId << ":" <<
associate_outcome.GetError().GetMessage() << std::endl;
return false;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
std::cout << "Successfully associated Elastic IP address " << allocationID
<< " with instance " << instanceId << std::endl;
2021-11-15 13:15:43 +00:00
// snippet-end:[ec2.cpp.allocate_address.code]
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.AllocateAddress2]
return true;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.AllocateAddress2]
2021-11-15 13:15:43 +00:00
/*
* main function
*
* Usage: 'run_allocate_address <instance_id>'
*
* Prerequisites: An EC2 instance to allocate an address for.
*
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "Usage: run_allocate_address <instance_id>"
<< std::endl;
2021-11-15 13:15:43 +00:00
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
Aws::String instanceID = argv[1];
2024-08-02 10:24:50 -04:00
Aws::String publicIPAddress;
Aws::String allocationID;
2024-08-02 10:24:50 -04:00
AwsDoc::EC2::allocateAndAssociateAddress(instanceID, publicIPAddress, allocationID,
clientConfig);
2021-11-15 13:15:43 +00:00
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD