[apigateway] add support for lambda token authorizer
When I specify an authorizerType of CUSTOM for my Lambda integration it doesn't actually populate when deployed to AWS and I have to manually update.
This is the authorizer spec;
this.aggIdApi.addMethod("GET", new LambdaIntegration(getterFn), {
authorizationType: AuthorizationType.Custom,
authorizerId: authorizer.authorizerId
});
When deployed it only has an authorizerId

however when I manually update the authorizerType is populated and everything works perfectly;

Looks like a bug
Happy to do a PR if you can point me in the right direction @eladb
The code the synthesizes the method resource is here. Not 100% sure why this is not propagating.
Hi @eladb . Just revisiting this and I wasn't able to see any issue with the code there either. I noticed you removed the gap label. I've done a bit more digging and it looks like the authorizationType is getting populated now but I'm still getting an error. The only difference I can see is that the requestParameters and requestModels is being populated with an empty object in CDK deployment.

Looking into it
The label was me, I must have misclicked in the UI. Sorry about that.
@brianfoody I am trying to understand what the issue is exactly at this point. Any chance you can submit a more complete code snippet so we can try to reproduce?
Hi @eladb . Yes sorry it's been a confusing one to hunt down. I turned on CloudWatch logs for my API Gateway method and I can see now that the actual error is that APIGateway doesn't have permission to invoke the authorizer. I'm guessing when I do it manually it adds the permissions as well.
I just need to figure out how to give the API Gateway method permission to call the authorizer now through CDK.

Probably ties into https://github.com/awslabs/aws-cdk/issues/1465 somewhat? Or am I wrong?
Authorizers are simply not supported by our L2 library yet, so yes- manual permission grants are needed.
You should be able to use https://awslabs.github.io/aws-cdk/refs/_aws-cdk_aws-lambda.html#@aws-cdk/aws-lambda.Function.grantInvoke
Well 6 hours later I have resolved the issue. The grantInvoke didn't actually work for me. I had to create a custom script to attach the right permissions in the end as it required a weird /authorizers sourceARN string. This was the code that fixed the issue for me in the end and replicated what was happening when I manually attached an authorizer.
I guess I should close for now if it's a known issue based on L2 library but it would be good if this could be documented somewhere.
@brianfoody apologies for the unpleasant experience, and thanks for sharing your solution. I am reopening this issue and repurposing it as “support lambda authorizers”.
I managed to get this working with the following code (along with cors support): https://github.com/erezrokah/cdk-serverless-example/blob/9627a92fab7186dcecd13346b17fd9f62f9ad36a/services/api-service/lib/apiService.ts#L106
The repository is still work in progress...
Edit: updated the link to a new location
@erezrokah is this working for you without doing something else? Not working for me at the moment..
@erezrokah is this working for you without doing something else? Not working for me at the moment..
Hey @AlexRex, I did get it to work (a few months ago...).
Have you tried cloning my repository https://github.com/erezrokah/cdk-serverless-example/ ?
You'll need to create an .env file based on .env.example with the proper values.
Then use the readme for guidance (you'll need to follow this since I'm using Auth0 management API to automate some stuff)
I was able to get Authorizers to work by making a custom class that extends CfnAuthorizer. I've made a quick example here https://github.com/IainCole/aws-cdk/blob/ic_add_apig_authorizer/packages/%40aws-cdk/aws-apigateway/lib/authorizer.ts#L17
@eladb would it be worth me submitting a PR along these lines?
+1. Really needed... thank you.
I want try to undestand why it doesn't work on python. So in #4532 you told me that it's incorrect use cfnxxx and non cfnxxx construct. I know, but It seems that all solutions model the cfnxxx.
so I tried to "translate" in python this -> https://github.com/IainCole/aws-cdk/blob/ic_add_apig_authorizer/packages/%40aws-cdk/aws-apigateway/lib/authorizer.ts#L17
class AuthorizerProps(CfnAuthorizerProps):
pass
class Authorizer (CfnAuthorizer):
@property
def authorizer_id(self):
return self.ref
def __init__(self, scope: core.Construct, id: str, auth_props: AuthorizerProps, **kwargs) -> None:
super().__init__(scope, id,
rest_api_id=auth_props.rest_api_id,
type=auth_props.type,
name=auth_props.name,
identity_source=auth_props.identity_source,
identity_validation_expression=auth_props.identity_validation_expression,
authorizer_uri=auth_props.authorizer_uri,
**kwargs)
auth=Authorizer(self,"authorizer",auth_props=AuthorizerProps(
rest_api_id=apigw_autoscaling.rest_api_id,
type="TOKEN",
name="test",
identity_source="method.request.header.AUTH",
identity_validation_expression="TESTAUTH",
authorizer_uri="".join(["arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/",lamdbda_api_authorizer.function_arn,"/invocations"])
))
auto_scaling_res.add_method('POST', authorization_type=apigw.AuthorizationType.CUSTOM, authorizer=auth.authorizer_id)
but I receive this error: jsii.errors.JSIIError: Expected object reference, got "${Token[TOKEN.89]}"
why?
please don't tell me "translate from this https://docs.aws.amazon.com/cdk/latest/guide/multiple_languages.html"
I've read the doc. How can I use the IAuthorizer? is it possible or a this time do I have to use CfnMethod with CfnAuthorizer?
I don't really know python but I feel like it should look like this:
auto_scaling_res.add_method('POST', authorization_type=apigw.AuthorizationType.CUSTOM, authorizer=auth)
Rather than:
auto_scaling_res.add_method('POST', authorization_type=apigw.AuthorizationType.CUSTOM, authorizer=auth.authorizer_id)