Unable to create security group in default VPC using Terraform

Hi,

I am not sure what I am missing, but I am getting the 'datacenter-sg' doesn't exist. error. Any help on this would be awesome.
Below is the terraform code.

data "aws_vpc" "default" {
  default = true
}

module "security_group" {
  source = "terraform-aws-modules/security-group/aws"

  name        = "datacenter-sg"
  description = "Security group for Nautilus App Servers"
  vpc_id      = data.aws_vpc.default.id

  ingress_rules = {
    http = {
      from_port   = 80
      ip_protocol = "tcp"
      cidr_ipv4   = "0.0.0.0/0"
      description = "HTTP from all"
    }
    ssh = {
      from_port   = 22
      ip_protocol = "tcp"
      cidr_ipv4   = "0.0.0.0/0"
      description = "SSH from all"
    }
  }

  tags = {
    Name = "datacenter-sg"
  }
}

Output of → aws ec2 describe-security-groups

{
    "SecurityGroups": [
        {
            "GroupId": "sg-025395ba7df7d3213",
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "UserIdGroupPairs": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": []
                }
            ],
            "Tags": [],
            "VpcId": "vpc-df89c6174675daae9",
            "SecurityGroupArn": "arn:aws:ec2:us-east-1:000000000000:security-group/sg-025395ba7df7d3213",
            "OwnerId": "000000000000",
            "GroupName": "default",
            "Description": "default VPC security group",
            "IpPermissions": []
        },
        {
            "GroupId": "sg-aacdcc3f098e40c2f",
            "IpPermissionsEgress": [],
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "datacenter-sg"
                }
            ],
            "VpcId": "vpc-df89c6174675daae9",
            "SecurityGroupArn": "arn:aws:ec2:us-east-1:000000000000:security-group/sg-aacdcc3f098e40c2f",
            "OwnerId": "000000000000",
            "GroupName": "datacenter-sg-9104789fd12fe3a9b925a47ad1",
            "Description": "Security group for Nautilus App Servers",
            "IpPermissions": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": 80,
                    "ToPort": 80,
                    "UserIdGroupPairs": [],
                    "IpRanges": [
                        {
                            "Description": "HTTP from all",
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": []
                },
                {
                    "IpProtocol": "tcp",
                    "FromPort": 22,
                    "ToPort": 22,
                    "UserIdGroupPairs": [],
                    "IpRanges": [
                        {
                            "Description": "SSH from all",
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": []
                }
            ]
        }
    ]
}

See my solution here which is for the more generic 100 days of cloud.
If the question you are attempting is in one of the other courses, add a default to the variable "infrastructure_prefix" with a value that matches the question, or the grader might fail if it tries to run the terraform.

1 Like

hi there can can refer to my solution for terraform help.

level 1 : GitHub - MiqdadProjects/kodekloud-terraform-solutions: Complete solutions and documentation for KodeKloud Engineer Terraform Level 1 tasks. Each task includes problem analysis, solution implementation, and detailed explanations with best practices. · GitHub

level 2 : GitHub - MiqdadProjects/kodekloud-terraform-level-2-solution: Complete solutions and documentation for KodeKloud Engineer Terraform Level 2 tasks. Each task includes problem analysis, solution implementation, and detailed explanations with best practices. · GitHub

level 3 : GitHub - MiqdadProjects/kodekloud-terraform-level-3-solution: Complete solutions and documentation for KodeKloud Engineer Terraform Level 3 tasks. Each task includes problem analysis, solution implementation, and detailed explanations with best practices. · GitHub

level 4 : GitHub - MiqdadProjects/kodekloud-terraform-level-4-solution: About Complete solutions and documentation for KodeKloud Engineer Terraform Level 4 tasks. Each task includes problem analysis, solution implementation, and detailed explanations with best practices. · GitHub

and if they help you make surer to give them a star.

1 Like

Hi, I’m facing the same issue. and I can also see it in the output of terraform state list. However, the task is still failing with the error saying that the security group was not created.

What’s the exact name of the lab you’re doing. That, or the “day of devops” day number will tell me which you’re doing. Also: have you tried Alistairs solution?

I got the lab to work (both in the " Day 95: Create Security Group Using Terraform"" version and in the KKE L1 version). There’s some overlap between Alistair’s version and the existing “provider.tf” file; if you remove the overlap from main.tf and add 'default = PREFIX` to the variable declaration, you can init and apply the code, and it will pass the grader.