How to setup Lambda Authorizer for a REST API added in Amplify,

0

I cannot find a way to set up Lambda Authorizer for the Rest API I added using AWS amplify in my amplify app. The only options it shows are for authenticated and unauthenticated users and does not let me configure anything else.

Maybe it can be done with overrides.ts but I could not find an example of how to do it, any guidance will be appreciated.

1回答
0

This is how I solved it. amplify override API to create an overrides.ts file and add the following.

 // Create a lambda authorizer
  resources.restApi.addPropertyOverride("Body.securityDefinitions", {
    Custom: {
      type: "apiKey",
      name: "Authorization",
      in: "header",
      "x-amazon-apigateway-authtype": "custom",
      "x-amazon-apigateway-authorizer": {
        type: "request",
        authorizerUri: {
          "Fn::Sub":
            "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AuthorizerFunctionName}/invocations",
        },
        authorizerResultTtlInSeconds: 300,
        identitySource: "method.request.header.Authorization",
      },
    },
  });

  // For every path in your REST API, add the authorizer for all methods
  for (const path in resources.restApi.body.paths) {
    resources.restApi.addPropertyOverride(
      `Body.paths.${path}.x-amazon-apigateway-any-method.parameters`,
      [
        ...resources.restApi.body.paths[path]["x-amazon-apigateway-any-method"]
          .parameters,
        {
          name: "Authorization",
          in: "header",
          required: false,
          type: "string",
        },
      ]
    );
    // Use your new Cognito User Pool authorizer for security
    resources.restApi.addPropertyOverride(
      `Body.paths.${path}.x-amazon-apigateway-any-method.security`,
      [ { Custom: [], }, ]
    );
  }
sarin
回答済み 1年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