Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • AWS EC2: Updating Security Groups using the AWS Command Line Tool

AWS EC2: Updating Security Groups using the AWS Command Line Tool

2

Working with multiple clients with their own AWS setups and security groups makes it difficult to work remotely sometimes. For instance, updating the many security groups with my current (dynamic) IP address so I can SSH into the server. To solve this problem, I created a shell script to manage the situation.

Using the aws ec2 command line tool, you can issue a set of commands to query, delete and add IP rules to the firewall. The script below allows you to:

  • Select a AWS profile to use (setup using the aws tool)
  • Specify one or more security groups to edit (under the same profile)
  • Any fixed IP addresses that should remain constant
  • The tcp port for the IP rules

The Script

Running the Script

Give the appropriate execute permissions to the script, e.g. chmod 755 aws-security.sh, then call the script using ./aws-security.sh

Caveats

Please note the following before running the script:

  • All existing rules matching the protocol (tcp) and port will be deleted by running this script. Please make sure any required IP addresses are added to fixed_ips.
  • The script will add your current IP address by default. If this already exists in fixed_ips, a error will be thrown by the aws tool.

AWS Security Policy

It’s a good idea to set up a AIM policy to manage access to your AWS account. With that in mind, you can create a new AIM user with the below access policy to restrict access to modifying the security group rules. This policy can be modified to restrict modifications to a specific security group by updating the “Resource”.

Amazon Web Services, EC2, Security

2 comments on “AWS EC2: Updating Security Groups using the AWS Command Line Tool”

  1. Alex says:
    November 20, 2019 at 4:41 AM

    It is possible to add description to new rule?
    and make search by description, instead of (# Port, port=22;)

    Thank You

    Reply
    • Niraj Shah says:
      November 30, 2019 at 9:38 AM

      The CLI tool does allow a description to be specified when adding the rule, using the more complex --ip-permissions flag. This also means you need to specify more details in the rule. You can see how this can be done in the documentation: https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html

      Reply

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy