在主输出中找不到来自模块源的Terraform输出

时间:2020-04-09 19:11:40

标签: terraform

这是我得到的错误:

错误:对未声明资源的引用

在output.tf第2行上的输出“ api-gateway-codedeploy-app-name”中:
2:值=“ $ {api-gateway-codedeploy.app-name}”

尚未管理托管资源“ api-gateway-codedeploy”“ app-name” 在根模块中声明。

我与地形相关的文件夹结构是

terraform
    |_ api-gatewy.tf
    |_ listing-service.tf
    |_ users-service.tf
    |_ outputs.tf
    |_ codedeploy
          |_ iam-instance-profiles.tf
          |_ main.tf
          |_ outputs.tf
          |_ s3.tf
          |_ variables.tf

主输出文件无法识别代码部署输出 即使api-gateway中的模块将codedeploy-app文件夹指定为源。 与用户/客户端服务相同,因此我仅以api模块为例

terraform / api-gateway.tf(api-gateway-codedeploy模块)

    module "api-gateway-codedeploy" {
  source = "./codedeploy-app"

  app-name          = "api-gateway"
  ec2-instance-name = module.api-gateway.name

}

terraform / outputs.tf

output "api-gateway-codedeploy-app-name" {
  value = api-gateway-codedeploy.app-name
}

output "api-gateway-deployment-bucket-name" {
  value = api-gateway-codedeploy.deployment-bucket-name
}

output "api-gateway-private-ip" {
  value = module.api-gateway.private-ip
}

output "api-gateway-public-ip" {
  value = aws_eip.api-gateway-eip.public_ip
}


output "aws-region" {
  value = var.aws-region
}


output "listing-service-codedeploy-app-name" {
  value = listing-service-codedeploy.app-name
}

output "listing-service-deployment-bucket-name" {
  value = listing-service-codedeploy.deployment-bucket-name
}

output "listing-service-private-ip" {
  value = module.api-gateway.private-ip
}

output "listing-service-db-address" {
  value = module.listing-service-db.address
}


output "users-service-codedeploy-app-name" {
  value = users-service-codedeploy.app-name
}

output "users-service-deployment-bucket-name" {
  value = users-service-codedeploy.deployment-bucket-name
}

output "users-service-private-ip" {
  value = module.api-gateway.private-ip
}

output "users-service-db-address" {
  value = module.users-service-db.address
}

terraform / codedeploy / main.tf

resource "aws_iam_role" "codedeploy-role" {
  name = "${var.app-name}-codedeploy-role"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "codedeploy.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}

resource "aws_iam_role_policy_attachment" "aws-codedeploy-role" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole"
  role       = aws_iam_role.codedeploy-role.name
}


resource "aws_codedeploy_app" "default" {
  compute_platform = "Server"
  name             = var.app-name
}

resource "aws_codedeploy_deployment_group" "prod" {
  app_name              = aws_codedeploy_app.default.name
  deployment_group_name = "prod"
  service_role_arn      = aws_iam_role.codedeploy-role.arn

  ec2_tag_set {
    ec2_tag_filter {
      key   = "Name"
      type  = "KEY_AND_VALUE"
      value = var.ec2-instance-name
    }
  }
}

terraform / codedeploy / s3.tf

resource "aws_s3_bucket" "deploy-bucket" {
  bucket = "katoms-microservices-demo-${var.app-name}-deployment"

}

terraform / codedeploy / variables.tf

  variable "app-name" {
  type = string
}

variable "ec2-instance-name" {
  type = string
}

terraform / codedeploy / iam-instance-profiles.tf

resource "aws_iam_instance_profile" "ec2" {
  name = "${var.app-name}-ec2"
  role = aws_iam_role.ec2.name

terraform / codedeploy / outputs.tf

 output "app-name" {
  value = aws_codedeploy_app.default.name
}

output "deployment-bucket-name" {
  value = aws_s3_bucket.deploy-bucket.id
}

output "iam-instance-profile" {
  value = aws_iam_instance_profile.ec2.name
}

1 个答案:

答案 0 :(得分:1)

您将遇到更多类似的错误。但是要解决您提到的问题,请解决以下问题:

output "api-gateway-codedeploy-app-name" {
  value = api-gateway-codedeploy.app-name
}

正确引用模块:

output "api-gateway-codedeploy-app-name" {
   value = module.api-gateway-codedeploy.app-name
}