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

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

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

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
        }

答案1

得分: 1

应该是 instance,而不是 instance_type

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

It should be instance, not instance_type:

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

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:

确定