If, like me, you have to manage several Security Groups on your AWS account, you can make use of the describe-security-groups
AWS CLI command for download them as a .json file for backup.
TL;DR
You can jump to the official AWS documentation for the describe-security-groups
AWS CLI command on this link: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html
Prerequisites
You will need to have the AWS CLI command line tool installed and configured on your system.
You can find the necessary information on this two links:
- https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html
- https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html
How to use
-
Open a terminal window on a local folder and run this command:
aws ec2 describe-security-groups --group-ids sg-123abcd4
You will need the ID of the Security Group you want the backup (in this example 'sg-123abcd4').
Done! You can now backup the newly created .json file with GIT or in any other way you want.
About the .json file
As a quick reference here is how the .json file looks:
{ "SecurityGroups": [ { "IpPermissionsEgress": [], "Description": "My security group", "IpPermissions": [ { "PrefixListIds": [], "FromPort": 22, "IpRanges": [ { "CidrIp": "203.0.113.0/24" } ], "ToPort": 22, "IpProtocol": "tcp", "UserIdGroupPairs": [] } ], "GroupName": "MySecurityGroup", "OwnerId": "123456789012", "GroupId": "sg-903004f8", } ] }
Top comments (2)
Hello Lucas, thanks for you post. Just a quick question can I use this method to copy security groups from one aws account to another?
Nice - this has always been in the back of my mind for needed retention.