Sample code for 30+ languages & platforms
C Requires Chilkat v11.6.0+

Amazon EC2: Launch an Instance with a Persisted Idempotency Token

See more Amazon EC2 Examples

Live mode creates a billable instance. Before the first run, select an AMI compatible with the instance type, a subnet, a security group in that subnet's VPC, and an existing key pair, all in the chosen region. The sample requires IMDSv2. Public addressing follows the subnet configuration; this is not a promise of Internet reachability.

The private ec2-launch-request.json stores the region, exact illustrated parameters, and client token before submission, and the resulting instance ID after success. Reruns load the saved parameters; changing constants in the source does not change an existing request. Serialize access to this file and retain it after timeouts. Deliberate retries must use the same token and unchanged parameters in the same region/subnet; a different Availability Zone can create another instance. Reconcile uncertain outcomes by client token before creating a new intent. A recorded instance ID prevents this sample from resubmitting. Launch is asynchronous; use Wait for Instance State and Health Checks.

Dry run by default: change dryRun to false deliberately for the live operation. DryRunOperation means the permission check passed, not that a resource was changed or that the later operation is guaranteed to succeed. The example never automatically changes a dry run into a live request.

Uses the current EC2 Query API (Version=2016-11-15) over HTTPS. Chilkat.Rest and AuthAws sign the GET request with AWS Signature Version 4, service ec2. Set the region once; the code builds the corresponding commercial AWS endpoint. Other partitions require their own endpoint suffix. EC2 returns XML, unlike DynamoDB's JSON protocol. AddQueryParam encodes parameter values.

Load current IAM credentials from a private qa_data/aws/ec2.json with string fields access_key, secret_key, and, for temporary credentials, session_token. Prefer short-lived role credentials. The example explicitly reads this file; it does not discover profiles or refresh expired credentials. The session token is included in the signed query, so keep URLs and diagnostics private. A Socket enables TLS certificate verification before handing its connection to Rest.

IAM permissions for the illustrated requests: ec2:RunInstances. Use least-privilege permissions for the intended resources; EC2 describe operations generally require Resource *.

Transport completion, HTTP status, and XML validity are checked separately. API errors contain Errors/Error/Code and RequestID; successful responses use requestId. Check the error code rather than assuming every HTTP 4xx means bad input. Use bounded exponential backoff with jitter for retryable failures. EC2 is eventually consistent: a recent resource change may not appear immediately. Verify account, region, ID, and observed state before retrying a change whose outcome is uncertain.

RunInstances reference

Chilkat C Downloads

