aws_caller_identity 对于模块内的两个提供者
aws_caller_identity for two providers inside a module
我在尝试获取 aws 提供商的帐户 ID 时遇到问题,该提供商不是部署资源的提供商。这是我的场景:
main.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"
}
main.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 文件,在 source_account 中 asdfLambdaApiGatewayPermission 我得到的是 account1 id 而不是 account2 id我想要(并且需要)。调用此 lambda 的 api 网关在另一个帐户中,因此我需要有关此第二个提供商的所有信息(帐户 ID、区域等)
我遇到了与我的问题类似的 GitHub 问题 (https://github.com/hashicorp/terraform-provider-aws/issues/1078),但在我的情况下,问题是 在 模块中,如GitHub主题中的回答所述,我可能会遇到一些问题
你知道我怎样才能做到这一点吗??我知道我可以使用带有 accountID 的变量,但我想以动态方式获取帐户 ID(在我的例子中,我在 .aws/config 中使用配置文件),而不是强制用户编写每个 accountID在变量中。
跟随 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。
我在尝试获取 aws 提供商的帐户 ID 时遇到问题,该提供商不是部署资源的提供商。这是我的场景:
main.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"
}
main.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 文件,在 source_account 中 asdfLambdaApiGatewayPermission 我得到的是 account1 id 而不是 account2 id我想要(并且需要)。调用此 lambda 的 api 网关在另一个帐户中,因此我需要有关此第二个提供商的所有信息(帐户 ID、区域等)
我遇到了与我的问题类似的 GitHub 问题 (https://github.com/hashicorp/terraform-provider-aws/issues/1078),但在我的情况下,问题是 在 模块中,如GitHub主题中的回答所述,我可能会遇到一些问题
你知道我怎样才能做到这一点吗??我知道我可以使用带有 accountID 的变量,但我想以动态方式获取帐户 ID(在我的例子中,我在 .aws/config 中使用配置文件),而不是强制用户编写每个 accountID在变量中。
跟随 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。