terraform aws_lb_listener_rule condition arguement not getting recognized in terraform 0.12.20

Viewed 1852

getting this error in aws_lb_listener_rule

Error: Unsupported argument

on ....\euc-terraformcontrol-alb\alb.tf line 127, in resource "aws_alb_listener_rule" "alb_listener_rule": 127: field = "${element(split(",", var.listener_rule_field), count.index)}"

An argument named "field" is not expected here.

Error: Unsupported argument

on ....\euc-terraformcontrol-alb\alb.tf line 128, in resource "aws_alb_listener_rule" "alb_listener_rule": 128: values = ["${element(split(",", var.listener_rule_value), count.index)}"]

resource "aws_alb_listener_rule" "alb_listener_rule" {
  count              = "${length(compact(split(",", var.priority))) > 0 ? "${length(compact(split(",", var.priority)))}" : 0}"
  listener_arn       = "${var.listener_arn}"
  priority           = "${element(split(",", var.priority), count.index)}"
  action              {
    type             = "forward"
    target_group_arn = "${element(split(",", var.target_group_arn), count.index)}"
  }
  condition           {
    //field            = "${element(split(",", var.listener_rule_field), count.index)}"
    //values           = "${element(split(",", var.listener_rule_value), count.index)}"
  }
}

2 Answers

This is breaking changes in the new version

In the older version, we were able to use below code for conditional routing

 condition {
   field = "host-header"
   values = ["api.example.com"]
 }

Switch your Terraform configuration to use the host_header or path_pattern configuration block instead.

So the above condition will become

   condition {
    host_header {
      values = ["api.example.com"]
    }
  }

If you are running you code in some CI/CD platform then better to add a version in configuration to avoid such issue in future and keep the code compatible.

provider "aws" {
  version = "~> 2.31.0"
}

The version meta-argument specifies a version constraint for a provider, and works the same way as the version argument in a required_providers block. The version constraint in a provider configuration is only used if required_providers does not include one for that provider.

/provider-veriosing

The field and values were deprecated in aws provider version 2.x as shown here. In the provider version 3.x they are removed and no longer valid as shown in the new docs for condition block.

This is further explained in the official "Terraform AWS Provider Version 3 Upgrade Guide" in:

So you either have to migrate your code to work with aws provider 3.x or go back to using older version.

Related