Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 您可以在terraform子目录之间共享父资源吗?_Amazon Web Services_Aws Api Gateway_Terraform - Fatal编程技术网

Amazon web services 您可以在terraform子目录之间共享父资源吗?

Amazon web services 您可以在terraform子目录之间共享父资源吗?,amazon-web-services,aws-api-gateway,terraform,Amazon Web Services,Aws Api Gateway,Terraform,我正在考虑使用这个terraform目录结构将应用程序分解为微服务: /staging /global /service1 /service2 /prod /global /service1 /service2 假设我希望每个微服务都有自己的AWS Apigateway端点: myurl.com/service1 myurl.com/service2 两个服务都可以是同一父网关资源的一部分: #creates the api (a parent of all reso

我正在考虑使用这个terraform目录结构将应用程序分解为微服务:

/staging
  /global
  /service1
  /service2
/prod
  /global
  /service1
  /service2
假设我希望每个微服务都有自己的AWS Apigateway端点:

myurl.com/service1
myurl.com/service2
两个服务都可以是同一父网关资源的一部分:

#creates the api (a parent of all resources), 
resource "aws_api_gateway_rest_api" "api" {
  name        = "API root parent"
  description = "Contains all endpoints"
}
我的理解是,每个顶级AWS REST api都是它自己的cloudfront,因此我只需要其中一个用于所有微服务,而不是每个微服务一个REST api。我想把restapi声明放在
/global
文件夹中,因为这将由所有微服务共享

然后在service1子目录中,我将开始定义需要引用父目录的资源:

resource "aws_api_gateway_resource" "service1" {
  rest_api_id = "${aws_api_gateway_rest_api.api.id}"
  parent_id   = "${aws_api_gateway_rest_api.api.root_resource_id}"
  path_part   = "service1"
}
但是,既然service1子目录不在同一目录中定义,它如何访问
api
资源呢

对于每个microservice子目录,我可能能够将现有资源的状态导入到子目录中,但这看起来很混乱


更新:看起来AWSAPI网关只收取使用费,所以我可以为每个子目录创建单独的RESTAPI资源。但我可能仍然需要知道如何跨子文件夹收集资源变量,以便将相应的api部署阶段添加到公共自定义域。是否有一种技术或模式能够将项目主要按文件夹分开,但能够提取变量并将它们粘在
/global
中,而不是特定于每个子目录?

terraform中的远程状态是您要找的

因为您在AWS中,所以可以使用S3作为远程状态的存储

data "terraform_remote_state" "prod_service1" {
  backend = "s3"
  config {
    region = "eu-west-1"
    bucket = "uservices_terraform_bucket_name"
    key    = "/states/prod/service1/terraform.tfstate"

    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    token      = "${var.aws_session_token}"
  }
}
然后,您将能够在另一个微服务中使用terraform状态的输出,使用

"${data.terraform_remote_state.prod_service1.api_id}"