Terraform模块问题:获取实例类型参数不被预期接受。

huangapple go评论98阅读模式
英文:

Terraform Module issue getting instance type argument is not expected

问题

我在应用以下tf文件时遇到以下错误。

> 错误:不支持的参数
instance_type = "t2.large"
这里不应该有名为"instance_type"的参数。

我按照以下方式调用模块

module "app-server" {
source = "../../modules/ec2"
instance_type = "t2.large"
}


variable "instance" {
type = string
default = "t2.micro"
}

variable "sg"{
type = list
default = [22,80,443,6801]
}

variable "ami" {
type = map(any)
default = {
"us-east-1" = "ami-026ebd4cfe2c043b2"
"us-west-2" = "ami-00aa0673b34e3c150"
"ap-sout-1" = "ami-008b85aa3ff5c1b02"
}
}

variable "region" {
type = string
default = "us-east-1"
}

locals {
common_tags = {
owner = "devops_team"
service = "backend"
}
}

resource "aws_instance" "app01" {
ami = lookup(var.ami,var.region)
instance_type = var.instance
availability_zone = "us-east-1a"
vpc_security_group_ids = [aws_security_group.web-sg.id]
key_name = "ec2-key"
tags = local.common_tags
connection {
type = "ssh"
user = "ec2-user"
private_key = file("./ec2-key.pem")
host = self.public_ip
}
provisioner "remote-exec" {
on_failure = continue
inline = [
"sudo yum install -y httpd",
"sudo systemctl restart httpd",
"sudo systemctl enable httpd"
]
}
}

resource "aws_security_group" "web-sg" {
name = "allow_tls"
description = "list of ingressports"
dynamic "ingress" {
for_each = var.sg
iterator = port
content {
from_port = port.value
to_port = port.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
egress {
description = "Outbound Allowed"
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}

resource "aws_ebs_volume" "web-ebs" {
availability_zone = "us-east-1a"
size = 40
tags = {
Name = "web-ebs-volume"
}
}

resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = aws_ebs_volume.web-ebs.id
instance_id = aws_instance.app01.id
}

英文:

I am getting below error while apply the below tf file.

> Error: Unsupported argument
instance_type = "t2.large"
An argument named "instance_type" is not expected here.

I am calling the module as mentioned below

  1. module "app-server" {
  2. source = "../../modules/ec2"
  3. instance_type = "t2.large"
  4. }

  1. variable "instance" {
  2. type = string
  3. default = "t2.micro"
  4. }
  5. variable "sg"{
  6. type = list
  7. default = [22,80,443,6801]
  8. }
  9. variable "ami" {
  10. type = map(any)
  11. default = {
  12. "us-east-1" = "ami-026ebd4cfe2c043b2"
  13. "us-west-2" = "ami-00aa0673b34e3c150"
  14. "ap-sout-1" = "ami-008b85aa3ff5c1b02"
  15. }
  16. }
  17. variable "region" {
  18. type = string
  19. default = "us-east-1"
  20. }
  21. locals {
  22. common_tags = {
  23. owner = "devops_team"
  24. service = "backend"
  25. }
  26. }
  27. resource "aws_instance" "app01" {
  28. ami = lookup(var.ami,var.region)
  29. instance_type = var.instance
  30. availability_zone = "us-east-1a"
  31. vpc_security_group_ids = [aws_security_group.web-sg.id]
  32. key_name = "ec2-key"
  33. tags = local.common_tags
  34. connection {
  35. type = "ssh"
  36. user = "ec2-user"
  37. private_key = file("./ec2-key.pem")
  38. host = self.public_ip
  39. }
  40. provisioner "remote-exec" {
  41. on_failure = continue
  42. inline = [
  43. "sudo yum install -y httpd",
  44. "sudo systemctl restart httpd",
  45. "sudo systemctl enable httpd"
  46. ]
  47. }
  48. }
  49. resource "aws_security_group" "web-sg" {
  50. name = "allow_tls"
  51. description = "list of ingressports"
  52. dynamic "ingress" {
  53. for_each = var.sg
  54. iterator = port
  55. content {
  56. from_port = port.value
  57. to_port = port.value
  58. protocol = "tcp"
  59. cidr_blocks = ["0.0.0.0/0"]
  60. }
  61. }
  62. egress {
  63. description = "Outbound Allowed"
  64. from_port = 0
  65. to_port = 65535
  66. protocol = "tcp"
  67. cidr_blocks = ["0.0.0.0/0"]
  68. }
  69. }
  70. resource "aws_ebs_volume" "web-ebs" {
  71. availability_zone = "us-east-1a"
  72. size = 40
  73. tags = {
  74. Name = "web-ebs-volume"
  75. }
  76. }
  77. resource "aws_volume_attachment" "ebs_att" {
  78. device_name = "/dev/sdh"
  79. volume_id = aws_ebs_volume.web-ebs.id
  80. instance_id = aws_instance.app01.id
  81. }

答案1

得分: 1

应该是 instance,而不是 instance_type

  1. module "app-server" {
  2. source = "../../modules/ec2"
  3. instance = "t2.large"
  4. }
英文:

It should be instance, not instance_type:

  1. module "app-server" {
  2. source = "../../modules/ec2"
  3. instance = "t2.large"
  4. }

huangapple
  • 本文由 发表于 2023年6月26日 15:45:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定