如何在AWS API网关V2(HTTP)中与Lambda别名和Terraform中的舞台变量进行集成

发布于 2025-02-02 14:04:09 字数 2319 浏览 5 评论 0原文

我有几个带有别名的lambdas(开发,生产,登台..),当我尝试与他这样的路线进行集成时,

resource "aws_apigatewayv2_route" "http_routes" {
  for_each = local.lambda_integrations

  api_id    = aws_apigatewayv2_api.api_http.id
  route_key = each.key

  target = "integrations/${aws_apigatewayv2_integration.http_integrations[each.key].id}"
}

resource "aws_apigatewayv2_integration" "http_integrations" {
  for_each = local.lambda_integrations

  api_id              = aws_apigatewayv2_api.api_http.id
  integration_type    = "AWS_PROXY"
  integration_method  = "POST"

  integration_uri       = "arn:aws:apigateway:${var.auth.region}:lambda:path/2015-03-31/functions/${module.lambdas_functions[index(module.lambdas_functions.*.function_name,each.value.lambda)].arn}:$${stageVariables.alias}/invocations"
}

这与

# Main Permission
resource "aws_lambda_permission" "permission_lambda" {
  for_each = local.lambda_integrations

  statement_id  = can(each.value.statement) ? each.value.statement : "AllowExecutionFromAPIGateway"
  action        = "lambda:InvokeFunction"
  
  function_name = module.lambdas_functions[
    index(module.lambdas_functions.*.function_name,each.value.lambda)
  ].function_name

  principal     = "apigateway.amazonaws.com"
  source_arn    = "${aws_apigatewayv2_api.api_http.execution_arn}/*/*"
}

# Stage develop permission
resource "aws_lambda_permission" "permission_lambda_alias_develop" {
  for_each = local.lambda_integrations

  statement_id  = can(each.value.statement) ? each.value.statement : "AllowExecutionFromAPIGateway"
  action        = "lambda:InvokeFunction"
  
  function_name = module.lambdas_functions[
    index(module.lambdas_functions.*.function_name,each.value.lambda)
  ].function_name

  principal     = "apigateway.amazonaws.com"
  source_arn    = "${aws_apigatewayv2_api.api_http.execution_arn}/*/*"

  qualifier     = "develop"
}

CloudWatch中的[数字]版本一起起作用,但是在Lambda触发器中,我得到了:

< a href =“ https://i.sstatic.net/xlvkf.png” rel =“ nofollow noreferrer”>

,尽管代码有效,但在我看来是正确的。

什么可能是解决这种情况的最好和正确的方法? 问候

I have several lambdas with aliases (develop, production, staging..) and when I try to make an integration with his route like this

resource "aws_apigatewayv2_route" "http_routes" {
  for_each = local.lambda_integrations

  api_id    = aws_apigatewayv2_api.api_http.id
  route_key = each.key

  target = "integrations/${aws_apigatewayv2_integration.http_integrations[each.key].id}"
}

resource "aws_apigatewayv2_integration" "http_integrations" {
  for_each = local.lambda_integrations

  api_id              = aws_apigatewayv2_api.api_http.id
  integration_type    = "AWS_PROXY"
  integration_method  = "POST"

  integration_uri       = "arn:aws:apigateway:${var.auth.region}:lambda:path/2015-03-31/functions/${module.lambdas_functions[index(module.lambdas_functions.*.function_name,each.value.lambda)].arn}:${stageVariables.alias}/invocations"
}

and

# Main Permission
resource "aws_lambda_permission" "permission_lambda" {
  for_each = local.lambda_integrations

  statement_id  = can(each.value.statement) ? each.value.statement : "AllowExecutionFromAPIGateway"
  action        = "lambda:InvokeFunction"
  
  function_name = module.lambdas_functions[
    index(module.lambdas_functions.*.function_name,each.value.lambda)
  ].function_name

  principal     = "apigateway.amazonaws.com"
  source_arn    = "${aws_apigatewayv2_api.api_http.execution_arn}/*/*"
}

# Stage develop permission
resource "aws_lambda_permission" "permission_lambda_alias_develop" {
  for_each = local.lambda_integrations

  statement_id  = can(each.value.statement) ? each.value.statement : "AllowExecutionFromAPIGateway"
  action        = "lambda:InvokeFunction"
  
  function_name = module.lambdas_functions[
    index(module.lambdas_functions.*.function_name,each.value.lambda)
  ].function_name

  principal     = "apigateway.amazonaws.com"
  source_arn    = "${aws_apigatewayv2_api.api_http.execution_arn}/*/*"

  qualifier     = "develop"
}

this works with a [number] version in CloudWatch, but in the lambda trigger I get this:

enter image description here
enter image description here

and although the code works, it does not seem to me to be correct.

what could be the best and correct approach to this situation?.
Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2025-02-09 14:04:09

对我有用的是:

  1. 破坏了API网关组件(使用的目标)

    Terraform Destrain-Var-File var_files -target api_and_lambda_modules

  2. 将舞台重命名为“ $ default”

  3. 使 perm_source_arn 参数的值
    “ arn:aws:execute-api:$ {region}:$ {account_id}:$ {api_id}/*/*/*$ {route_key_key_path}”

之后,只是重新应用。

What worked for me is this:

  1. Destroyed the API Gateway components (used target)

    terraform destroy --var-file VAR_FILES --target API_AND_LAMBDA_MODULES

  2. Renamed the stage to "$default"

  3. Made the value of the perm_source_arn parameter as
    "arn:aws:execute-api:${REGION}:${ACCOUNT_ID}:${API_ID}/*/*${ROUTE_KEY_PATH}"

After these, just re-apply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文