AWS CLI v2 by Examples: Managing Security Groups for RDS Instances
AWS CLI v2 by Examples: Managing Security Groups for RDS Instances
Dive into managing security groups for RDS instances using AWS CLI v2 with use cases covering creation, deletion, listing, inbound rule authorization, revocation, and association with RDS. This article highlights practical examples for handling the lifecycle of security groups and rule configurations to secure your RDS deployments. By following these examples, you’ll not only automate common tasks but also gain insights into best practices and robust security management techniques.

In this guide, we explore two main categories: Security Group Creation and Deletion and Security Group Rule Configuration & Association with RDS Instances. We start with managing the lifecycle of security groups — including creating new groups, listing them, and deleting outdated ones — then move on to adjusting inbound/outbound rules and associating the right security group with your RDS databases using AWS CLI commands. Each section offers detailed sample scripts with comprehensive code to help you automate these tasks and integrate them within your infrastructure management strategies.
Section 1: Security Group Creation and Deletion
Overview
This section dives into the lifecycle management of RDS security groups, providing complete practical examples for effective resource management. It includes use cases for creating a new security group, deleting an existing one, and listing all available security groups using AWS CLI v2. Each use case is followed by a sample script to illustrate the process, ensuring you understand how to automate these tasks reliably and efficiently.
Use Case 1: Create a New Security Group
Description: Creating a new security group is essential for defining network access controls for your RDS instances. The sample below demonstrates how to create a security group in your default VPC and add an ingress rule for the database port. This process provides a flexible starting point for further customization, ensuring your database is accessible while unwanted traffic is blocked.
Sample: Create a Security Group and Authorize Ingress
#!/bin/bash
export AWS_PROFILE=myprofile
region="us-west-2"
groupName="myRdsSecurityGroup"
description="Security Group for RDS instance access"
vpcId=$(aws ec2 describe-vpcs --filters Name=isDefault,Values=true --query "Vpcs[0].VpcId" --output text --region $region)
sgId=$(aws ec2 create-security-group --group-name $groupName --description "$description" --vpc-id $vpcId --query 'GroupId' --output text --region $region)
echo "Created security group with ID: $sgId"
aws ec2 authorize-security-group-ingress --group-id $sgId --protocol tcp --port 3306 --cidr 0.0.0.0/0 --region $region
echo "Ingress rule for MySQL (port 3306) added to security group."
Use Case 2: Delete an Existing Security Group
Description: Once a security group is no longer needed or must be replaced, it should be deleted to avoid leaving unused resources that may cause confusion. The following script checks for a specific security group by name and deletes it if found, ensuring your configuration stays current.
Sample: Delete a Security Group Safely
#!/bin/bash
groupName="myRdsSecurityGroup"
region="us-west-2"
sgId=$(aws ec2 describe-security-groups --filters Name=group-name,Values=$groupName --query "SecurityGroups[0].GroupId" --output text --region $region)
if [ "$sgId" = "None" ]; then
echo "Security group '$groupName' not found. Exiting."
exit 1
fi
aws ec2 delete-security-group --group-id $sgId --region $region
echo "Deleted security group with ID: $sgId successfully."
Use Case 3: List All Security Groups
Description: Listing all security groups helps you review and audit existing permissions, making it easy to track down outdated or misconfigured rules. The sample below retrieves and displays a formatted list of security groups within a selected region.
Sample: List Security Groups in a Region
#!/bin/bash
region="us-west-2"
echo "Retrieving all security groups in region: $region"
groups=$(aws ec2 describe-security-groups --region $region --query "SecurityGroups[*].[GroupId,GroupName,Description]" --output text)
echo "Security Group List:"
while IFS= read -r line; do
echo "$line"
done <<< "$groups"
echo "Completed listing security groups."
exit 0
Use Case 4: Create and Tag a New Security Group (Additional Sample)
Description: In addition to creating a security group, tagging is a best practice for resource identification and management in AWS environments.
Sample: Create and Tag a Security Group
#!/bin/bash
export AWS_PROFILE=myprofile
region="us-east-2"
groupName="taggedRdsSecurityGroup"
description="Tagged Security Group for RDS instance access"
vpcId=$(aws ec2 describe-vpcs --filters Name=isDefault,Values=true --query "Vpcs[0].VpcId" --output text --region $region)
sgId=$(aws ec2 create-security-group --group-name $groupName --description "$description" --vpc-id $vpcId --query 'GroupId' --output text --region $region)
echo "Created security group with ID: $sgId"
aws ec2 create-tags --resources $sgId --tags Key=Purpose,Value=RDSAccess --region $region
echo "Tag 'Purpose=RDSAccess' added to security group."
Section 2: Security Group Rule Configuration and Association with RDS Instances
Overview
Here we focus on tailoring and enforcing network access rules specifically for RDS instances. Practical examples include authorizing new inbound rules, revoking outdated ones, and associating security groups with RDS instances.
Use Case 1: Authorize Inbound Traffic for RDS
Description: Adjust the security group to allow access to the RDS instance by explicitly authorizing inbound traffic.
Sample: Authorize Inbound Rule for PostgreSQL
#!/bin/bash
region="us-west-2"
sgId="sg-0123456789abcdef0" # Replace with your actual security group ID
dbPort=5432
echo "Authorizing inbound traffic for PostgreSQL on port $dbPort..."
aws ec2 authorize-security-group-ingress --group-id $sgId --protocol tcp --port $dbPort --cidr 0.0.0.0/0 --region $region
echo "Ingress rule added successfully."
Use Case 2: Revoke Inbound Rule for RDS
Description: When access is no longer required or a misconfiguration is detected, removing the specific ingress rule is crucial.
Sample: Revoke Inbound Rule from Security Group
#!/bin/bash
region="us-east-1"
sgId="sg-0fedcba9876543210" # Replace with your actual security group ID
dbPort=5432
echo "Revoking inbound traffic for PostgreSQL on port $dbPort..."
aws ec2 revoke-security-group-ingress --group-id $sgId --protocol tcp --port $dbPort --cidr 0.0.0.0/0 --region $region
echo "Ingress rule revoked."
Use Case 3: Associate Security Group with RDS Instance
Description: Associating a security group with an RDS instance ensures that the correct network rules apply.
Sample: Associate a Security Group with an RDS Instance
#!/bin/bash
region="us-west-2"
dbInstanceIdentifier="my-rds-instance"
newSgId="sg-abcdefghijklmnop" # Replace with your target security group ID
echo "Associating security group $newSgId with RDS instance $dbInstanceIdentifier..."
aws rds modify-db-instance --db-instance-identifier $dbInstanceIdentifier --vpc-security-group-ids $newSgId --apply-immediately --region $region
echo "Association request submitted."
Use Case 4: Authorize Inbound Traffic for Admin Access (Additional Sample)
Description: In this additional use case, we provide an example to restrict inbound access for administrative purposes from a specific IP address.
Sample: Authorize Inbound Rule for Admin Access
#!/bin/bash
region="us-west-1"
sgId="sg-0a1b2c3d4e5f6g7h8" # Replace with your actual security group ID
adminIp="203.0.113.10/32"
adminPort=22 # Assuming SSH access for admin tasks; modify as needed.
echo "Authorizing inbound traffic for admin access from IP $adminIp on port $adminPort..."
aws ec2 authorize-security-group-ingress --group-id $sgId --protocol tcp --port $adminPort --cidr $adminIp --region $region
echo "Inbound rule for admin access added successfully."
Conclusion
In this article, we explored key use cases for managing security groups associated with RDS instances using AWS CLI v2. We covered lifecycle actions such as creating, tagging, listing, and deleting security groups, as well as fine-tuning network access through the configuration and association of security rules. Detailed sample scripts provide step-by-step guidance and practical examples to help you automate and enforce security best practices for your RDS deployments.
By incorporating these examples into your workflows, you can achieve a robust cloud security posture and streamline the management of your RDS instances. Continue exploring AWS CLI v2 features to expand your automation skills and ensure your cloud infrastructure remains secure, efficient, and up-to-date. Enjoy experimenting with these examples and adapt them to fit your unique environment and requirements.
메타데이터
- post_id
- 73ddf4fa571d
- slug
- aws-cli-v2-by-examples-managing-security-groups-for-rds-instances-73ddf4fa571d
- url
- https://medium.com/@mb20261/aws-cli-v2-by-examples-managing-security-groups-for-rds-instances-73ddf4fa571d
- canonical_url
- https://medium.com/@mb20261/aws-cli-v2-by-examples-managing-security-groups-for-rds-instances-73ddf4fa571d
- author_url
- https://medium.com/@mb20261
- status
- ok
- fetched_at
- 2026-06-13 07:35:29