Answers: <p>You may not create those two things in the right order, since there is no dependency declared between them. Terraform doesn't know that the security group with name &quot;my-sg&quot; is the same as the security group that it is creating with  that name.</p>
  <p>You could use a reference to the sg resource in your instance declaration:</p>
  <pre><code>resource &quot;aws_security_group&quot; &quot;my_sg&quot; {
    vpc_id = aws_vpc.mainvpc.id
    name = &quot;my_sg&quot;
    ingress {
      cidr_blocks = [
        &quot;0.0.0.0/0&quot;
      ]
      from_port = 22
      to_port = 22
      protocol = &quot;tcp&quot;
    }
  
  }
  
  resource &quot;aws_instance&quot; &quot;my_new_instance&quot; {
    ami = &quot;AMI-ID&quot;
    instance_type = &quot;t2.micro&quot;
    security_groups = [aws_security_group.my_sg.name]
    }
  }
  </code></pre>
  <p>or you can <a href="https://www.terraform.io/language/meta-arguments/depends_on" rel="nofollow noreferrer">declare a dependency</a>:</p>
  <pre><code>resource &quot;aws_security_group&quot; &quot;my_sg&quot; {
    vpc_id = aws_vpc.mainvpc.id
    name = &quot;my_sg&quot;
    ingress {
      cidr_blocks = [
        &quot;0.0.0.0/0&quot;
      ]
      from_port = 22
      to_port = 22
      protocol = &quot;tcp&quot;
    }
  
  }
  
  resource &quot;aws_instance&quot; &quot;my_new_instance&quot; {
    ami = &quot;AMI-ID&quot;
    instance_type = &quot;t2.micro&quot;
    security_groups = [&quot;my-sg&quot;]
    }
    depends_on = [aws_security_group.my_sg,]
  }
  </code></pre>
  <p>If you make the reference to another resource (example 1), then Terraform can itself determine the dependency, and wait for the creation of the security group before creating the instance.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-03-28T07:54:50.340Z
Answers: 2022-03-28T07:54:50.340Z
Answers: Bruce Becker
Body: <p>I am trying to create a security group (SG) using Terraform and then use it for an AWS instance.</p>
  <p>My config looks like</p>
  <pre><code>resource &quot;aws_security_group&quot; &quot;my_sq&quot; {
    vpc_id = aws_vpc.mainvpc.id
    name = &quot;my_sg&quot;
    ingress {
      cidr_blocks = [
        &quot;0.0.0.0/0&quot;
      ]
      from_port = 22
      to_port = 22
      protocol = &quot;tcp&quot;
    }
  
  }
  
  resource &quot;aws_instance&quot; &quot;my_new_instance&quot; {
    ami = &quot;AMI-ID&quot;
    instance_type = &quot;t2.micro&quot;
    security_groups = [&quot;my_sg&quot;]
    }
  }
  </code></pre>
  <p>I tried assigning the SG by name and id.
  When I ran <code>terraform plan</code> everything is all right.
  When I tried to apply settings <code>terraform apply</code>
  I see this error:</p>
  <pre><code>│ Error: Error launching instance, possible mismatch of Security Group IDs and Names.
  </code></pre>
  <p>How do I use the new SG which I created in the config file?</p>
  
Tags: <amazon-web-services><terraform><amazon-ec2><aws-security-group>
Title: Create AWS SG and use it
UserDisplayName: Eugene
ThreadText: # Create AWS SG and use it
  
  (Asked by: username_0 on 2022-03-28)
  
  I am trying to create a security group (SG) using Terraform and then use it for an AWS instance.
  
  My config looks like
  
  ```
  resource "aws_security_group" "my_sq" {
    vpc_id = aws_vpc.mainvpc.id
    name = "my_sg"
    ingress {
      cidr_blocks = [
        "0.0.0.0/0"
      ]
      from_port = 22
      to_port = 22
      protocol = "tcp"
    }
  
  }
  
  resource "aws_instance" "my_new_instance" {
    ami = "AMI-ID"
    instance_type = "t2.micro"
    security_groups = ["my_sg"]
    }
  }
  
  ```
  
  I tried assigning the SG by name and id.
  When I ran `terraform plan` everything is all right.
  When I tried to apply settings `terraform apply`
  I see this error:
  
  ```
  │ Error: Error launching instance, possible mismatch of Security Group IDs and Names.
  
  ```
  
  How do I use the new SG which I created in the config file?
  
  **Tags:** amazon-web-services, terraform, amazon-ec2, aws-security-group
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-03-28. Score: 3 | Accepted answer
  
  You may not create those two things in the right order, since there is no dependency declared between them. Terraform doesn't know that the security group with name "my-sg" is the same as the security group that it is creating with that name.
  
  You could use a reference to the sg resource in your instance declaration:
  
  ```
  resource "aws_security_group" "my_sg" {
    vpc_id = aws_vpc.mainvpc.id
    name = "my_sg"
    ingress {
      cidr_blocks = [
        "0.0.0.0/0"
      ]
      from_port = 22
      to_port = 22
      protocol = "tcp"
    }
  
  }
  
  resource "aws_instance" "my_new_instance" {
    ami = "AMI-ID"
    instance_type = "t2.micro"
    security_groups = [aws_security_group.my_sg.name]
    }
  }
  
  ```
  
  or you can [declare a dependency](https://www.terraform.io/language/meta-arguments/depends_on):
  
  ```
  resource "aws_security_group" "my_sg" {
    vpc_id = aws_vpc.mainvpc.id
    name = "my_sg"
    ingress {
      cidr_blocks = [
        "0.0.0.0/0"
      ]
      from_port = 22
      to_port = 22
      protocol = "tcp"
    }
  
  }
  
  resource "aws_instance" "my_new_instance" {
    ami = "AMI-ID"
    instance_type = "t2.micro"
    security_groups = ["my-sg"]
    }
    depends_on = [aws_security_group.my_sg,]
  }
  
  ```
  
  If you make the reference to another resource (example 1), then Terraform can itself determine the dependency, and wait for the creation of the security group before creating the instance.
  
  
  ---
  
  