Examples

This section provides two examples of how to use the Amazon EC2 firewall.

[Note]Note

These examples use the Command Line Tools Reference. You can also achieve these results using the SOAP API. For more information, see Using the SOAP API.

This example shows Albert modifying the default group to meet his security needs.

Albert Modifies the Default Group

1

Albert launches a copy of his favorite public AMI.

$ ec2-run-instances ami-eca54085
RESERVATION r-a034c7c9 924417782495 default
INSTANCE i-cfd732a6 ami-eca54085 pending 0 m1.small 2007-07-11T16:40:44+0000  us-east-1c

2

After a little wait for image launch to complete. Albert, who is a cautious type, checks the access rules of the default group.

$ ec2-describe-group default
GROUP   598916040194    default default group
PERMISSION  default  ALLOWS  all   FROM   USER   598916040194   GRPNAME default

Albert notices that it only accepts ingress network connections from other members of the default group for all protocols and ports.

3

Albert, being paranoid as well as cautious, port scans his instance.

$ nmap -P0 -p1-100 ec2-67-202-51-105.compute-1.amazonaws.com 
Starting nmap 3.81 ( http://www.insecure.org/nmap/ ) at 2006-08-07 15:42 SAST
All 100 scanned ports on ec2-67-202-51-105.compute-1.amazonaws.com  (67.202.51.105) are: filtered

Nmap finished: 1 IP address (1 host up) scanned in 31.008 seconds

4

Albert decides he should be able to SSH into his instance, but only from his own machine.

$ ec2-authorize default -P tcp -p 22 -s 192.168.1.130/32
GROUP   default
PERMISSION   default ALLOWS  tcp  22  22  FROM  CIDR  192.168.1.130/32

5

Albert repeats the port scan.

$ nmap -P0 -p1-100 ec2-67-202-51-105.compute-1.amazonaws.com 
Starting nmap 3.81 ( http://www.insecure.org/nmap/ ) at 2006-08-07 15:43 SAST
Interesting ports on ec2-67-202-51-105.compute-1.amazonaws.com  (67.202.51.105):
(The 99 ports scanned but not shown are in state: filtered)
PORT   STATE SERVICE
22/tcp open  ssh

Nmap finished: 1 IP address (1 host up) scanned in 32.705 seconds

Albert is happy (or at least less paranoid).


Mary wants to deploy her public, failure resilient, three-tier web service in Amazon EC2. Her grand plan is to have her web tier start off executing in seven instances of ami-fba54092, her application tier executing in twenty instances of ami-e3a5408a, and her multi-master database in two instances of ami-f1a54098. She's concerned about the security of her subscriber database, so she wants to restrict network access to her middle and back tier machines. When the traffic to her site increases over the holiday shopping period, she adds additional instances to her web and application tiers to handle the extra load.

Launch Process

1

First, Mary creates a group for her Apache web server instances and allows HTTP access to the world.

$ ec2-add-group apache -d "Mary's Apache group"
GROUP   apache  Mary's Apache group

$ ec2-describe-group apache
GROUP   598916040194    apache  Mary's Apache group

$ ec2-authorize apache -P tcp -p 80 -s 0.0.0.0/0
GROUP   apache
PERMISSION   apache  ALLOWS  tcp   80    80    FROM    CIDR    0.0.0.0/0

$ ec2-describe-group apache
GROUP   598916040194    apache  Mary's Apache group
PERMISSION   598916040194   apache  ALLOWS  tcp   80   80   FROM   CIDR   0.0.0.0/0

2

Mary launches seven instances of her web server AMI as members of the apache group.

$ ec2run ami-fba54092 -n 7 -g apache
RESERVATION r-0592776c 598916040194 default
INSTANCE i-cfd732a6 ami-fba54092 pending 0 m1.small 2007-07-11T16:40:44+0000  us-east-1c
INSTANCE i-cfd732a7 ami-fba54092 pending 0 m1.small 2007-07-11T16:40:44+0000  us-east-1c
INSTANCE i-cfd732a8 ami-fba54092 pending 0 m1.small 2007-07-11T16:40:44+0000  us-east-1c
INSTANCE i-cfd732a9 ami-fba54092 pending 0 m1.small 2007-07-11T16:40:44+0000  us-east-1c
INSTANCE i-cfd732aa ami-fba54092 pending 0 m1.small 2007-07-11T16:40:44+0000  us-east-1c
INSTANCE i-cfd732ab ami-fba54092 pending 0 m1.small 2007-07-11T16:40:44+0000  us-east-1c
INSTANCE i-cfd732ac ami-fba54092 pending 0 m1.small 2007-07-11T16:40:44+0000  us-east-1c


$ ec2din i-cfd732a6
RESERVATION     r-0592776c      598916040194
INSTANCE        i-cfd732a6      ami-fba54092       ec2-67-202-51-245.compute-1.amazonaws.com       running 0
m1.small 2007-07-11T16:40:44+0000

3

Being as paranoid as Albert, Mary does a port scan to confirm the permissions she just configured.

$ nmap -P0 -p1-100 ec2-67-202-51-245.compute-1.amazonaws.com 
Starting nmap 3.81 ( http://www.insecure.org/nmap/ ) at 2006-08-07 16:21 SAST
Interesting ports on ec2-67-202-51-245.compute-1.amazonaws.com  (67.202.51.245):
(The 99 ports scanned but not shown are in state: filtered)
PORT   STATE SERVICE
80/tcp open  http

Nmap finished: 1 IP address (1 host up) scanned in 33.409 seconds

4

Mary verifies her web server can be reached.

$ telnet ec2-67-202-51-245.compute-1.amazonaws.com  80
Trying 67.202.51.245...
Connected to ec2-67-202-51-245.compute-1.amazonaws.com  (67.202.51.245).
Escape character is '^]'.

Mary can reach her web server.

5

Mary creates a separate group for her application server.

$ ec2-add-group appserver -d "Mary's app server"
GROUP   appserver       Mary's app server

6

Mary starts twenty instances as members of appserver group.

$ ec2run ami-e3a5408a -n 20 -g appserver

7

Mary grants network access between her web server group and the application server group.

$ ec2-authorize appserver -o apache -u 495219933132
GROUP   appserver
PERMISSION   appserver  ALLOWS  all   FROM   USER   495219933132   GRPNAME apache

8

Mary verifies access to her app server is restricted by port scanning one of the application servers.

$ nmap -P0 -p1-100 ec2-67-202-51-162.compute-1.amazonaws.com 
Starting nmap 3.81 ( http://www.insecure.org/nmap/ ) at 2006-08-07 15:42 SAST
All 100 scanned ports on ec2-67-202-51-162.compute-1.amazonaws.com  (67.202.51.162) are: filtered

Nmap finished: 1 IP address (1 host up) scanned in 31.008 seconds

9

Mary confirms that her web servers have access to her application servers.

  1. She (temporarily) grants SSH access from her workstation to the web server group:

    $ ec2-authorize apache -P tcp -p 22 -s 192.168.1.130/32
  2. She logs in to one of her web servers and connects to an application server on TCP port 8080.

    $ telnet ec2-67-202-51-162.compute-1.amazonaws.com  8080
    	Trying 67.202.51.162...
    	Connected to ec2-67-202-51-162.compute-1.amazonaws.com (67.202.51.162).
    	Escape character is '^]'
  3. Satisfied with the setup, she revokes SSH access to the web server group.

    $ ec2-revoke apache -P tcp -p 22 -s 192.168.1.130/32

10

Mary repeats these steps to create the database server group and to grant access between the application server and database server groups.


[Note]Note

Defining firewall rules in terms of groups is flexible enough to allow you to implement functionality equivalent to a VLAN.

In addition to the distributed firewall, you can maintain your own firewall on any of your instances. This can be useful if you have specific requirements not met by the Amazon EC2 distributed firewall.