Amazon API Gateway traits¶
Smithy can integrate with Amazon API Gateway using traits, authentication schemes, and OpenAPI specifications.
aws.apigateway#apiKeySource trait¶
- Summary
Specifies the source of the caller identifier that will be used to throttle API methods that require a key.
- Trait selector
service- Value type
stringset to one of the following values:Value
Description
HEADER
for receiving the API key from the X-API-Key header of a request
AUTHORIZER
for receiving the API key from the UsageIdentifierKey from a Lambda authorizer (formerly known as a custom authorizer)
- See also
Create and Use Usage Plans with API Keys for more on usage plans and API keys
Choose an API Key Source for information on choosing a source
x-amazon-apigateway-api-key-source for how this relates to OpenAPI
The following example sets the X-API-Key header as the API key source.
$version: "2"
namespace smithy.example
use aws.apigateway#apiKeySource
@apiKeySource("HEADER")
service Weather {
version: "2018-03-17"
}
Note
This trait should be considered internal-only and not exposed to your customers.
aws.apigateway#requestValidator trait¶
- Summary
Opts-in to Amazon API Gateway request validation for a service or operation.
- Trait selector
:test(service, operation)- Value type
stringvalue set to one of the following:Value
Description
fullThe parameters and body of a request are validated.
params-onlyOnly the parameters of a request are validated.
body-onlyOnly the body of a request is validated.
- See also
Enable Request Validation in API Gateway for more information
Request validators for information on how this converts to OpenAPI
x-amazon-apigateway-request-validator for more on how this converts to OpenAPI
x-amazon-apigateway-request-validators for more on how this converts to OpenAPI
Then following example enables request validation on a service:
$version: "2"
namespace smithy.example
use aws.apigateway#requestValidator
@requestValidator("full")
service Weather {
version: "2018-03-17"
}
Note
This trait should be considered internal-only and not exposed to your customers.
Warning
API Gateway request validation uses JSON Schema to validate requests against models and may not meet all the validation needs of your application.
aws.apigateway#integration trait¶
- Summary
Defines an API Gateway integration that integrates with an actual backend.
- Trait selector
:test(service, resource, operation)- Value type
structure- See also
Integrations for information on how this converts to OpenAPI
API Gateway Integration for in-depth API documentation
x-amazon-apigateway-integration for details on how this looks to OpenAPI
The aws.apigateway#integration trait is a structure that supports the
following members:
Property |
Type |
Description |
|---|---|---|
type |
|
Required. The type of integration with the specified backend. Valid values are:
|
uri |
|
Required. The endpoint URI of the backend. For integrations of
the |
httpMethod |
|
Required. Specifies the integration's HTTP method type
(for example, |
credentials |
|
Specifies the credentials required for the integration, if any. For AWS IAM role-based credentials, specify the ARN of an appropriate IAM role. If unspecified, credentials will default to resource-based permissions that must be added manually to allow the API to access the resource. For more information, see Granting Permissions Using a Resource Policy. |
passThroughBehavior |
|
Specifies how a request payload of unmapped content type is passed
through the integration request without modification. Supported
values are |
contentHandling |
Request payload content handling. |
|
timeoutInMillis |
|
Integration timeouts between 50 ms and 29,000 ms. |
connectionId |
|
The ID of a VpcLink for the private integration. |
connectionType |
|
The type of the network connection to the integration endpoint.
The valid value is |
cacheNamespace |
|
An API-specific tag group of related cached parameters. |
payloadFormatVersion |
|
Specifies the format of the payload sent to an integration. Required for HTTP APIs. For HTTP APIs, supported values for Lambda proxy integrations are 1.0 and 2.0. For all other integrations, 1.0 is the only supported value. |
cacheKeyParameters |
|
A list of request parameter names whose values are to be cached. |
requestParameters |
|
Specifies mappings from method request parameters to integration request parameters. Supported request parameters are querystring, path, header, and body. |
requestTemplates |
|
Mapping templates for a request payload of specified media types. |
responses |
|
Defines the method's responses and specifies desired parameter mappings or payload mappings from integration responses to method responses. |
The following example defines an integration that is applied to every operation within the service.
{
"version": "2.0",
"shapes": {
"smithy.example#Weather": {
"type": "service",
"version": "2018-03-17",
"traits": {
"aws.protocols#restJson1": {},
"aws.auth#sigv4": {
"name": "weather"
},
"aws.apigateway#integration": {
"type": "aws",
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:012345678901:function:HelloWorld/invocations",
"httpMethod": "POST",
"credentials": "arn:aws:iam::012345678901:role/apigateway-invoke-lambda-exec-role",
"requestTemplates": {
"application/json": "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }",
"application/xml": "#set ($root=$input.path('$')) <stage>$root.name</stage> "
},
"requestParameters": {
"integration.request.path.stage": "method.request.querystring.version",
"integration.request.querystring.provider": "method.request.querystring.vendor"
},
"cacheNamespace": "cache namespace",
"cacheKeyParameters": [],
"responses": {
"2\\d{2}": {
"statusCode": "200",
"responseParameters": {
"method.response.header.requestId": "integration.response.header.cid"
},
"responseTemplates": {
"application/json": "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }",
"application/xml": "#set ($root=$input.path('$')) <stage>$root.name</stage> "
}
},
"302": {
"statusCode": "302",
"responseParameters": {
"method.response.header.Location": "integration.response.body.redirect.url"
}
},
"default": {
"statusCode": "400",
"responseParameters": {
"method.response.header.test-method-response-header": "'static value'"
}
}
}
}
}
}
}
}
Note
This trait should be considered internal-only and not exposed to your customers.
aws.apigateway#mockIntegration trait¶
- Summary
Defines an API Gateway integration that returns a mock response.
- Trait selector
:test(service, resource, operation)- Value type
structure
The aws.apigateway#mockIntegration trait is a structure that supports the
following members:
Property |
Type |
Description |
|---|---|---|
passThroughBehavior |
|
Specifies how a request payload of unmapped content type is passed
through the integration request without modification. Supported
values are |
requestParameters |
|
Specifies mappings from method request parameters to integration request parameters. Supported request parameters are querystring, path, header, and body. |
requestTemplates |
|
Mapping templates for a request payload of specified media types. |
responses |
|
Defines the method's responses and specifies desired parameter mappings or payload mappings from integration responses to method responses. |
The following example defines an operation that uses a mock integration.
{
"smithy": "2.0",
"shapes": {
"smithy.example#MyOperation": {
"type": "operation",
"traits": {
"smithy.api#http": {
"method": "POST",
"uri": "/2"
},
"aws.apigateway#mockIntegration": {
"requestTemplates": {
"application/json": "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }",
"application/xml": "#set ($root=$input.path('$')) <stage>$root.name</stage> "
},
"requestParameters": {
"integration.request.path.stage": "method.request.querystring.version",
"integration.request.querystring.provider": "method.request.querystring.vendor"
},
"responses": {
"2\\d{2}": {
"statusCode": "200",
"responseParameters": {
"method.response.header.requestId": "integration.response.header.cid"
},
"responseTemplates": {
"application/json": "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }",
"application/xml": "#set ($root=$input.path('$')) <stage>$root.name</stage> "
}
},
"302": {
"statusCode": "302",
"responseParameters": {
"method.response.header.Location": "integration.response.body.redirect.url"
}
},
"default": {
"statusCode": "400",
"responseParameters": {
"method.response.header.test-method-response-header": "'static value'"
}
}
}
}
}
}
}
}
Note
This trait should be considered internal-only and not exposed to your customers.