【问题标题】:aws_caller_identity for two providers inside a module模块内两个提供程序的 aws_caller_identity
【发布时间】:2021-12-30 03:28:16
【问题描述】:

我在尝试获取不是将部署资源的提供者的 aws 提供者的帐户 ID 时遇到问题。这是我的场景:


ma​​in.tf(根目录)

terraform {
  backend "s3" {
    [Omitted]
  }
}

module "ASDF" {
  source = "./modules/asdf"
  providers = {
    aws-account1 = aws.acc1
    aws-account2  = aws.acc2
  }
}

providers.tf(根目录)

provider "aws" {
  alias   = "acc1"
  profile = "profile-acc1"
  region  = "eu-west-1"
}

provider "aws" {
  alias   = "acc2"
  profile = "profile-acc2"
  region  = "eu-west-1"
}

ma​​in.tf(asdf 模块)

terraform {
  required_providers {
    aws-account1 = {
      source  = "hashicorp/aws"
      version = "~> 3.65.0"
    }
    aws-account2 = {
      source  = "hashicorp/aws"
      version = "~> 3.65.0"
    }
  }
}

data.tf(asdf 模块)

data "aws_caller_identity" "account1" {
  provider = aws-account1
}
data "aws_caller_identity" "account2" {
  provider = aws-account2
}

lambda.tf(asdf 模块)

resource "aws_lambda_function" "asdfLambda" {
  provider = aws-account1
  role = aws_iam_role.asdfLambdaExecutionRole.arn
  [Omitted]
}

resource "aws_iam_role" "asdfLambdaExecutionRole" {
  provider = aws-account1
  [Omitted]
}

resource "aws_lambda_permission" "asdfLambdaApiGatewayPermission" {
  provider = aws-account1

  action = "lambda:InvokeFunction"
  function_name = aws_lambda_function.asdfLambda.function_name
  principal = "apigateway.amazonaws.com"
  source_account = data.aws_caller_identity.account2.account_id
  source_arn = [APIGateway arn in account2]
}

有了这个 terraform 文件,在 asdfLambdaApiGatewayPermissionsource_account 中,我得到了我想要(和需要)的 account1 id 而不是 account2 id。调用此 lambda 的 api 网关在另一个帐户中,因此我需要有关此第二个提供者的所有信息(帐户 ID、区域等)

我遇到了与我的问题类似的这个 GitHub 问题 (https://github.com/hashicorp/terraform-provider-aws/issues/1078),但在我的情况下,问题是 inside 一个模块,正如 GitHub 主题中的答案所述我可能会遇到一些问题

你知道我怎样才能做到这一点吗?我知道我可以使用带有 accountID 的变量,但我想以动态方式获取帐户 ID(在我的情况下,我在 .aws/config 中使用配置文件),而不是强制用户在变量中写入每个 accountID .

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    在 Hashicorp documentation 之后,子 (asdf) 模块的 main.tf 文件应为:

    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 3.65.0"
          configuration_aliases = [ aws-account1, aws-account2]
        }
      }
    }
    

    否则 asdf 模块的 main.tf 正在使用默认的 aws 配置文件配置两个提供程序,我从您的错误中猜测是 account1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多