DEV Community

bgoh216
bgoh216

Posted on

[CDKTF] Configuring API key source for API Gateway created with OpenAPI

Context

A typical ApiGatewayRestApi object looks like this,

const apiGatewayRestApi = new aws.apiGatewayRestApi.ApiGatewayRestApi(this, "awpigw-rest-api", {
    name: "my-apigw",
    body: myOpenApiSpecification
})
Enter fullscreen mode Exit fullscreen mode

Initial intuition and instruction to update API Gateway's API_KEY_SOURCE is to add x-amazon-apigateway-api-key-source to OpenAPI specification and pass it as the body for initializing ApiGatewayRestApi.

Problem

Adding x-amazon-apigateway-api-key-source to OpenAPI specification and setting it in the body does not update API Gateway's API_KEY_SOURCE

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "Test1"
  },
  "servers" : [ {
    "url" : "/{basePath}",
    "variables" : {
      "basePath" : {
        "default" : "import"
      }
    }
  } ],
  "x-amazon-apigateway-api-key-source" : "HEADER",
   .
   .
   .
}
Enter fullscreen mode Exit fullscreen mode

Solution

In order to add the change the API Gateways's API_KEY_SOURCE, we only need to add one more line of configuration while initializing ApiGatewayRestApi,

const apiGatewayRestApi = new aws.apiGatewayRestApi.ApiGatewayRestApi(this, "awpigw-rest-api", {
    name: "my-apigw",
    body: myOpenApiSpecification,
    apiKeySource: "AUTHORIZER"
})
Enter fullscreen mode Exit fullscreen mode

There is no need to configure it in the 'myOpenApiSpecification' as specified here.

Image of Datadog

Learn how to monitor AWS container environments at scale

In this eBook, Datadog and AWS share insights into the changing state of containers in the cloud and explore why orchestration technologies are an essential part of managing ever-changing containerized workloads.

Download the eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more