1
votes

this is the error i'm getting:

Error: Reference to undeclared resource

on outputs.tf line 2, in output "api-gateway-codedeploy-app-name":
2: value = "${api-gateway-codedeploy.app-name}"

A managed resource "api-gateway-codedeploy" "app-name" has not been declared in the root module.

my terraform relevant folder structure is

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

the main outputs file doesn't recognize the codedeploy outputs even though the module inside api-gateway points the codedeploy-app folder as the source. same thing with users/client service, so I just attached the api module as the example

terraform/api-gateway.tf (api-gateway-codedeploy module)

    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

1 Answers

1
votes

You are going to have more similar errors. But to fix the one you mentioned fix the following:

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

to the correct one which references a module:

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