---
title: "Discussion on \"AWS Security Groups are tricky!\" | Hashnode"
description: "Discussion on \"AWS Security Groups are tricky!\". Recently I was configuring a security group for a service that uses TCP and UDP ports for ingress connectivity.\nSecurity Groups are an Amazon Web Services feature for Amazon VPC (virtual private cloud) to set up a stateful firewall.\nI used Terraform ..."
url: "https://hashnode.com/posts/aws-security-groups-are-tricky/659401cb71deca9b06d6128d"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [AWS Security Groups are tricky!](https://tymik.me/aws-security-groups-are-tricky?utm_source=hashnode&utm_medium=feed)

Recently I was configuring a security group for a service that uses TCP and UDP ports for ingress connectivity. Security Groups are an Amazon Web Services feature for Amazon VPC (virtual private cloud) to set up a stateful firewall. I used Terraform ...

Jan 2, 2024 · 2 min read · tymik.me  
Read the article

-   [#aws](https://hashnode.com/tag/aws)
-   [#vpc](https://hashnode.com/tag/vpc)
-   [#securitygroups](https://hashnode.com/tag/securitygroups)
-   [#security](https://hashnode.com/tag/security)
-   [#terraform](https://hashnode.com/tag/terraform)

## Written by

[Jan Tymiński](https://hashnode.com/@tymik)

@tymik · Poland

  
[View profile](https://hashnode.com/@tymik)

♾️ DevOps ☁️ AWS 🐳 Docker, Terraform, Automation

I started my professional career in 2012 as Systems Administrator and continued it until 2018 to become DevOps Engineer. I work with AWS since 2016 and I am 5 times certified AWS Specialist.

## Responses 1

Markdown supported · ⌘↵ to post

[Tomek Habiger](https://hashnode.com/@thabiger)

[Feb 17, 2024](https://hashnode.com/posts/aws-security-groups-are-tricky/659401cb71deca9b06d6128d/comment/65d0803ad51c276bbe61d2b8)

The main principle of a security group is that the most permissive rule wins.

A port is a concept specific to TCP and UDP protocols that operate at the transport layer (Layer 4). If you allow any IP protocol (-1) in your rule, you're essentially saying, 'I don't care about the protocol; just work at the IP network layer (Layer 3).' Such a rule not only disregards ports but also overrides any other protocol-oriented rules you have specified in your set (as it operates at a lower layer).

If used consciously, there is nothing wrong with this so far.

So let's check if the AWS tooling lets you do anything risky with that setting:

-   UI doesn't even allow you to provide ports in such conditions(protocol set to -1),
    
-   Terraform won't let you do this as well; with such a code:
    

resource "aws\_security\_group" "example" { name = "MySecurityGroup" description = "My security group" vpc\_id = "_\*_"

ingress { from\_port = 666 to\_port = 666 protocol = "-1" cidr\_blocks = \["0.0.0.0/0"\] } }

it throws an error: updating Security Group (_\*_) ingress rules: updating rules: from\_port (666) and to\_port (666) must both be 0 to use the 'ALL' "-1" protocol!

-   only the CLI allows that, but it must be admitted that in its output, it tells you exactly what it has done:

aws ec2 authorize-security-group-ingress \\ --group-id $security\_group\_id \\ --protocol -1 \\ --port 666 \\ --cidr 0.0.0.0/0

{ "Return": true, "SecurityGroupRules": \[ { "SecurityGroupRuleId": "**_", "GroupId": "_**", "GroupOwnerId": "_\*_", "IsEgress": false, "IpProtocol": "-1", "FromPort": -1, "ToPort": -1, "CidrIpv4": "0.0.0.0/0" } \] }

Consent on one matter: awareness! :)

[Jan Tymiński](https://hashnode.com/@tymik)

[Feb 17, 2024](https://hashnode.com/posts/aws-security-groups-are-tricky/659401cb71deca9b06d6128d/comment/65d0803ad51c276bbe61d2b8#reply-65d0a16d28d5736666e771cb)

Thank you for the clarification Tomek!

I was not really aware of the networking specifics that cause the behaviour of using all protocols, I'm not that fluent with networks - and I believe more people might also not understand the underlying reason so your comment brings a great value here!

I had successfully set `-1` as protocol with Terraform but perhaps I used older provider when I did so - unfortunately I'm not able to verify that anymore as I don't continue that project.
