Search code examples
terraformterraform-provider-awsaws-security-group

Terraform for aws security group create with name prefix


whenever i use terraform module for aws security group creation it creates a sgname-prefix eg., user-service-20200511140358261500000001. Because of this always terraform apply remove existing sg and create new sg eventhough there is no changes to it.

How to make it create SG with only name not using any timestamp ex: user-service-20200511140358261500000001 or how to make sg to be constant not changing anyway to freeze?

sample code

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

  name        = "user-service"
  description = "Security group for user-service with custom ports open within VPC, and PostgreSQL publicly open"
  vpc_id      = "vpc-xxx"

  ingress_cidr_blocks      = ["10.10.0.0/16"]
  ingress_rules            = ["https-443-tcp"]
  ingress_with_cidr_blocks = [
    {
      from_port   = 8080
      to_port     = 8090
      protocol    = "tcp"
      description = "User-service ports"
      cidr_blocks = "10.10.0.0/16"
    },
    {
      rule        = "postgresql-tcp"
      cidr_blocks = "0.0.0.0/0"
    },
  ]
}

module : https://github.com/terraform-aws-modules/terraform-aws-security-group


Solution

  • So with this module you can set the following which should do the trick:

    module "sg" {
      source = "terraform-aws-modules/security-group/aws"
      use_name_prefix = false
      name = "my-sg-name"
      ...
    }