AWS Classroomnotes 26/Sep/2023

AWS CLI

Activity2: Create a mysql rds instance

  • Steps:

    • Create a subnet group
    • Create a securiry group
    • Create a rds instance
  • Creating a subnet group Refer Here for the changes
  • Searching a sub string Refer Here
  • refer below script
#!/bin/bash

# get_default_vpc_id()
# This function gets the default vpc id
function get_default_vpc_id() 
{
    vpc_id=$(aws ec2 describe-vpcs --filters "Name=is-default,Values=true" --query "Vpcs[].VpcId" --output text)
    echo $vpc_id
}

# get_subnet_ids(vpc_id)
# This function gets the subnet ids based on vpc's passed
function get_subnet_ids()
{
    vpc_id=$1
    subnets=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$vpc_id" --query "Subnets[].SubnetId" --output text)
    echo $subnets
}

# exists(string, substring)
# This function gets the subnet ids based on vpc's passed
function exists() 
{
    #STR='GNU/Linux is an operating system'
    #SUB='Linux'
    STR=$1
    SUB=$2
    if [[ "$STR" == *"$SUB"* ]]; then
        echo "Exists"
    else
        echo "NotExists"
    fi


}

subnet_group_name='custom'
subnet_ids=$(get_subnet_ids $(get_default_vpc_id))

all_subnet_groups=$(aws rds describe-db-subnet-groups --query "DBSubnetGroups[].DBSubnetGroupName"  --output text)
echo $all_subnet_groups
exists_output=$(exists "$all_subnet_groups" $subnet_group_name)
if [[  $exists_output == "Exists" ]]; then
    echo "Subnet group already exists"
else
    echo "creating subnet group with ids ${subnet_ids}"
    aws rds create-db-subnet-group \
        --db-subnet-group-name $subnet_group_name \
        --db-subnet-group-description "created from cli" \
        --subnet-ids $subnet_ids \
        --query "DBSubnetGroup.DBSubnetGroupName"
    echo "Created subnet group"
fi


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner