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
string
set 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#authorizers
trait#
- Summary
- Lambda authorizers to attach to the authentication schemes defined on this service.
- Trait selector
service[trait|protocols]
A service shape that has the protocols trait
- Value type
map
of arbitrary names to authorizer definitions. These authorizer definitions are applied to a service, resource, or operation using the aws.apigateway#authorizer trait.
An authorizer definition is a structure that supports the following members:
Property | Type | Description |
---|---|---|
scheme | string |
Required. A Smithy authentication scheme shape ID that identifies how a client authenticates. This value MUST reference one of the authentication schemes attached to the service. |
type | string |
The type of the authorizer. If specifying information beyond the scheme or customAuthType, this value is required. The value must be "token", for an authorizer with the caller identity embedded in an authorization token, or "request", for an authorizer with the caller identity contained in request parameters. |
customAuthType | string |
The authType of the authorizer. This value is used in APIGateway
exports as x-amazon-apigateway-authtype . This value is set to
custom by default if type is set, or awsSigv4 if your
scheme is aws.auth#sigv4. |
uri | string |
Specifies the authorizer's Uniform Resource Identifier
(URI). For token or request authorizers, this must be a
well-formed Lambda function URI, for example,
arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:{account_id}:function:{lambda_function_name}/invocations .
In general, the URI has this form arn:aws:apigateway:{region}:lambda:path/{service_api} ,
where {region} is the same as the region hosting the Lambda
function, path indicates that the remaining substring in the URI
should be treated as the path to the resource, including the initial
/ . For Lambda functions, this is usually of the form
/2015-03-31/functions/[FunctionARN]/invocations . |
credentials | string |
Specifies the required credentials as an IAM role for API Gateway to invoke the authorizer. To specify an IAM role for API Gateway to assume, use the role's Amazon Resource Name (ARN). This value MUST be omitted in order to use resource-based permissions on the Lambda function. |
identitySource | string |
The identity source for which authorization is requested. For a For the |
identityValidationExpression | string |
A validation expression for the incoming identity token. For token
authorizers, this value is a regular expression. API Gateway will
match the aud field of the incoming token from the client against
the specified regular expression. It will invoke the authorizer's
Lambda function when there is a match. Otherwise, it will return a
401 Unauthorized response without calling the Lambda function. The
validation expression does not apply to the request authorizer. |
resultTtlInSeconds | integer |
The TTL in seconds of cached authorizer results. If it equals 0, authorization caching is disabled. If it is greater than 0, API Gateway will cache authorizer responses. If this field is not set, the default value is 300. The maximum value is 3600, or 1 hour. |
authorizerPayloadFormatVersion | string |
For HTTP APIs, specifies the format of the data that API Gateway
sends to a Lambda authorizer, and how API Gateway interprets the
response from Lambda. Supported values are 1.0 and 2.0 .
For more information, see Lambda Authorizers Payload Format. |
enableSimpleResponses | boolean |
For HTTP APIs, specifies whether a request authorizer returns a
Boolean value or an IAM policy. Supported only for authorizers
with an authorizerPayloadFormatVersion of 2.0. If enabled, the
Lambda authorizer function returns a Boolean value. |
$version: "2"
namespace ns.foo
use aws.apigateway#authorizer
use aws.apigateway#authorizers
use aws.auth#sigv4
use aws.protocols#restJson1
@restJson1
@sigv4(name: "weather")
@authorizer("arbitrary-name")
@authorizers(
"arbitrary-name": {
scheme: sigv4
type: "request"
uri: "arn:foo:baz"
credentials: "arn:foo:bar"
identitySource: "mapping.expression"
identityValidationExpression: "[A-Z]+"
resultTtlInSeconds: 100
authorizerPayloadFormatVersion: "2.0"
enableSimpleResponses: true
}
)
service Weather {
version: "2018-03-17"
}
Note
This trait should be considered internal-only and not exposed to your customers.
aws.apigateway#authorizer
trait#
- Summary
- Applies a Lambda authorizer to a service, resource, or operation. Authorizers are resolved hierarchically: an operation inherits the effective authorizer applied to a parent resource or operation.
- Trait selector
:is(service, resource, operation)
A service, resource, or operation
- Value type
string
value that MUST reference one of the keys in the aws.apigateway#authorizers trait of the service that contains the shape.
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
string
value set to one of the following:Value Description full
The parameters and body of a request are validated. params-only
Only the parameters of a request are validated. body-only
Only 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 | string |
Required. The type of integration with the specified backend. Valid values are:
|
uri | string |
Required. The endpoint URI of the backend. For integrations of
the aws type, this is an ARN value. For the HTTP integration,
this is the URL of the HTTP endpoint including the https or
http scheme. |
httpMethod | string |
Required. Specifies the integration's HTTP method type
(for example, POST ). For Lambda function invocations, the value
must be POST . |
credentials | string |
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 | string |
Specifies how a request payload of unmapped content type is passed
through the integration request without modification. Supported
values are when_no_templates , when_no_match , and never .
For more information, see Integration.passthroughBehavior. |
contentHandling | ContentHandling string | Request payload content handling. |
timeoutInMillis | integer |
Integration timeouts between 50 ms and 29,000 ms. |
connectionId | string |
The ID of a VpcLink for the private integration. |
connectionType | string |
The type of the network connection to the integration endpoint.
The valid value is INTERNET for connections through the public
routable internet or VPC_LINK for private connections between
API Gateway and a network load balancer in a VPC. The default
value is INTERNET . |
cacheNamespace | string |
An API-specific tag group of related cached parameters. |
payloadFormatVersion | string |
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 | list<string> |
A list of request parameter names whose values are to be cached. |
requestParameters | map of requestParameters structure to request parameters |
Specifies mappings from method request parameters to integration request parameters. Supported request parameters are querystring, path, header, and body. |
requestTemplates | map of media types to requestTemplates structure |
Mapping templates for a request payload of specified media types. |
responses | map of response codes to responses structure |
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 | string |
Specifies how a request payload of unmapped content type is passed
through the integration request without modification. Supported
values are when_no_templates , when_no_match , and never .
For more information, see Integration.passthroughBehavior. |
requestParameters | map of requestParameters structure to request parameters |
Specifies mappings from method request parameters to integration request parameters. Supported request parameters are querystring, path, header, and body. |
requestTemplates | map of media types to requestTemplates structure |
Mapping templates for a request payload of specified media types. |
responses | map of response codes to responses structure |
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.