Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apigwv2: AuthorizerPayloadFormatVersion not a field in HttpLambdaAuthorizerProps #21492

Open
2 tasks
elad-asaf opened this issue Aug 7, 2022 · 4 comments · May be fixed by #30843
Open
2 tasks

apigwv2: AuthorizerPayloadFormatVersion not a field in HttpLambdaAuthorizerProps #21492

elad-asaf opened this issue Aug 7, 2022 · 4 comments · May be fixed by #30843
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2

Comments

@elad-asaf
Copy link

Describe the feature

The HttpLambdaAuthorizerProps includes a response type field, however, you can't choose the IAM response type and set the version of the payload format as 2.0.
The IAM payload 2.0 behaves a bit differently than 1.0, as described here:
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html#http-api-lambda-authorizer.payload-format-response
It would be great to be able to set this as a prop in HttpLambdaAuthorizerProps.

Use Case

APIGW deployment using CDK would be much smoother with this.

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.35.0

Environment details (OS name and version, etc.)

Ubuntu 20.04

@elad-asaf elad-asaf added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 7, 2022
@github-actions github-actions bot added the @aws-cdk/aws-iam Related to AWS Identity and Access Management label Aug 7, 2022
@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels Sep 2, 2022
@rix0rrr rix0rrr removed their assignment Sep 2, 2022
@khushail khushail added p1.5 and removed p1 labels May 16, 2023
@otaviomacedo otaviomacedo added p2 and removed p1.5 labels May 22, 2023
@montoyan877
Copy link

Any solution to this? I'm in the same

@yasamoka
Copy link

yasamoka commented Jul 19, 2024

In case the PR is not (yet) accepted, for anyone facing this issue, you can use the lower-level HttpAuthorizer construct that is used internally for HttpLambdaAuthorizer (here and here). Here is a full example:

import {
  aws_apigatewayv2 as apigateway,
  aws_ecr as ecr,
  aws_lambda as lambda,
  Names,
  Stack,
  StackProps,
} from "aws-cdk-lib";
import {
  AuthorizerPayloadVersion,
  HttpAuthorizer,
  HttpAuthorizerType,
} from "aws-cdk-lib/aws-apigatewayv2";
import { Construct } from "constructs";
import * as ecrdeploy from "cdk-ecr-deployment";
import { DockerImageAsset } from "aws-cdk-lib/aws-ecr-assets";
import { IFunction } from "aws-cdk-lib/aws-lambda";
import { ServicePrincipal } from "aws-cdk-lib/aws-iam";

export class CdkStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const repo = new ecr.Repository(this, "repo");

    const dummyImage = new DockerImageAsset(this, "dummy-image", {
      assetName: "dummy-image",
      directory: "../dummy",
    });

    const ecrDeployment = new ecrdeploy.ECRDeployment(
      this,
      "ecr-deployment",
      {
        src: new ecrdeploy.DockerImageName(dummyImage.imageUri),
        dest: new ecrdeploy.DockerImageName(
          `${repo.repositoryUri}:latest`
        ),
      }
    );

    const api = new apigateway.HttpApi(this, "api");

    const lambdaFunction = new lambda.Function(this, "lambda", {
      runtime: lambda.Runtime.FROM_IMAGE,
      code: lambda.Code.fromEcrImage(repo),
      handler: lambda.Handler.FROM_IMAGE,
    });

    lambdaFunction.node.addDependency(ecrDeployment);

    const authorizer = new HttpAuthorizer(this, "authorizer", {
      httpApi: api,
      identitySource: [
        '$request.header.Authorization',
      ],
      type: HttpAuthorizerType.LAMBDA,
      enableSimpleResponses: false,
      payloadFormatVersion: AuthorizerPayloadVersion.VERSION_2_0,
      authorizerUri: lambdaAuthorizerArn(lambdaFunction),
    });

    lambdaFunction.addPermission(
      `${Names.nodeUniqueId(authorizer.node)}-Permission`,
      {
        scope: api,
        principal: new ServicePrincipal("apigateway.amazonaws.com"),
        sourceArn: this.formatArn({
          service: "execute-api",
          resource: api.apiId,
          resourceName: `authorizers/${authorizer.authorizerId}`,
        }),
      }
    );
  }
}

function lambdaAuthorizerArn(handler: IFunction) {
  return `arn:${Stack.of(handler).partition}:apigateway:${Stack.of(handler).region
    }:lambda:path/2015-03-31/functions/${handler.functionArn}/invocations`;
}

Alternatively, you can copy the contents of lambda.ts from the PR (here), remove HttpLambdaResponseType, and just use the new version of HttpLambdaAuthorizer you now have, which also implements the required IHttpRouteAuthorizer interface, in your stack. This may be the only option if you want to attach authorizers to separate routes.

@tombeuckelaere
Copy link

tombeuckelaere commented Aug 14, 2024

@yasamoka Thanks for providing an example with a lower level L2 construct.
I'm also impacted by this issue for AWS CDK .NET which I previously solved by creating an CfnAuthorizer. This lead me however to constructing other L1 constructs as well because L1 and L2 don't mix which is a pitty. I'm happily expecting the release of your pending PR 😇 . And thanks for the efforts you've already made.

@Tobias243
Copy link

I also stumbled over this, thanks for putting in the effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2
Projects
None yet
8 participants