Day 42: Relational Database Service in AWS

Rahul sharmaRahul sharma
10 min read

Amazon Relational Database Service (RDS) is a web service provided by Amazon Web Services (AWS) that makes it easier to set up, operate, and scale a relational database in the cloud. RDS supports various database engines including MySQL, PostgreSQL, Oracle, SQL Server, and Amazon Aurora (a MySQL and PostgreSQL-compatible database built for the cloud).

Here are some key features and benefits of using Amazon RDS:

  1. Managed Service: AWS manages the database infrastructure for you, including tasks like patching, backups, monitoring, and scaling, allowing you to focus on your application development rather than database administration.

  2. Scalability: RDS makes it easy to scale your database instance vertically (by changing the instance type) or horizontally (by adding read replicas). This ensures that your database can handle varying workloads and growing data volumes.

  3. High Availability: RDS offers built-in high availability features such as automated backups, Multi-AZ (Availability Zone) deployments, and failover, which help ensure that your database remains accessible and resilient to hardware failures or other disruptions.

  4. Security: RDS provides several security features including network isolation using Amazon VPC (Virtual Private Cloud), encryption at rest using AWS Key Management Service (KMS), encryption in transit using SSL/TLS, and IAM (Identity and Access Management) integration for fine-grained access control.

  5. Monitoring and Metrics: AWS CloudWatch integration allows you to monitor the performance of your RDS instances, set up alarms for specific metrics, and automate actions based on certain events or thresholds.

  6. Compatibility and Flexibility: With support for multiple database engines, RDS allows you to choose the engine that best fits your application requirements. Additionally, RDS supports various storage types (e.g., General Purpose SSD, Provisioned IOPS SSD, and Magnetic) and instance types, giving you the flexibility to optimize performance and cost according to your needs.

Overall, Amazon RDS simplifies the process of deploying and managing relational databases in the cloud, providing developers with a reliable and scalable solution for their data storage needs.

Create a Free tier RDS instance of MySQL

To create a Free Tier RDS instance of MySQL on AWS, you can follow these steps:

  1. Sign in to the AWS Management Console: Go to the AWS Management Console at https://console.aws.amazon.com and sign in to your AWS account.

  2. Navigate to the RDS Dashboard: Once logged in, navigate to the Amazon RDS dashboard by searching for "RDS" in the AWS services search bar or by selecting "Database" under the "Services" dropdown menu.

  3. Click on "Create database": On the RDS dashboard, click on the "Create database" button to start the instance creation process.

  4. Select the Engine: Choose the MySQL engine for your RDS instance. Make sure to select a version that is eligible for the Free Tier.

  5. Choose a Use Case: Select the "Dev/Test" option if this database will be used for development or testing purposes.

  6. Specify DB Details: Here you'll configure the instance details such as DB instance size, storage type, DB instance identifier, username, and password. Make sure to choose options that are eligible for the Free Tier.

  7. Configure Advanced Settings: You can configure additional settings such as VPC, subnet group, security group, backup retention period, monitoring, and maintenance options. For the Free Tier, you can choose default settings for most of these options.

  8. Add Tags (Optional): You can optionally add tags to your RDS instance for easier management and tracking.

  9. Review and Launch: Review the configuration details you've provided, make any necessary changes, and then click on "Create database" to launch your Free Tier RDS instance.

  10. Wait for the Instance to be Created: It may take a few minutes for your RDS instance to be created. Once it's ready, you can view its details on the RDS dashboard.

Remember to monitor your usage to ensure that you stay within the Free Tier limits to avoid incurring any charges.

Create an EC2 instance

Sure, here's a step-by-step guide to creating an EC2 instance in the AWS Management Console:

  1. Sign in to the AWS Management Console: Go to the AWS Management Console at https://console.aws.amazon.com and sign in to your AWS account.

  2. Navigate to the EC2 Dashboard: Once logged in, navigate to the Amazon EC2 dashboard by searching for "EC2" in the AWS services search bar or by selecting "Compute" under the "Services" dropdown menu.

  3. Launch Instance: Click on the "Launch Instance" button to start the instance creation process.

  4. Choose an Amazon Machine Image (AMI): Select the AMI that you want to use for your instance. You can choose from a wide range of pre-configured AMIs provided by AWS or use your own custom AMI.

  5. Choose an Instance Type: Select the instance type that best fits your needs. Instance types vary in terms of CPU, memory, storage, and networking capacity.

  6. Configure Instance Details: Specify configuration details such as the number of instances to launch, network settings, subnet, IAM role (if applicable), and other advanced settings. You can leave most settings as default for a basic setup.

  7. Add Storage: Specify the storage requirements for your instance. You can add additional EBS volumes if needed. By default, EC2 instances come with a root volume that uses Amazon EBS (Elastic Block Store).

  8. Add Tags (Optional): You can optionally add tags to your instance for easier management and tracking.

  9. Configure Security Group: Define the security group settings for your instance. A security group acts as a virtual firewall that controls the traffic to and from your instance. You can create a new security group or select an existing one.

  10. Review and Launch: Review the configuration details you've provided, make any necessary changes, and then click on "Launch" to launch your EC2 instance.

  11. Select Key Pair: If you haven't already created a key pair, you'll be prompted to create one. Key pairs are used for secure SSH access to your instance. Download and save the private key (.pem file) to your local machine. Make sure to keep it secure, as you'll need it to access your instance.

  12. Launch Instance: After selecting a key pair, click on "Launch Instances" to launch your EC2 instance.

