AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Using the SDK

To use the SDK, you should properly initialize it with Aws::InitAPI before creating service clients and using them. You should then shut down the SDK with Aws::ShutdownAPI.

Both of these functions take an instance of Aws::SDKOptions, which you can use to set additional run-time options for SDK calls.

Initializing and Shutting Down the SDK#

A basic skeleton application looks like this:

#include <aws/core/Aws.h>
int main(int argc, char** argv)
{
   Aws::SDKOptions options;
   Aws::InitAPI(options);
   {
      // make your SDK calls here.
   }
   Aws::ShutdownAPI(options);
   return 0;
}

best practice

To properly shut down / clean up any service clients that you may initialize before Aws::ShutdownAPI is called, it's a best practice to make sure that all SDK calls are made within a pair of curly-braces as shown above, or within another function called between Aws::InitAPI and Aws::ShutdownAPI.

Setting SDK options#

The Aws::SDKOptions struct shown in Initializing and Shutting Down the SDK provides a number of options you can set. You should send the same options object to both Aws::InitAPI and Aws::ShutdownAPI.

A few examples:

  • Turn logging on using the default logger:

    Aws::SDKOptions options
    options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Info;
    Aws::InitAPI(options);
    {
        // make your SDK calls here.
    }
    Aws::ShutdownAPI(options);
    
  • Install a custom memory manager:

    MyMemoryManager memoryManager;
    Aws::SDKOptions options
    options.memoryManagementOptions.memoryManager = &memoryManager;
    Aws::InitAPI(options);
    {
        // make your SDK calls here.
    }
    Aws::ShutdownAPI(options);
    
  • Override the default HTTP client factory:

    Aws::SDKOptions options
    options.httpOptions.httpClientFactory_create_fn = [](){
            return Aws::MakeShared<MyCustomHttpClientFactory>(
                "ALLOC_TAG", arg1);
        };
    Aws::InitAPI(options);
    {
        // make your SDK calls here.
    }
    Aws::ShutdownAPI(options);
    

Note

httpOptions takes a closure instead of a std::shared_ptr. The SDK does this for all of its factory functions because the memory manager will not yet be installed at the time you will need to allocate this memory. Pass a closure to the SDK, and it will be called when it is safe to do so. This simplest way to do this is with a Lambda expression.

More Information#

For further examples of AWS SDK for C++ application code, view the topics in the Working with AWS Services section. Each example contains a link to the full source code on GitHub, which you can use as a starting point for your own applications.