Skip to content

Resource: awsApigatewayv2Api

Manages an Amazon API Gateway Version 2 API.

-> Note: Amazon API Gateway Version 2 resources are used for creating and deploying WebSocket and HTTP APIs. To create and deploy REST APIs, use Amazon API Gateway Version 1 resources.

Example Usage

Basic WebSocket API

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
new aws.apigatewayv2Api.Apigatewayv2Api(this, "example", {
  name: "example-websocket-api",
  protocolType: "WEBSOCKET",
  routeSelectionExpression: "$request.body.action",
});

Basic HTTP API

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as aws from "./.gen/providers/aws";
new aws.apigatewayv2Api.Apigatewayv2Api(this, "example", {
  name: "example-http-api",
  protocolType: "HTTP",
});

Argument Reference

The following arguments are supported:

  • name - (Required) Name of the API. Must be less than or equal to 128 characters in length.
  • protocolType - (Required) API protocol. Valid values: http, websocket.
  • apiKeySelectionExpression - (Optional) An API key selection expression. Valid values: $contextAuthorizerUsageIdentifierKey, $requestHeaderXApiKey. Defaults to $requestHeaderXApiKey. Applicable for WebSocket APIs.
  • corsConfiguration - (Optional) Cross-origin resource sharing (CORS) configuration. Applicable for HTTP APIs.
  • credentialsArn - (Optional) Part of quick create. Specifies any credentials required for the integration. Applicable for HTTP APIs.
  • description - (Optional) Description of the API. Must be less than or equal to 1024 characters in length.
  • disableExecuteApiEndpoint - (Optional) Whether clients can invoke the API by using the default executeApi endpoint. By default, clients can invoke the API with the default {apiId}ExecuteApi.{region}AmazonawsComEndpoint. To require that clients use a custom domain name to invoke the API, disable the default endpoint.
  • routeKey - (Optional) Part of quick create. Specifies any route key. Applicable for HTTP APIs.
  • routeSelectionExpression - (Optional) The route selection expression for the API. Defaults to $requestMethod $requestPath.
  • tags - (Optional) Map of tags to assign to the API. If configured with a provider defaultTags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
  • target - (Optional) Part of quick create. Quick create produces an API with an integration, a default catch-all route, and a default stage which is configured to automatically deploy changes. For HTTP integrations, specify a fully qualified URL. For Lambda integrations, specify a function ARN. The type of the integration will be HTTP_PROXY or AWS_PROXY, respectively. Applicable for HTTP APIs.
  • body - (Optional) An OpenAPI specification that defines the set of routes and integrations to create as part of the HTTP APIs. Supported only for HTTP APIs.
  • version - (Optional) Version identifier for the API. Must be between 1 and 64 characters in length.
  • failOnWarnings - (Optional) Whether warnings should return an error while API Gateway is creating or updating the resource using an OpenAPI specification. Defaults to false. Applicable for HTTP APIs.

Note: If the body argument is provided, the OpenAPI specification will be used to configure the integrations and route for the HTTP API. If this argument is provided, the following resources should not be managed as separate ones, as updates may cause manual resource updates to be overwritten:

  • awsApigatewayv2Integration
  • awsApigatewayv2Route

Further more, the name, description, corsConfiguration, tags and version fields should be specified in the Terraform configuration and the values will override any values specified in the OpenAPI document.

The corsConfiguration object supports the following:

  • allowCredentials - (Optional) Whether credentials are included in the CORS request.
  • allowHeaders - (Optional) Set of allowed HTTP headers.
  • allowMethods - (Optional) Set of allowed HTTP methods.
  • allowOrigins - (Optional) Set of allowed origins.
  • exposeHeaders - (Optional) Set of exposed HTTP headers.
  • maxAge - (Optional) Number of seconds that the browser should cache preflight request results.

Attributes Reference

In addition to all arguments above, the following attributes are exported:

Import

awsApigatewayv2Api can be imported by using the API identifier, e.g.,

$ terraform import aws_apigatewayv2_api.example aabbccddee