Your EC2 instance will now be provisioned and launched. Once it's ready, you can connect to it using SSH or RDP depending on the operating system of your instance.

Create an IAM role with RDS access

Sure, here's how you can create an IAM role with RDS access in the AWS Management Console:

  1. Sign in to the AWS Management Console: Go to the AWS Management Console at https://console.aws.amazon.com and sign in to your AWS account.

  2. Navigate to the IAM Dashboard: Once logged in, navigate to the IAM (Identity and Access Management) dashboard by searching for "IAM" in the AWS services search bar or by selecting "IAM" under the "Security, Identity, & Compliance" section.

  3. Choose "Roles" from the left-hand menu: In the IAM dashboard, click on "Roles" from the left-hand menu.

  4. Create Role: Click on the "Create role" button to start creating a new IAM role.

  5. Selec**t type of trusted entity**: Choose the type of trusted entity that will assume the role. In this case, select "AWS service" since we want to grant RDS access to an AWS service.

  6. Choose the se**rvice that will use this role**: In the "Choose a use case" section, select "RDS" as the service that will use this role.

  7. Permissions: In the "Attach permissions policies" section, you can either choose existing policies to attach to this role or create a custom policy. To grant RDS access, you can use the existing policy called "AmazonRDSFullAccess" which provides full access to RDS resources. Alternatively, you can create a custom policy with more granular permissions if needed.

  8. Review: Review the role configuration and make sure everything looks correct.

  9. Name and Description: Provide a name and optional description for your role.

  10. Create Role: Click on the "Create role" button to create the IAM role.

Once the role is created, you can attach it to EC2 instances, Lambda functions, or other AWS resources that need access to RDS. When these resources assume the role, they will inherit the permissions granted to the role, allowing them to interact with RDS resources according to the permissions defined in the attached policies.

Assign the role to EC2 so that your EC2 Instance can connect with RDS

To assign the IAM role we just created to an EC2 instance so that it can connect with RDS, follow these steps:

  1. Launch an EC2 Instance: If you haven't already launched an EC2 instance, you can follow the steps outlined earlier to do so.

  2. Select or Launch an Existing Instance: Go to the EC2 dashboard and select the EC2 instance to which you want to assign the IAM role. Alternatively, you can launch a new EC2 instance if needed.

  3. Stop the Instance (if running): If the instance is already running, stop it. You can't attach an IAM role to a running instance.

  4. Select the Instance: In the EC2 dashboard, select the instance you want to assign the IAM role to by clicking on its checkbox.

  5. Navigate to Actions Menu: Click on the "Actions" dropdown menu at the top of the EC2 dashboard.

  6. Choose Instance Settings > Attach/Replace IAM Role: In the dropdown menu, navigate to "Instance Settings" and then select "Attach/Replace IAM Role."

  7. Select IAM Role: In the "Attach/Replace IAM Role" window, you'll see a list of available IAM roles. Select the IAM role that you created earlier, which has permissions to access RDS resources.

  8. Confirm: After selecting the IAM role, click on the "Apply" button to confirm and attach the role to the EC2 instance.

  9. Start the Instance: If you stopped the instance in step 3, start it again.

Once the EC2 instance is running and has the IAM role attached, it will have the necessary permissions to access RDS resources according to the permissions defined in the attached IAM role. You can now configure your application or services running on the EC2 instance to connect to the RDS instance using the appropriate credentials and endpoint.

Once the RDS instance is up and running, get the credentials and connect your EC2 instance using a MySQL client.

Once your RDS instance is up and running, you can retrieve the credentials and connect your EC2 instance to it using a MySQL client. Here's how you can do it:

  1. Retrieve RDS Endpoint and Port: In the AWS Management Console, go to the RDS dashboard and select your RDS instance. Note down the endpoint (DNS name) and port number of your RDS instance.

  2. Retrieve RDS Master Username: Note down the master username of your RDS instance. This is the username you specified when creating the RDS instance.

  3. Retrieve RDS Master Password: If you haven't saved the master password when creating the RDS instance, you can retrieve it by going to the RDS dashboard, selecting your instance, and clicking on the "Modify" button. In the Modify DB Instance window, you can view and update the master password. Make sure to save the password securely.

  4. Install MySQL Client on EC2 Instance: If you haven't already installed a MySQL client on your EC2 instance, you can do so using package managers like apt (for Ubuntu/Debian) or yum (for CentOS/RHEL). For example:

     sqlCopy codesudo apt update
     sudo apt install mysql-client
    
  5. Connect to RDS from EC2 Instance: Once you have the credentials and MySQL client installed on your EC2 instance, you can connect to the RDS instance using the following command:

     phpCopy codemysql -h <RDS_ENDPOINT> -P <RDS_PORT> -u <MASTER_USERNAME> -p
    

    Replace <RDS_ENDPOINT> with the endpoint of your RDS instance, <RDS_PORT> with the port number (usually 3306 for MySQL), and <MASTER_USERNAME> with the master username of your RDS instance. When you run this command, you'll be prompted to enter the master password.

  6. Verify Connection: After entering the master password, you should be connected to your RDS instance via the MySQL client running on your EC2 instance. You can now execute SQL queries and manage your MySQL databases as needed.

Make sure that your EC2 instance has network connectivity to the RDS instance (e.g., they are both in the same VPC and security groups allow traffic between them) for successful connection.

1
Subscribe to my newsletter

Read articles from Rahul sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul sharma
Rahul sharma