How to deny all outbound traffic from an AWS EC2 Instance using a Security Group?

Chiranga Alwis

I am trying to set an AWS Security Group egress rule which blocks all outbound traffic. It has been known that by default, security groups allow all outbound traffic.

I am using AWS CloudFormation and how should we define the appropriate security egress rule?

John Rotenstein

Security Groups always define ALLOW traffic. There is no concept of a DENY for security groups.

Therefore, if you wish to deny all traffic, simply have an empty Security Group.

However, please note that Security Groups are stateful. This means that, if the Inbound security group permits a connection (eg a request coming into a web server), the response will be automatically permitted to exit the server. Therefore, it is only truly blocked if both the inbound and outbound security groups are empty (depending upon your configuration).

Other options for blocking the server are a host-based firewall rule (that is, a configuration within the operating system) or the use of Network Access Control Lists (NACLs) that operate at the Subnet level. NACLs have DENY rules that can block traffic in/out of a Subnet (but not to a specific instance).

Update

It turns out that, if no Egress rules are supplied, then the default "Allow All" rule is applied to the security group.

Therefore, you need to supply a rule that does nothing, so that the default rule doesn't apply.

For example:

"InstanceSecurityGroup": {
  "Type": "AWS::EC2::SecurityGroup",
  "Properties": {
    "VpcId": {
      "Ref": "VPC"
    },
    "SecurityGroupIngress": [
      {
        "IpProtocol": "tcp",
        "FromPort": "80",
        "ToPort": "80",
        "CidrIp": "0.0.0.0/0"
      }
    ],
    "SecurityGroupEgress": [
      {
        "IpProtocol": "tcp",
        "FromPort": "1",
        "ToPort": "1",
        "CidrIp": "0.0.0.0/32"
      }
    ]
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it possible to block countries IP using the security group on an EC2 instance?

BOTO3 -- Attach / Detach Security Group from EC2 instance

What does Outbound traffic mean for an AWS Security Group assigned to an AWS RDS instance?

Is there a way to add a security group to an EC2 instance using the CLI like we can with the management console?

Redirect all outbound SSH traffic through single IP from bunch of AWS EC2 instances

Can we remove a security group from an running EC2 instance?

How to ping from EC2 instance to on-premises Windows Server using AWS VPN?

How to copy files/directories remotely from linux to windows on AWS EC2 instance using Python?

How to set "Name" of security group (AWS EC2)

AWS DotNet SDK Error: Unable to get IAM security credentials from EC2 Instance Metadata Service

AWS EC2 > IGW Outbound Traffic Filtering by Domain or URL

AWS EC2 instance periodically drops security group

How to connect to AWS EC2 instance using filezillla?

Traffic between EC2 nodes in same security group

Multiple EC2 Instances outgoing/outbound traffic presented from a single/common IP (not using NAT!)

How to pull files from AWS ec2 instance?

AWS Security Group Outbound Query

Security group rule between aws external ALB and privale Ip ec2 instance?

How to increase Security Group Rules Limit for an EC2 (AWS)

How to depend on Terraform aws aws_autoscaling_group's ec2 instance state and status

Is is possible to detect event attaching/detaching security group from AWS EC2 instance?

How to extract ec2 instance details like name, memory from aws website using python selenium

AWS CLI script to change security group of EC2 to match currently assigned IP address when using VPN to obfuscate network traffic?

AWS Security Group rules: How does ssh connection to EC2 still works when I have removed outbound rules

Creating an ec2 instance using terraform by attaching a security group that already exists

How to connect to AWS ec2 Instance from Chromebook Pixel

failed to create ec2 instance using terraform if set security group

Add Security Group to EC2 instance using AWS CDK Python

Allow ECS Fargate task access to EC2 instance using Security Group ID instead of IP