Sample code for 30+ languages & platforms
C

Call an AWS Lambda Function

See more AWS Misc Examples

Demonstrates how to call an AWS Lambda function.

Chilkat C Downloads

C
#include <C_CkRest.h>
#include <C_CkAuthAws.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAws authAws;
    HCkJsonObject json;
    HCkStringBuilder sbRequestBody;
    HCkStringBuilder sbResponseBody;
    int statusCode;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    rest = CkRest_Create();

    // Connect to the Amazon AWS REST server.
    // such as https://email.us-west-2.amazonaws.com/
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;

    // -------------------------------------------------------------------------------------------
    // Note: The source of the lambda function (hosted on AWS) is shown at the bottom of this page.
    // --------------------------------------------------------------------------------------------

    // If your lambda function URL is: https://itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws/
    // then use just the domain part here:
    success = CkRest_Connect(rest,"itwxyj3vd6gjtaerbfqnfccs2e0fplzh.lambda-url.us-west-2.on.aws",port,bTls,bAutoReconnect);

    // Provide AWS credentials for the REST call.
    authAws = CkAuthAws_Create();
    CkAuthAws_putAccessKey(authAws,"AWS_ACCESS_KEY");
    CkAuthAws_putSecretKey(authAws,"AWS_SECRET_KEY");
    // the region should match our domain above..
    CkAuthAws_putRegion(authAws,"us-west-2");
    CkAuthAws_putServiceName(authAws,"lambda");

    CkRest_SetAuthAws(rest,authAws);

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"name","Benny");

    CkRest_AddHeader(rest,"Content-Type","application/json");

    sbRequestBody = CkStringBuilder_Create();
    CkJsonObject_EmitSb(json,sbRequestBody);

    sbResponseBody = CkStringBuilder_Create();
    success = CkRest_FullRequestSb(rest,"POST","/",sbRequestBody,sbResponseBody);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkAuthAws_Dispose(authAws);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    statusCode = CkRest_getResponseStatusCode(rest);
    if (statusCode >= 400) {
        printf("Response Status Code: %d\n",statusCode);
        printf("Response Body: %s\n",CkStringBuilder_getAsString(sbResponseBody));
        printf("Failed.\n");
        CkRest_Dispose(rest);
        CkAuthAws_Dispose(authAws);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    printf("Response Body:\n");
    printf("%s\n",CkStringBuilder_getAsString(sbResponseBody));


    CkRest_Dispose(rest);
    CkAuthAws_Dispose(authAws);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbRequestBody);
    CkStringBuilder_Dispose(sbResponseBody);

    }