简体   繁体   中英

Terraform aws_elastic_beanstalk_environment “Error: Missing required argument”

Background

I have a Terraform script that creates several different AWS resources and links them together. One component is the aws_elastic_beanstalk_environment. It has the required parameters, and lots of settings for configuration. The beginning of the file is thus:

data "aws_elastic_beanstalk_application" "myapp" {
  name = "beanstalkapp"
}

resource "aws_elastic_beanstalk_environment" "beanstalk" {
  name                = "beanstalk-environment"
  application         = data.aws_elastic_beanstalk_application.myapp.name
  solution_stack_name = "64bit Amazon Linux 2 v5.2.5 running Node.js 12"

  setting {
    namespace = "aws:ec2:instances"
    name = "InstanceTypes"
    value = "t2.micro"
  }
  setting {
    namespace = "aws:elasticbeanstalk:environment"
    name = "EnvironmentType"
    value = "LoadBalanced"
  }
  setting {
    namespace = "aws:elasticbeanstalk:environment"
    name = "LoadBalancerType"
    value = "application"
  }
  setting {
    namespace = "aws:elasticbeanstalk:environment"
    name = "ServiceRole"
    value = "aws-elasticbeanstalk-service-role"
  }
  setting {
    namespace = "aws:autoscaling:launchconfiguration"
    name = "IamInstanceProfile"
    value = "aws-elasticbeanstalk-ec2-role"
  }
  setting {
    namespace = "aws:ec2:vpc"
    name      = "VPCId"
    value     = var.createNewVPC ? aws_vpc.vpc_new[0].id : var.vpc_id_existing
  }
  setting {
    namespace = "aws:ec2:vpc"
    name      = "Subnets"
    value     = var.createNewSubnets ? "${aws_subnet.subnet_private_a_new[0].id},${aws_subnet.subnet_private_b_new[0].id}" : "${var.subnet_private_a_id},${var.subnet_private_b_id}"
  }
  setting {
    namespace = "aws:ec2:vpc"
    name      = "ELBSubnets"
    value     = var.createNewSubnets ? "${aws_subnet.subnet_public_a_new[0].id},${aws_subnet.subnet_public_b_new[0].id}" : "${var.subnet_public_a_id},${var.subnet_public_b_id}"
  }
  setting {
    namespace = "aws:autoscaling:asg"
    name = "MinSize"
    value = "2"
  }
  setting {
    namespace = "aws:autoscaling:asg"
    name = "MaxSize"
    value = "2"
  }
  setting {
     namespace = "aws:elasticbeanstalk:application"
     name      = "Application Healthcheck URL"
     value     = "/"
  }
  setting {
     namespace = "aws:elasticbeanstalk:application:environment"
     name      = "DB_HOST"
     value     = data.aws_db_instance.myDB.address
  }
  setting {
     namespace = "aws:elasticbeanstalk:application:environment"
     name      = "DB_USER"
     value     = random_password.rds_username.result
  }
  setting {
     namespace = "aws:elasticbeanstalk:application:environment"
     name      = "DB_PASS"
     value     = random_password.rds_password.result
  }
  setting {
     namespace = "aws:elasticbeanstalk:application:environment"
     name      = "DB_PORT"
     value     = data.aws_db_instance.myDB.port
  }
  setting {
     namespace = "aws:elasticbeanstalk:application:environment"
     name      = "CACHE_ADDRESS"
     value     = data.aws_elasticache_cluster.myCache.cluster_address
  }
  setting {
     namespace = "aws:elasticbeanstalk:application:environment"
     name      = "CACHE_PORT"
     value     = var.cache_port
  }
}

Problem

When running the script with -target=aws_elastic_beanstalk_environment.beanstalk, the beanstalk deploys just fine. When running the script to deploy the full stack, the other components are created and then I get

Error: Missing required argument

  on beanstalk.tf line 6, in resource "aws_elastic_beanstalk_environment" "beanstalk":
  6: resource "aws_elastic_beanstalk_environment" "beanstalk" {

The argument "setting.1.value" is required, but no definition was found.

I'm probably as adapt at deciphering cryptic error messages as the next guy, but this seems like something in the guts of Terraform that is choking. I was on 0.13.5 and had the error, so I upgraded to 0.14.6. The only difference is now it displays the line about "setting.1.value".

Any ideas on what this means or how to solve it?

This is the setting block that causes the problem:

  setting {
     namespace = "aws:elasticbeanstalk:application:environment"
     name      = "CACHE_ADDRESS"
     value     = data.aws_elasticache_cluster.myCache.cluster_address
  }

If I replace the value with a static value, everything works. I believe this to be an issue with Terraform itself returning the proper value of the cluster_address. The issue arises because this ElastiCache is a Redis instance, but the cluster_address property is only for Memcached. It seems that Terraform should have a better error messge for this.

So if you see a weird "setting.x.value" error, it probably means that you are trying to use something that only applies to some of the options available for the resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM