Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)

I'm using AWS resource using terraform and here is the Security Group:

ingress {

          from_port = 0

          to_port = 0

          protocol = -1

          self = true

      }

But now how to add different Protocols, what is the terraform syntax to do that?

1 Answer

0 votes
by (12.4k points)
edited by

You may implement a self-referential group by splitting the 'sec group' from rules using the resources 'aws_security_group' and 'aws_security_group_rule'

And your terraform code will look like this:

resource "aws_security_group" "sec_group" {

  name   = "sec_group"

  vpc_id = "${local.vpc_id}"

}

resource "aws_security_group_rule" "sec_group_allow_tcp" {

  type              = "ingress"

  from_port         = 0 // first part of port range 

  to_port           = 65535 // second part of port range

  protocol          = "tcp" // Protocol, could be "tcp" "udp" etc. 

  security_group_id = "${aws_security_group.sec_group.id}" // Which group to attach it to

  source_security_group_id = "${aws_security_group.sec_group.id}" // Which group to specify as source

}

resource "aws_security_group_rule" "sec_group_allow_udp" {

  type              = "ingress"

  from_port         = 0 // first part of port range 

  to_port           = 65535 // second part of port range

  protocol          = "udp" // Protocol, could be "tcp" "udp" etc. 

  security_group_id = "${aws_security_group.sec_group.id}" // Which group to attach it to

  source_security_group_id = "${aws_security_group.sec_group.id}" // Which group to specify as source

}

resource "aws_security_group_rule" "sec_group_allow_1865" {

  type              = "ingress"

  from_port         = 1865 // first part of port range 

  to_port           = 1865 // second part of port range

  protocol          = "tcp" // Protocol, could be "tcp" "udp" etc. 

  security_group_id = "${aws_security_group.sec_group.id}" // Which group to attach it to

  source_security_group_id = "${aws_security_group.sec_group.id}" // Which group to specify as source

}

Do you want to master AWS, then do check out the AWS Course by Intellipaat.

For more details on Terraform, you can check out the video tutorial below.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
asked Jan 5, 2021 in AWS by Justin (7k points)
0 votes
1 answer
0 votes
2 answers

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...