Terraform创建aws_eip资源时遭遇不可配置属性错误求助
Terraform创建aws_eip时"domain"属性报错的解决方法
你使用AWS Provider 4.16版本创建aws_eip资源时,两段配置都触发了同一个错误:
Error: Value for unconfigurable attribute
with aws_eip.lb,
on main.tf line 25, in resource "aws_eip" "lb":
25: domain = "vpc"Can't configure a value for "domain": its value will be decided
automatically based on the result of applying this configuration.
问题原因
AWS Provider 4.x版本之后,aws_eip的domain属性被改为计算属性,不再允许手动指定值。Terraform会根据EIP的使用场景自动推断:如果关联VPC内的实例或网络接口,自动设为vpc;如果是经典网络环境则设为standard。
修正后的配置代码
第一段配置修正版
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.16" } } } provider "aws" { region = "eu-west-2" } resource "aws_eip" "lb" { # 移除手动设置的domain属性,由Terraform自动推断 }
第二段配置修正版
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.16" } } } provider "aws" { region = "eu-west-2" } resource "aws_vpc" "vpc_a" { cidr_block = "10.0.0.0/16" } resource "aws_instance" "server_a" { ami = "ami-06464c878dbe46da4" instance_type = "t2.micro" } resource "aws_eip" "lb" { instance = aws_instance.server_a.id # 移除手动设置的domain属性 }
修正后执行terraform plan即可正常运行。你的教程大概率是基于旧版本AWS Provider编写的,当时domain还允许手动配置,新版本做了这个兼容性变更。
内容的提问来源于stack exchange,提问作者Acire
相关产品推荐
相关产品推荐

