trying to run this simple terraform script
variable "instance_count" {
default = "1"
}
provider "aws" {
region = "us-west-1"
}
resource "aws_instance" "example" {
ami = "ami-0cf4a2d03d1a3d62c"
instance_type = "t2.micro"
key_name = "t2micro-1"
subnet_id = "subnet-0d9d37440a2265163"
security_groups = ["${aws_security_group.allow_rdp.Name}"]
}
resource "aws_security_group" "allow_rdp" {
name = "allow_rdp"
description = "Allow ssh traffic"
vpc_id = "vpc-013c7c9a647b7342b"
ingress {
from_port = 3389 # By default, the windows server listens on TCP port 3389 for RDP
to_port = 3389
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
but ghetting the error
Error: Unsupported attribute
│
│ on ec2.tf line 14, in resource "aws_instance" "example":
│ 14: security_groups = ["${aws_security_group.allow_rdp.Name}"]
│
│ This object has no argument, nested block, or exported attribute named "Name". Did you mean "name"?
╵
i changed it to
resource "aws_instance" "example" {
ami = "ami-0cf4a2d03d1a3d62c"
instance_type = "t2.micro"
key_name = "t2micro-1"
subnet_id = "subnet-0d9d37440a2265163"
security_groups = ["allow_rdp"]
but then got another error
Error: Error launching source instance: InvalidGroup.NotFound: The security group 'allow_rdp' does not exist in VPC 'vpc-013c7c9a647b7342b'
│ status code: 400, request id: e747fc43-28e1-4291-bee9-2d7bf01bd1ee
│
│ with aws_instance.example,
│ on ec2.tf line 9, in resource "aws_instance" "example":
│ 9: resource "aws_instance" "example" {
Hi @tdubb123,
I think the typo mentioned in the error message is the main problem here: you need to write name
with a lowercase n
, not with an uppercase N
:
security_groups = [aws_security_group.allow_rdp.name]
(I also removed the "${
and }"
syntax because that is only needed when you are combining multiple values together using a string template; there’s no need for that when you just want to use a single string value directly.
With that said, if I recall correctly security group names are only for EC2-Classic and so unless you have a very old AWS account you’ll probably need to use the vpc_security_group_ids
argument instead, like this:
vpc_security_group_ids = [aws_security_group.allow_rdp.id]
(Notice that for this argument we need to use the id
attribute of the security group, rather than the name
attribute. This is just a quirk of the differences between the legacy EC2-Classic and modern EC2; for modern EC2, security groups are always identified by a server-assigned ID rather than by the name you specified.)