循环依赖地形

问题描述 投票:0回答:2

我有 terraform 的循环依赖问题。我有一个用户池

user_pool
,当用户注册时,它会调用 lambda
user_signup_lambda
作为触发器。此 lambda 还需要用户池的 Id 作为环境变量。

我的地形看起来像这样

resource "aws_lambda_function" "user_signup_lambda" {
  function_name = "user_signup_lambda"
  filename = var.file_name
  source_code_hash = filebase64sha256(var.file_name)
  handler = var.handler
  runtime = var.runtime
  memory_size = var.memory_size
  timeout = var.timeout
  role = var.iam_role_arn

  environment {
    variables = aws_cognito_user_pool.user_pool.id
  }
}

resource "aws_cognito_user_pool" "user_pool" {
  name = "some-user-pool"

  ....other config

  lambda_config {
     post_confirmation = module.user_signup_lambda.arn
  }

这会导致以下错误:

Error: Cycle: module.user_signup_lambda.var.environment_variables, module.user_signup_lambda.aws_lambda_function.lambda_function, module.user_signup_lambda.output.arn, aws_cognito_user_pool.user_pool

除了对用户池 ID 进行硬编码之外,还有其他方法吗?

aws-lambda terraform amazon-cognito aws-userpools
2个回答
4
投票

我们遇到了完全相同的问题。我们最终将包含 UserPool 名称的变量传递给 UserPool 和 Lambda 触发器资源。

然后 lambda 从其名称中获取 Cognito 用户池信息。

这样这两个资源之间就不存在循环依赖。

# Module containing Cognito userPool
resource "aws_cognito_user_pool" "default" {

 ...
  # Triggers
  lambda_config {
    post_confirmation = module.cognito_post_confirmation.lambda_post_confirmation.arn
  }
  name = var.cognito_identity_pool_name
} 


module "cognito_post_confirmation" {
  source                     = "./triggers/post-confirmation"
  cognito_identity_pool_name = var.cognito_identity_pool_name
  ...
}

0
投票

您是否考虑过使用事件负载?看起来像这样。

    { 
      version: '1',
      userPoolId: 'ap-southeast-BLABLA',
    }

事件本身有userPoolId。如果您使用它,则不需要环境变量。

这就是我处理这个问题的方法。

    const postConfirmationHanlder = async (event) => {
        const userPoolId = event.userPoolId
        const userAttributes = event.request.userAttributes;
        const username = event.userName;
        // test of the logic
    }
© www.soinside.com 2019 - 2024. All rights reserved.