AWS: Security Groups through CLI


Prerequisite:
Knowledge of AWS EC2 and it's security groups.
How to install and configure AWS CLI.

Here are some important points to remember about AWS security groups.
  • AWS security groups are instance level security measure. 
  • A security group can have several instances in it. It acts as a virtual firewall for them. 
  • They let you define rules for allowing inbound and outbound traffic. Please not that you can only allow but can not explicitly deny traffic from an particular host or range of IPs (that can be done with help of NACLs).
  • Rules can be added and removed at any time and will be effective immediately (or in a very short interval). 
  • Security groups are state-full, which means any request that goes outside it's response is allowed inside, no matter what the outbound rules are.
  • All the rules are processed before allowing any traffic. 
  • In a custom security group all outbound traffic is allowed and all inbound traffic is blocked.
Below steps explains you to create a security group through amazon CLI. 

1) Create a new security group
Once you are inside window's powershell and AWS CLI is configured, use below command

aws ec2 create-security-group --group-name CLIGroup --description "Testing"

Group name and description are mandatory attributes. If you need it in particular VPC --vpc-id can be used. Group name has to be unique or else it will give error. Output will provide the id of newly created group.

2) Add inbound rule

To add inbound rules use

aws ec2 authorize-security-group-ingress --group-id sg-0e905383785119273 --protocol tcp --port 22 --cidr 0.0.0.0/0


aws ec2 authorize-security-group-ingress --group-id sg-0e905383785119273 --protocol tcp --port 443 --cidr 0.0.0.0/16

As the commands states these will authorize traffic through tcp port 22 (ssh) from cidr range 0.0.0.0/0 and through port 443 (https) from 0.0.0.0/16

* ingress means incoming

3) Add outbound rule

To add outbound rules use

aws ec2 authorize-security-group-egress --group-id sg-0e905383785119273 --protocol tcp --port 22 --cidr 0.0.0.0/0

By default all outbound traffic is allowed, but we can delete that rule and allow only specific port open to interact with.

4) Delete a rule

Simply use revoke instead of authorize. Remember to mention exact same details.

aws ec2 revoke-security-group-ingress --group-id sg-0e905383785119273 --protocol tcp --port 22 --cidr 0.0.0.0/0

aws ec2 revoke-security-group-egress --group-id sg-0e905383785119273 --protocol tcp --port 22 --cidr 0.0.0.0/0

Here is the complete result. I did not run the revoke commands. 

5) Delete the security group

Finally to delete security group use

aws ec2 delete-security-group --group-id sg-0e905383785119273

It won't give any out but will delete the complete security group.

I hope this help. Please try and do let me know your experience and issues on the way. Happy learning.


Comments

Popular posts from this blog

Journey With A Cute Guy...!!!

Khajuraho: A small town with international fame.

An Untold Story