AWS Classroomnotes 27/Sep/2023

Activity2: Create a mysql rds instance

Exercises

  • Correct the below script to use subnet_group_name and security_group_id
#!/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()
{
    default_vpc_id=$(get_default_vpc_id)
    vpc_id=${1:-$default_vpc_id}
    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)

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

# create_security_group(name,description,vpc_id, port, whom)
# This function gets the subnet ids based on vpc's passed
function create_security_group()
{
    all_ip='0.0.0.0/0'
    default_vpc_id=$(get_default_vpc_id)
    name=${1:-rdssg}
    description=${2:-rdssg}
    vpc_id=${3:-$default_vpc_id}
    port=${4:-3306}
    whom=${5:-$all_ip}
    all_security_groups=$(aws ec2 describe-security-groups \
        --filters Name=vpc-id,Values=$vpc_id \
        --query "SecurityGroups[].GroupName" \
        --output text \
        )
    exists_output=$(exists "$all_security_groups" $name)

    if [[  $exists_output == "Exists" ]]; then
        echo "Security group already exists"
    else
        echo "creating security group "
        group_id=$(aws ec2 create-security-group \
            --description "$description" \
            --group-name $name \
            --vpc-id $vpc_id \
            --query "GroupId" \
            --output text
        )
        echo "Created subnet"

        aws ec2 authorize-security-group-ingress \
                --group-id $group_id \
                --protocol tcp \
                --port ${port} \
                --cidr ${whom}
        echo "create a security group ingress rule"
    fi


}

security_group_name="myrdssg"
description="open mysql to every one"
create_security_group $security_group_name "$description"

# create_security_group(identifier,size,engine, username, password, storage_size,subnet_group,security_group_id)
# This function gets the subnet ids based on vpc's passed
function create_rds()
{
    identifier=$1
    size=${2:-db.t2.micro}
    engine=${3:-mysql}
    username=${4:-qtdevops}
    password=${5:-qtdevopsqtdevops}
    storage_size=${6:-20}
    aws rds create-db-instance \
        --db-instance-identifier "${identifier}" \
        --db-instance-class "${size}" \
        --engine "${engine}" \
        --master-username "${username}" \
        --master-user-password "${password}" \
        --allocated-storage ${storage_size} \

}

create_rds "qtdevopsfromcli"

  • Create a script which create an ec2 instance and displays ssh command
    • note: add a tag creator=script
  • Create a script which delete all the ec2 instances created by scripts
    • Find all the ec2 instances with tag creator=script and delete them

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube