Managing EC2 Security Groups using Puppet
This article originally appeared in a slightly different form on the AWS Advent blog, curated by the ever-awesome Brandon Burton. Check out this and many other great articles at http://awsadvent.tumblr.com/.
At Puppet Labs we recently shipped a module to make managing AWS easier. This tutorial shows how it can be used to manage your security groups. EC2 Security groups act as a virtual firewall and are used to isolate instances and other AWS resources from each other and the internet.
You can find the full details about installation and configuration for the module in the official README but the basic version, assuming a working Puppet and Ruby setup, is:
You’ll also want to have your AWS API credentials in environment variables (or use IAM if you’re running from within AWS).
First lets create a simple security group called test-sg in the us-east-1 region. Save the following to a file called securitygroup.pp:
Now lets run Puppet to create the group:
We’re running here with apply and the --test flag so we can easily see what’s happening, but if you have a Puppet master setup you can run with an agent too.
You will probably change your security groups over time as you’re infrastructure evolves. And managing that evolution is where Puppet’s declarative approach really shines. You can have confidence in the description of your infrastructure in code because Puppet can tell you about any changes when it runs.
Next lets add a new ingress rule to our existing group. Modify the securitygroup.pp file like so:
And again lets run Puppet to modify the group:
puppet apply securitygroup.pp --test You should see something like the following output:
The module also has full support for the Puppet resource command, so all of the functionality is available from the command line as well as the DSL. As an example lets clean-up and delete the group created above.
Hopefully that’s given you an idea of what’s possible with the Puppet AWS module. You can see more examples of the module in action in the main repository.
Some of the advantages of using Puppet for managing AWS resources are:
The familiar DSL - if you’re already using Puppet the syntax will already be familiar, if you’re not already using Puppet you’ll find lots of good references and documentation
Puppet is a declarative tool - Puppet is used to declare the desired state of the world, this means it’s useful for maintaining state and changing resources over time, as well as creating new groups
The current preview release of the module supports EC2 instances, security groups and ELB load balancers, with work on support for VPC, Route53 and Autoscaling Groups available soon. We’re looking for as much feedback as possible at the moment so feel free to report issues on GitHub), ask questions on the puppet-user mailing list or contact me on twitter at @garethr.