AppSyncLambdaAuthorizerConfig Struct
Configuration for Lambda authorization in AppSync.
Note that you can only have a single AWS Lambda function configured to authorize your API.
| Source | aws-appsync/lib/auth-config.ts |
|---|---|
| Type last changed |
Example
ts
import * as lambda from 'aws-cdk-lib/aws-lambda';
declare const handler: lambda.Function;
const iamProvider: appsync.AppSyncAuthProvider = {
authorizationType: appsync.AppSyncAuthorizationType.IAM,
};
const apiKeyProvider: appsync.AppSyncAuthProvider = {
authorizationType: appsync.AppSyncAuthorizationType.API_KEY,
};
const lambdaProvider: appsync.AppSyncAuthProvider = {
authorizationType: appsync.AppSyncAuthorizationType.LAMBDA,
lambdaAuthorizerConfig: {
handler,
resultsCacheTtl: Duration.minutes(6),
validationRegex: 'test',
},
};
const api = new appsync.EventApi(this, 'api', {
apiName: 'api',
authorizationConfig: {
// set auth providers
authProviders: [
iamProvider,
apiKeyProvider,
lambdaProvider,
],
connectionAuthModeTypes: [
appsync.AppSyncAuthorizationType.IAM, // IAM authorization to connect
],
defaultPublishAuthModeTypes: [
appsync.AppSyncAuthorizationType.API_KEY, // API key authorization to publish
],
defaultSubscribeAuthModeTypes: [
appsync.AppSyncAuthorizationType.LAMBDA, // Lambda authorization to subscribe
],
}
});
api.addChannelNamespace('default');
python
import aws_cdk.aws_lambda as lambda_
# handler: lambda.Function
iam_provider = appsync.AppSyncAuthProvider(
authorization_type=appsync.AppSyncAuthorizationType.IAM
)
api_key_provider = appsync.AppSyncAuthProvider(
authorization_type=appsync.AppSyncAuthorizationType.API_KEY
)
lambda_provider = appsync.AppSyncAuthProvider(
authorization_type=appsync.AppSyncAuthorizationType.LAMBDA,
lambda_authorizer_config=appsync.AppSyncLambdaAuthorizerConfig(
handler=handler,
results_cache_ttl=Duration.minutes(6),
validation_regex="test"
)
)
api = appsync.EventApi(self, "api",
api_name="api",
authorization_config=appsync.EventApiAuthConfig(
# set auth providers
auth_providers=[iam_provider, api_key_provider, lambda_provider
],
connection_auth_mode_types=[appsync.AppSyncAuthorizationType.IAM
],
default_publish_auth_mode_types=[appsync.AppSyncAuthorizationType.API_KEY
],
default_subscribe_auth_mode_types=[appsync.AppSyncAuthorizationType.LAMBDA
]
)
)
api.add_channel_namespace("default")
java
import software.amazon.awscdk.services.lambda.*;
Function handler;
AppSyncAuthProvider iamProvider = AppSyncAuthProvider.builder()
.authorizationType(AppSyncAuthorizationType.IAM)
.build();
AppSyncAuthProvider apiKeyProvider = AppSyncAuthProvider.builder()
.authorizationType(AppSyncAuthorizationType.API_KEY)
.build();
AppSyncAuthProvider lambdaProvider = AppSyncAuthProvider.builder()
.authorizationType(AppSyncAuthorizationType.LAMBDA)
.lambdaAuthorizerConfig(AppSyncLambdaAuthorizerConfig.builder()
.handler(handler)
.resultsCacheTtl(Duration.minutes(6))
.validationRegex("test")
.build())
.build();
EventApi api = EventApi.Builder.create(this, "api")
.apiName("api")
.authorizationConfig(EventApiAuthConfig.builder()
// set auth providers
.authProviders(List.of(iamProvider, apiKeyProvider, lambdaProvider))
.connectionAuthModeTypes(List.of(AppSyncAuthorizationType.IAM))
.defaultPublishAuthModeTypes(List.of(AppSyncAuthorizationType.API_KEY))
.defaultSubscribeAuthModeTypes(List.of(AppSyncAuthorizationType.LAMBDA))
.build())
.build();
api.addChannelNamespace("default");
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.Lambda;
Function handler;
var iamProvider = new AppSyncAuthProvider {
AuthorizationType = AppSyncAuthorizationType.IAM
};
var apiKeyProvider = new AppSyncAuthProvider {
AuthorizationType = AppSyncAuthorizationType.API_KEY
};
var lambdaProvider = new AppSyncAuthProvider {
AuthorizationType = AppSyncAuthorizationType.LAMBDA,
LambdaAuthorizerConfig = new AppSyncLambdaAuthorizerConfig {
Handler = handler,
ResultsCacheTtl = Duration.Minutes(6),
ValidationRegex = "test"
}
};
var api = new EventApi(this, "api", new EventApiProps {
ApiName = "api",
AuthorizationConfig = new EventApiAuthConfig {
// set auth providers
AuthProviders = new [] { iamProvider, apiKeyProvider, lambdaProvider },
ConnectionAuthModeTypes = new [] { AppSyncAuthorizationType.IAM },
DefaultPublishAuthModeTypes = new [] { AppSyncAuthorizationType.API_KEY },
DefaultSubscribeAuthModeTypes = new [] { AppSyncAuthorizationType.LAMBDA }
}
});
api.AddChannelNamespace("default");
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import lambda "github.com/aws/aws-cdk-go/awscdk"
var handler Function
iamProvider := &AppSyncAuthProvider{
AuthorizationType: appsync.AppSyncAuthorizationType_IAM,
}
apiKeyProvider := &AppSyncAuthProvider{
AuthorizationType: appsync.AppSyncAuthorizationType_API_KEY,
}
lambdaProvider := &AppSyncAuthProvider{
AuthorizationType: appsync.AppSyncAuthorizationType_LAMBDA,
LambdaAuthorizerConfig: &AppSyncLambdaAuthorizerConfig{
Handler: *Handler,
ResultsCacheTtl: awscdk.Duration_Minutes(jsii.Number(6)),
ValidationRegex: jsii.String("test"),
},
}
api := appsync.NewEventApi(this, jsii.String("api"), &EventApiProps{
ApiName: jsii.String("api"),
AuthorizationConfig: &EventApiAuthConfig{
// set auth providers
AuthProviders: []AppSyncAuthProvider{
iamProvider,
apiKeyProvider,
lambdaProvider,
},
ConnectionAuthModeTypes: []AppSyncAuthorizationType{
appsync.AppSyncAuthorizationType_IAM,
},
DefaultPublishAuthModeTypes: []AppSyncAuthorizationType{
appsync.AppSyncAuthorizationType_API_KEY,
},
DefaultSubscribeAuthModeTypes: []AppSyncAuthorizationType{
appsync.AppSyncAuthorizationType_LAMBDA,
},
},
})
api.AddChannelNamespace(jsii.String("default"))
Properties
The authorizer lambda function.
resultsCacheTtloptionalDurationresults_cache_ttloptionalDurationresultsCacheTtloptionalDurationResultsCacheTtloptionalDurationResultsCacheTtloptionalDuration = Duration.minutes(5) #How long the results are cached.
Disable caching by setting this to 0.
validationRegexoptionalstringvalidation_regexoptionalstrvalidationRegexoptionalStringValidationRegexoptionalstringValidationRegexoptional*string #A regular expression for validation of tokens before the Lambda function is called.
Default: no regex filter will be applied.