aws-cdk-lib.aws_apigateway.RequestAuthorizer

class RequestAuthorizer (construct)

LanguageType name
.NETAmazon.CDK.AWS.APIGateway.RequestAuthorizer
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsapigateway#RequestAuthorizer
Javasoftware.amazon.awscdk.services.apigateway.RequestAuthorizer
Pythonaws_cdk.aws_apigateway.RequestAuthorizer
TypeScript (source)aws-cdk-lib » aws_apigateway » RequestAuthorizer

Implements IConstruct, IDependable, IResource, IAuthorizer

Request-based lambda authorizer that recognizes the caller's identity via request parameters, such as headers, paths, query strings, stage variables, or context variables.

Based on the request, authorization is performed by a lambda function.

Example

declare const authFn: lambda.Function;
declare const books: apigateway.Resource;

const auth = new apigateway.RequestAuthorizer(this, 'booksAuthorizer', {
  handler: authFn,
  identitySources: [apigateway.IdentitySource.header('Authorization')]
});

books.addMethod('GET', new apigateway.HttpIntegration('http://amazon.com'), {
  authorizer: auth
});

Initializer

new RequestAuthorizer(scope: Construct, id: string, props: RequestAuthorizerProps)

Parameters

  • scope Construct
  • id string
  • props RequestAuthorizerProps

Construct Props

NameTypeDescription
handlerIFunctionThe handler for the authorizer lambda function.
identitySourcesstring[]An array of request header mapping expressions for identities.
assumeRole?IRoleAn optional IAM role for APIGateway to assume before calling the Lambda-based authorizer.
authorizerName?stringAn optional human friendly name for the authorizer.
resultsCacheTtl?DurationHow long APIGateway should cache the results.

handler

Type: IFunction

The handler for the authorizer lambda function.

The handler must follow a very specific protocol on the input it receives and the output it needs to produce. API Gateway has documented the handler's input specification and output specification.


identitySources

Type: string[]

An array of request header mapping expressions for identities.

Supported parameter types are Header, Query String, Stage Variable, and Context. For instance, extracting an authorization token from a header would use the identity source IdentitySource.header('Authorizer').

Note: API Gateway uses the specified identity sources as the request authorizer caching key. When caching is enabled, API Gateway calls the authorizer's Lambda function only after successfully verifying that all the specified identity sources are present at runtime. If a specified identify source is missing, null, or empty, API Gateway returns a 401 Unauthorized response without calling the authorizer Lambda function.

See also: https://docs.aws.amazon.com/apigateway/api-reference/link-relation/authorizer-create/#identitySource


assumeRole?

Type: IRole (optional, default: A resource policy is added to the Lambda function allowing apigateway.amazonaws.com to invoke the function.)

An optional IAM role for APIGateway to assume before calling the Lambda-based authorizer.

The IAM role must be assumable by 'apigateway.amazonaws.com'.


authorizerName?

Type: string (optional, default: the unique construct ID)

An optional human friendly name for the authorizer.

Note that, this is not the primary identifier of the authorizer.


resultsCacheTtl?

Type: Duration (optional, default: Duration.minutes(5))

How long APIGateway should cache the results.

Max 1 hour. Disable caching by setting this to 0.

Properties

NameTypeDescription
authorizerArnstringThe ARN of the authorizer to be used in permission policies, such as IAM and resource-based grants.
authorizerIdstringThe id of the authorizer.
authorizerPropsCfnAuthorizerProps
envResourceEnvironmentThe environment this resource belongs to.
handlerIFunctionThe Lambda function handler that this authorizer uses.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.
authorizationType?AuthorizationTypeThe authorization type of this authorizer.
restApiId?string
role?IRoleThe IAM role that the API Gateway service assumes while invoking the Lambda function.

authorizerArn

Type: string

The ARN of the authorizer to be used in permission policies, such as IAM and resource-based grants.


authorizerId

Type: string

The id of the authorizer.


authorizerProps

Type: CfnAuthorizerProps


env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


handler

Type: IFunction

The Lambda function handler that this authorizer uses.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.


authorizationType?

Type: AuthorizationType (optional)

The authorization type of this authorizer.


restApiId?

Type: string (optional)


role?

Type: IRole (optional)

The IAM role that the API Gateway service assumes while invoking the Lambda function.

Methods

NameDescription
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
toString()Returns a string representation of this construct.
protected lazyRestApiId()Returns a token that resolves to the Rest Api Id at the time of synthesis.
protected setupPermissions()Sets up the permissions necessary for the API Gateway service to invoke the Lambda function.

applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


protected lazyRestApiId()

protected lazyRestApiId(): string

Returns

  • string

Returns a token that resolves to the Rest Api Id at the time of synthesis.

Throws an error, during token resolution, if no RestApi is attached to this authorizer.


protected setupPermissions()

protected setupPermissions(): void

Sets up the permissions necessary for the API Gateway service to invoke the Lambda function.