async function createAPIKey()

in source/lambda/customresource/customresource.js [327:408]


async function createAPIKey(event) {
  try {
    await waitForStage(event, 10000, 50);

    console.log("[INFO] Stage is ready, creating API key");

    var api = event.ResourceProperties.APIGateway;

    var apiId = api.Id;
    var apiStage = api.Stage;
    var planName = api.PlanName;
    var keyName = api.Key.Name;
    var keyValue = api.Key.Value;

    var createKeyParams = {
      description: "AWS Captions API Key",
      enabled: true,
      name: keyName,
      value: keyValue,
    };

    console.log(
      "[INFO] about to create api key: " +
        JSON.stringify(createKeyParams, null, "  ")
    );

    var createApiKeyResponse = await apiGateway
      .createApiKey(createKeyParams)
      .promise();

    console.log("[INFO] got createApiKey() response: %j", createApiKeyResponse);

    var createUsagePlanParams = {
      name: planName,
      apiStages: [
        {
          apiId: apiId,
          stage: apiStage,
        },
      ],
      description: "AWS Captions API Usage Plan",
    };

    console.log(
      "[INFO] about to create usage plan: " +
        JSON.stringify(createUsagePlanParams, null, "  ")
    );

    var createUsagePlanResponse = await apiGateway
      .createUsagePlan(createUsagePlanParams)
      .promise();

    console.log(
      "[INFO] got createUsagePlan() response: %j",
      createUsagePlanResponse
    );

    var createUsagePlanKeyParams = {
      keyId: createApiKeyResponse.id,
      keyType: "API_KEY",
      usagePlanId: createUsagePlanResponse.id,
    };

    console.log(
      "[INFO] about to create usage plan key: " +
        JSON.stringify(createUsagePlanKeyParams, null, "  ")
    );

    var createUsagePlanKeyResponse = await apiGateway
      .createUsagePlanKey(createUsagePlanKeyParams)
      .promise();

    console.log(
      "[INFO] got createUsagePlanKey() response: %j",
      createUsagePlanKeyResponse
    );

    console.log("[INFO] successfully created API key");
  } catch (error) {
    console.log("[ERROR] failed to create API key material", error);
  }
}