C
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>
#include <C_CkAuthAws.h>
#include <C_CkRest.h>
#include <C_CkSocket.h>
#include <C_CkXml.h>
#include <C_CkFileAccess.h>
#include <C_CkCrypt2.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject credentials;
    const char *region;
    HCkStringBuilder endpoint;
    HCkAuthAws aws;
    HCkRest rest;
    HCkSocket connection;
    HCkXml xml;
    const char *responseXml;
    const char *errorCode;
    BOOL dryRun;
    const char *launchFile;
    HCkFileAccess files;
    HCkJsonObject launch;
    int fileStatus;
    HCkCrypt2 uuid;
    const char *clientToken;
    HCkStringBuilder tokenText;
    const char *instanceId;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    //  Private configuration, not a file in public web content or source control.
    //  Load current IAM credentials authorized for the EC2 operation.
    credentials = CkJsonObject_Create();
    CkJsonObject_putStrict(credentials,TRUE);
    success = CkJsonObject_LoadFile(credentials,"qa_data/aws/ec2.json");
    if (success != TRUE) {
        printf("Could not load private AWS credentials.\n");
        CkJsonObject_Dispose(credentials);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(credentials,"access_key") != 1) || (CkJsonObject_stringOf(credentials,"access_key") == "")) {
        printf("Missing or invalid access_key.\n");
        CkJsonObject_Dispose(credentials);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(credentials,"secret_key") != 1) || (CkJsonObject_stringOf(credentials,"secret_key") == "")) {
        printf("Missing or invalid secret_key.\n");
        CkJsonObject_Dispose(credentials);
        return;
    }

    //  Set this once to the region containing the instances.
    region = "us-west-2";
    endpoint = CkStringBuilder_Create();
    CkStringBuilder_Append(endpoint,"ec2.");
    CkStringBuilder_Append(endpoint,region);
    CkStringBuilder_Append(endpoint,".amazonaws.com");
    aws = CkAuthAws_Create();
    CkAuthAws_putAccessKey(aws,CkJsonObject_stringOf(credentials,"access_key"));
    CkAuthAws_putSecretKey(aws,CkJsonObject_stringOf(credentials,"secret_key"));
    CkAuthAws_putRegion(aws,region);
    CkAuthAws_putServiceName(aws,"ec2");
    CkAuthAws_putSignatureVersion(aws,4);
    rest = CkRest_Create();
    CkRest_putConnectTimeoutMs(rest,20000);
    CkRest_putIdleTimeoutMs(rest,60000);
    success = CkRest_SetAuthAws(rest,aws);
    if (success != TRUE) {
        printf("Could not configure AWS signing.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        return;
    }

    //  Temporary IAM/STS credentials require their session token on every signed request.
    //  AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
    //  Leave session_token absent or empty only for credentials that do not have one.
    if (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
        if (CkJsonObject_JsonTypeOf(credentials,"session_token") != 1) {
            printf("session_token must be a string.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            return;
        }

        if (CkJsonObject_stringOf(credentials,"session_token") != "") {
            success = CkRest_AddQueryParam(rest,"X-Amz-Security-Token",CkJsonObject_stringOf(credentials,"session_token"));
            if (success != TRUE) {
                printf("Could not set the session-token parameter.\n");
                CkJsonObject_Dispose(credentials);
                CkStringBuilder_Dispose(endpoint);
                CkAuthAws_Dispose(aws);
                CkRest_Dispose(rest);
                return;
            }

        }

    }

    //  Supply a Socket to explicitly enable TLS certificate verification.
    connection = CkSocket_Create();
    CkSocket_putRequireSslCertVerify(connection,TRUE);
    CkSocket_putMaxReadIdleMs(connection,60000);
    CkSocket_putMaxSendIdleMs(connection,60000);
    success = CkSocket_Connect(connection,CkStringBuilder_getAsString(endpoint),443,TRUE,20000);
    if (success != TRUE) {
        printf("TLS connection failed; inspect diagnostics privately.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        return;
    }

    success = CkRest_UseConnection(rest,connection,TRUE);
    if (success != TRUE) {
        printf("Could not attach the TLS connection.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        return;
    }

    //  EC2 uses the Query API and XML responses, not AWS JSON protocol headers.
    //  AddQueryParam URL-encodes the values; do not pre-encode IDs or tokens.
    xml = CkXml_Create();
    CkXml_putStrict(xml,TRUE);
    success = CkRest_AddQueryParam(rest,"Version","2016-11-15");
    if (success != TRUE) {
        printf("Could not set Version.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        return;
    }

    responseXml = "";
    errorCode = "";
    dryRun = TRUE;
    //  Serialize access to this private file. Use a new file only for a NEW intended launch.
    launchFile = "ec2-launch-request.json";
    files = CkFileAccess_Create();
    launch = CkJsonObject_Create();
    CkJsonObject_putStrict(launch,TRUE);
    fileStatus = CkFileAccess_FileExists3(files,launchFile);
    if (fileStatus == 0) {
        //  Replace these sample settings before the first run. Later runs use the saved file.
        uuid = CkCrypt2_Create();
        clientToken = CkCrypt2_generateUuid(uuid);
        tokenText = CkStringBuilder_Create();
        CkStringBuilder_Append(tokenText,clientToken);
        if (CkStringBuilder_getLength(tokenText) != 36) {
            printf("Could not generate a launch token.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"client_token",clientToken);
        if (success != TRUE) {
            printf("Could not save client token.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"region",region);
        if (success != TRUE) {
            printf("Could not save region.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"image_id","ami-0123456789abcdef0");
        if (success != TRUE) {
            printf("Could not prepare image_id.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"instance_type","t3.micro");
        if (success != TRUE) {
            printf("Could not prepare instance_type.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"subnet_id","subnet-0123456789abcdef0");
        if (success != TRUE) {
            printf("Could not prepare subnet_id.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"security_group_id","sg-0123456789abcdef0");
        if (success != TRUE) {
            printf("Could not prepare security_group_id.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"key_name","my-key-pair");
        if (success != TRUE) {
            printf("Could not prepare key_name.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"min_count","1");
        if (success != TRUE) {
            printf("Could not prepare min_count.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"max_count","1");
        if (success != TRUE) {
            printf("Could not prepare max_count.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"http_tokens","required");
        if (success != TRUE) {
            printf("Could not prepare http_tokens.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_UpdateString(launch,"http_endpoint","enabled");
        if (success != TRUE) {
            printf("Could not prepare http_endpoint.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

    }
    else {
        if (fileStatus != 1) {
            printf("Launch path is inaccessible or is not a regular file.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        success = CkJsonObject_LoadFile(launch,launchFile);
        if (success != TRUE) {
            printf("Could not load the saved launch request.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

    }

    if ((CkJsonObject_JsonTypeOf(launch,"region") != 1) || (CkJsonObject_stringOf(launch,"region") == "")) {
        printf("Missing or invalid saved region.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"client_token") != 1) || (CkJsonObject_stringOf(launch,"client_token") == "")) {
        printf("Missing or invalid saved client_token.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"image_id") != 1) || (CkJsonObject_stringOf(launch,"image_id") == "")) {
        printf("Missing or invalid saved image_id.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"instance_type") != 1) || (CkJsonObject_stringOf(launch,"instance_type") == "")) {
        printf("Missing or invalid saved instance_type.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"subnet_id") != 1) || (CkJsonObject_stringOf(launch,"subnet_id") == "")) {
        printf("Missing or invalid saved subnet_id.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"security_group_id") != 1) || (CkJsonObject_stringOf(launch,"security_group_id") == "")) {
        printf("Missing or invalid saved security_group_id.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"key_name") != 1) || (CkJsonObject_stringOf(launch,"key_name") == "")) {
        printf("Missing or invalid saved key_name.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"min_count") != 1) || (CkJsonObject_stringOf(launch,"min_count") == "")) {
        printf("Missing or invalid saved min_count.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"max_count") != 1) || (CkJsonObject_stringOf(launch,"max_count") == "")) {
        printf("Missing or invalid saved max_count.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"http_tokens") != 1) || (CkJsonObject_stringOf(launch,"http_tokens") == "")) {
        printf("Missing or invalid saved http_tokens.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(launch,"http_endpoint") != 1) || (CkJsonObject_stringOf(launch,"http_endpoint") == "")) {
        printf("Missing or invalid saved http_endpoint.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if (CkJsonObject_stringOf(launch,"region") != region) {
        printf("Saved launch region differs; do not reuse this token in another region.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkJsonObject_stringOf(launch,"min_count") != "1") || (CkJsonObject_stringOf(launch,"max_count") != "1")) {
        printf("This demonstration launches exactly one instance.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if (CkJsonObject_HasMember(launch,"instance_id") == TRUE) {
        if ((CkJsonObject_JsonTypeOf(launch,"instance_id") != 1) || (CkJsonObject_stringOf(launch,"instance_id") == "")) {
            printf("Missing or invalid saved instance_id.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

        printf("Launch already recorded for instance: %s\n",CkJsonObject_stringOf(launch,"instance_id"));
        printf("No new launch request was sent.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    //  Persist before sending, including dry runs. Never generate a new token for a retry.
    success = CkJsonObject_WriteFile(launch,launchFile);
    if (success != TRUE) {
        printf("Could not persist launch parameters; no launch sent.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"Action","RunInstances");
    if (success != TRUE) {
        printf("Could not set Action.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"ClientToken",CkJsonObject_stringOf(launch,"client_token"));
    if (success != TRUE) {
        printf("Could not set ClientToken.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"ImageId",CkJsonObject_stringOf(launch,"image_id"));
    if (success != TRUE) {
        printf("Could not set ImageId.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"InstanceType",CkJsonObject_stringOf(launch,"instance_type"));
    if (success != TRUE) {
        printf("Could not set InstanceType.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"SubnetId",CkJsonObject_stringOf(launch,"subnet_id"));
    if (success != TRUE) {
        printf("Could not set SubnetId.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"SecurityGroupId.1",CkJsonObject_stringOf(launch,"security_group_id"));
    if (success != TRUE) {
        printf("Could not set SecurityGroupId.1.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"KeyName",CkJsonObject_stringOf(launch,"key_name"));
    if (success != TRUE) {
        printf("Could not set KeyName.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"MinCount",CkJsonObject_stringOf(launch,"min_count"));
    if (success != TRUE) {
        printf("Could not set MinCount.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"MaxCount",CkJsonObject_stringOf(launch,"max_count"));
    if (success != TRUE) {
        printf("Could not set MaxCount.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"MetadataOptions.HttpTokens",CkJsonObject_stringOf(launch,"http_tokens"));
    if (success != TRUE) {
        printf("Could not set MetadataOptions.HttpTokens.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkRest_AddQueryParam(rest,"MetadataOptions.HttpEndpoint",CkJsonObject_stringOf(launch,"http_endpoint"));
    if (success != TRUE) {
        printf("Could not set MetadataOptions.HttpEndpoint.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if (dryRun == TRUE) {
        success = CkRest_AddQueryParam(rest,"DryRun","true");
        if (success != TRUE) {
            printf("Could not set DryRun.\n");
            CkJsonObject_Dispose(credentials);
            CkStringBuilder_Dispose(endpoint);
            CkAuthAws_Dispose(aws);
            CkRest_Dispose(rest);
            CkSocket_Dispose(connection);
            CkXml_Dispose(xml);
            CkFileAccess_Dispose(files);
            CkJsonObject_Dispose(launch);
            CkCrypt2_Dispose(uuid);
            CkStringBuilder_Dispose(tokenText);
            return;
        }

    }

    responseXml = CkRest_fullRequestNoBody(rest,"GET","/");
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("Request did not complete; inspect diagnostics privately.\n");
        //  The operation might already have been accepted. Check state before retrying.
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkXml_LoadXml(xml,responseXml);
    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("EC2 HTTP status: %d\n",CkRest_getResponseStatusCode(rest));
        if (success == TRUE) {
            if (CkXml_HasChildWithTag(xml,"Errors|Error|Code") == TRUE) {
                errorCode = CkXml_getChildContent(xml,"Errors|Error|Code");
                if ((dryRun == TRUE) && (errorCode == "DryRunOperation")) {
                    printf("Dry run authorized. No resource was changed.\n");
                    printf("Set dryRun to false only when ready to perform the operation.\n");
                    CkJsonObject_Dispose(credentials);
                    CkStringBuilder_Dispose(endpoint);
                    CkAuthAws_Dispose(aws);
                    CkRest_Dispose(rest);
                    CkSocket_Dispose(connection);
                    CkXml_Dispose(xml);
                    CkFileAccess_Dispose(files);
                    CkJsonObject_Dispose(launch);
                    CkCrypt2_Dispose(uuid);
                    CkStringBuilder_Dispose(tokenText);
                    return;
                }

                printf("AWS error code: %s\n",errorCode);
            }

            if (CkXml_HasChildWithTag(xml,"RequestID") == TRUE) {
                printf("AWS request ID: %s\n",CkXml_getChildContent(xml,"RequestID"));
            }

        }

        //  Keep full error messages and signed request diagnostics private.
        //  UnauthorizedOperation is not a successful dry run. Check IAM permissions.
        //  Retry throttled reads with bounded backoff; renew expired credentials.
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if (success != TRUE) {
        printf("Expected a well-formed XML response.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if (CkXml_tag(xml) != "RunInstancesResponse") {
        printf("Unexpected EC2 response element.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if (dryRun == TRUE) {
        printf("Unexpected HTTP 200 for a dry run; do not treat this as authorization to proceed.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkXml_HasChildWithTag(xml,"requestId") != TRUE) || (CkXml_getChildContent(xml,"requestId") == "")) {
        printf("Missing or empty requestId.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    printf("AWS request ID: %s\n",CkXml_getChildContent(xml,"requestId"));
    if (CkXml_NumChildrenAt(xml,"instancesSet") != 1) {
        printf("Expected exactly one launched instance; reconcile using the saved client token.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    if ((CkXml_HasChildWithTag(xml,"instancesSet|item|instanceId") != TRUE) || (CkXml_getChildContent(xml,"instancesSet|item|instanceId") == "")) {
        printf("Missing or empty instancesSet|item|instanceId.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    instanceId = CkXml_getChildContent(xml,"instancesSet|item|instanceId");
    printf("Launch accepted for instance: %s\n",instanceId);
    if (CkXml_HasChildWithTag(xml,"instancesSet|item|instanceState|name") == TRUE) {
        printf("State: %s\n",CkXml_getChildContent(xml,"instancesSet|item|instanceState|name"));
    }

    success = CkJsonObject_UpdateString(launch,"instance_id",instanceId);
    if (success != TRUE) {
        printf("Could not record the returned instance ID; keep it privately.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    success = CkJsonObject_WriteFile(launch,launchFile);
    if (success != TRUE) {
        printf("Launch accepted but the result could not be saved. Record the ID before retrying.\n");
        CkJsonObject_Dispose(credentials);
        CkStringBuilder_Dispose(endpoint);
        CkAuthAws_Dispose(aws);
        CkRest_Dispose(rest);
        CkSocket_Dispose(connection);
        CkXml_Dispose(xml);
        CkFileAccess_Dispose(files);
        CkJsonObject_Dispose(launch);
        CkCrypt2_Dispose(uuid);
        CkStringBuilder_Dispose(tokenText);
        return;
    }

    printf("Saved instance ID. Wait for running state and health before use.\n");


    CkJsonObject_Dispose(credentials);
    CkStringBuilder_Dispose(endpoint);
    CkAuthAws_Dispose(aws);
    CkRest_Dispose(rest);
    CkSocket_Dispose(connection);
    CkXml_Dispose(xml);
    CkFileAccess_Dispose(files);
    CkJsonObject_Dispose(launch);
    CkCrypt2_Dispose(uuid);
    CkStringBuilder_Dispose(tokenText);

    }