【问题标题】:Terraform API Gateway v2 Authorizer - Automatically grant API Gateway permission to invoke your Lambda functionTerraform API Gateway v2 Authorizer - 自动授予 API Gateway 调用 Lambda 函数的权限
【发布时间】:2022-03-29 19:27:40
【问题描述】:

在 AWS 控制台中,可以为“自动授予 API Gateway ”创建一个具有真/假值的 API Gateway Authorizer

但是,我没有看到此标志通过 Terraform 中的 AWS 提供商公开用于 aws_apigatewayv2_authorizer 资源。

有没有办法通过 Terraform 进行设置?

【问题讨论】:

    标签: amazon-web-services terraform aws-api-gateway terraform-provider-aws


    【解决方案1】:

    hashicorp/aws@4.8.0 提供程序也遇到了同样的问题。为了解决这个问题,我必须创建一个 IAM 角色并将授权者中的角色分配为authorizer_credentials_arn

    data "aws_iam_policy_document" "apig_lambda_policy" {
      statement {
        actions = [
          "lambda:InvokeFunction",
        ]
        effect    = "Allow"
        resources = [aws_lambda_function.authorizer_lambda.arn]
        sid       = "ApiGatewayInvokeLambda"
      }
    }
    
    data "aws_iam_policy_document" "apig_lambda_role_assume" {
      statement {
        actions = [
          "sts:AssumeRole",
        ]
        effect = "Allow"
        principals {
          type        = "Service"
          identifiers = ["apigateway.amazonaws.com"]
        }
      }
    }
    
    resource "aws_iam_role" "apig_lambda_role" {
      name               = "apigateway-authorize-lambda-role"
      assume_role_policy = data.aws_iam_policy_document.apig_lambda_role_assume.json
    }
    
    resource "aws_iam_policy" "apig_lambda" {
      name   = "apig-lambda-policy"
      policy = data.aws_iam_policy_document.apig_lambda_policy.json
    }
    
    resource "aws_iam_role_policy_attachment" "apig_lambda_role_to_policy" {
      role       = aws_iam_role.apig_lambda_role.name
      policy_arn = aws_iam_policy.apig_lambda.arn
    }
    
    resource "aws_apigatewayv2_authorizer" "auth" {
      api_id                            = aws_apigatewayv2_api.api.id
      authorizer_type                   = "REQUEST"
      authorizer_uri                    = aws_lambda_function.authorizer_lambda.invoke_arn
      authorizer_credentials_arn        = aws_iam_role.apig_lambda_role.arn
      authorizer_payload_format_version = "2.0"
      authorizer_result_ttl_in_seconds  = 1
      enable_simple_responses           = true
      identity_sources                  = ["$request.header.Authorization"]
      name                              = "lambda-authorizer"
    }
    

    【讨论】:

      【解决方案2】:

      在 terraform 中,您需要手动创建 IAM 语句才能完成这项工作。最简单的方法是使用与您的 lambda 关联的资源策略:

      resource "aws_lambda_permission" "my_authorizer_lambda_permission" {
          statement_id  = "AllowAPIGatewayInvoke"
          action        = "lambda:InvokeFunction"
          function_name = aws_lambda_function.onconnect.function_name
          principal     = "apigateway.amazonaws.com"
      
          source_arn    = "${aws_apigatewayv2_api.my_api.execution_arn}/authorizers/${aws_apigatewayv2_authorizer.my_authorizer.id}"
      }
      

      【讨论】:

      • 这似乎确实有效,即使我在控制台中查看 lambda 配置时收到此错误消息:“ID 为 {id} 的 API 不包含路径 /* 具有集成的路由arn:aws:lambda:us-east-1:{account-id}:function:{function-name}" 你知道这是否是 AWS 中的一个已知错误吗?
      • 它看起来像一个错误,它期望存在一个 REST (v1) api,并基于此给出一个错误。
      • 我也试过这个。不幸的是,对我来说它没有用。 “调用权限”中仍然没有配置任何内容,并且请求一直响应 Internal Server Error
      猜你喜欢
      • 2016-10-27
      • 2017-02-15
      • 2020-09-21
      • 1970-01-01
      • 2019-12-25
      • 2020-07-11
      • 2018-10-28
      • 1970-01-01
      • 2020-09-23
      相关资源
      最近更新 更多