DEV Community

Lucas Dasso
Lucas Dasso

Posted on

How to backup AWS Security Groups settings

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:

How to use

  1. 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').

  2. 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",
        }
    ]
}

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
mada0007 profile image
Mustaph A

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?

Collapse
 
dietertroy profile image
Troy

Nice - this has always been in the back of my mind for needed retention.