Auto update for EC2 security groups

Access to services on an Amazon Web Services EC2 instance is controlled by security groups. These allow you to have, say, port 80 available to anyone but have other ports only accessible from specific IP address(es). If the IP address supplied by your ISP is dynamic or, at least, not not static you can find yourself shutout of your own services until you’ve updated the EC2 security groups.

Updating groups is easy enough but with more than one it’s just a pain to have to do. And what have you missed while those services were inaccessible?

The utility documented (and source code provided) in this post solves the problem by checking your IP address periodically to update any security groups with a new IP address if a change is detected. It will also send you an email when changes are made or if internet connectivity fails.


Download the source code

Configuring the utility

The utility is written using C#. Like all .NET programs, configuration information is supplied in a configuration file. Here’s an empty configuration file:

<?xml version="1.0"?>
		&lt;add key="AWSAccessKey" value="<your key>"/>
		&lt;add key="AWSSecretKey" value="<your key>"/>
		&lt;!-- Enter the previous IP address here -->
		&lt;add key="PreviousAddress" value="" />
		&lt;!-- Enter the address of the mail server.
			  If not defined or empty no emails will be sent. -->
		&lt;add key="MailServer" value="<your email server>"/>
		&lt;!-- Enter the address of the recipient(s) who are to receive notification emails.
			 If not defined or empty no emails will be sent. -->
		&lt;add key="Recipient" value="<your email address>"/>
		&lt;!-- The url to find out the current IP address -->
		&lt;add key="IPAddressCheck" value="<IP address check address>" />
	&lt;!-- Only relevant if .NET 4.0 is installed! -->
		&lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

Most of these are obvious. PreviousAddress can be left blank. It will be used by the utility when it first checks your IP address. The reason for IPAddressCheck is covered in Checking the IP address below.

Checking the IP address

The utility discovers your external IP address (the one EC2 uses) by polling a nominated external web page you create. The page returns your current reported IP address (and only this) in the body of the response to a web request. Below is an example of a PHP script which will do the job:

&lt;?php $_SERVER["REMOTE_HOST"]; ?>

Installing as a service

Obviously you want this utility to run all the time and so that you can, it is implemented as a Windows service. To install the utility as a service you will need to use the .NET InstallUtil. Normally this utility is available here:

c:\Windows\Microsoft.NET\Framework\v4.0.30319 or
c:\Windows\Microsoft.NET\Framework\v4.0.30319 or

Depending upon the version of the framework you use.

To start the utility use the command:


Or find the service with the name Update AWS Security Groups in the services applet.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.

Other Posts

Reader Comments

Sorry, comments are closed.