← Solutions Architect Associate study path

Practice in your account · 40 min

A private Amazon RDS database with Secrets Manager

In this lab you will create a MySQL database with no public IP, encrypted and with its password managed by RDS, and you will connect to it without opening any port.

In this lab you will build the data tier of a three-tier architecture, which is the part where the data the application stores and looks up lives. That tier is an Amazon RDS for MySQL that only accepts connections from the application. Amazon RDS is the AWS service that installs, backs up and maintains the database for you, and MySQL is the type of database you will use. For example, an online store would keep its products and orders in this database, and only the store's program could read them.

That database has no public IP, which is the address something can be reached at from the internet, and it is . Its password is kept by AWS Secrets Manager, a service that stores passwords and other secrets and hands them only to whoever has permission, so the password never appears in your code or in your terminal. These topics belong to domain 1 of the exam, in the part about encryption, secrets and network access, and to domain 3, in the part about choosing the database.

To get into the database you will use a client and Session Manager, which opens a terminal on the instance from the console, without and without inbound . A terminal is a window where you type commands, and the client instance plays the role of the application server. From that terminal you will create a table and query it without ever typing the database password. For example, this is how someone at the store who needs to check the orders could work without anyone sending them the password over chat.

Amazon RDS in private subnets of the VPC

The database has three protections:

  • The DB subnet group, which is the list of subnets where RDS can place the database and which spans data subnets in two . A subnet is a part of the network with its own rules, and the data subnets have no way out to the internet.
  • The database's security group, which is the database's list of rules and which accepts port 3306 only from the application's group.
  • The master password, which is the password of the database's main user and which lives in Secrets Manager.

For example, to read the store's orders, someone would have to be inside the network, on a server with the application's group, and also have permission to read the secret. Multi-AZ, which keeps a second instance on standby in another zone, is turned off in this lab so you don't pay for that instance.

Before you start

First do the budget lab, because this database charges by the hour while it exists. If your account uses the free plan, keep in mind that creating an RDS database is one of the five activities that give you extra credit, which is a balance AWS gives you to pay your charges.

The CloudFormation template, which is the file that describes everything that will be created, creates IAM roles, so when you create the stack you have to check the box under Capabilities. An is what gives the instance its permissions, and the box confirms you know the template creates permissions. If your account is a new experience account, your project is in us-east-2, and the template creates its own VPC.

What the template creates

The [lab10-rds.yaml](/roadmaps/plantillas/lab10-rds.yaml) template creates a 10.10.0.0/16 VPC, which is a private network of your own inside AWS, with one public subnet for the client and two private data subnets in two zones. The route table of the private subnets, which is the list of paths their traffic can follow, only has the local route, so they have no path to the internet. For example, even if someone knew the address of the store's database, there is no path from the internet that reaches it.

Inside that network, the template puts a DB subnet group with the two private subnets and a db.t4g.micro RDS for MySQL instance, a small database size, with 20 GiB of encrypted gp3 storage, which is the disk where the database keeps its data. RDS manages that database's master password in Secrets Manager.

The client is a t3.micro instance with Amazon Linux 2023, the AmazonSSMManagedInstanceCore role and permission to read only that secret. That role is what lets Session Manager open the terminal on the instance. The client's security group has no inbound rules, because Session Manager doesn't need them. For example, nobody on the internet can try to get into the store's client, because it has no open door, and you still get in from the console.

The AWS tutorial that goes with this step does something similar by hand, with the default VPC, which is the network AWS creates on its own in each Region, and a web server with PHP. The template leaves out the web server and isolates the database from the start.

Create the database and check its configuration

  1. In CloudFormation, choose Create stack, With new resources (standard), Upload a template file and upload lab10-rds.yaml.
  2. Name the stack cloudpath-saa-lab10-rds. Leave the default parameters.
  3. Under Capabilities, check I acknowledge that AWS CloudFormation might create IAM resources and choose Submit. It takes about 10 minutes, almost all of them creating the database.
  4. Open the RDS console and the cloudpath-saa-lab10-rds-mysql database. Write down the endpoint and the zone. Check that it isn't publicly accessible, that encryption is turned on and that the password is managed in Secrets Manager.
  5. Open Secrets Manager. You will see a secret that starts with rds!db-. RDS created it and rotates it every seven days by default. You don't need to see its value.

The endpoint is the address the application uses to find the database, similar to a website's address. Rotating a secret means replacing the password with a new one every so often, and in this lab RDS does it for you. For example, if the password of the store's database leaked today, within seven days at most it would no longer work, and the store's program would keep getting in because it always reads the current password from the secret.

Connect with Session Manager

Session Manager opens the client instance's terminal for you in the browser. From there, the instance uses its role to ask Secrets Manager for the password and connects to the database, as the application server would. For example, at the store, this would be the path the orders program follows every time it needs to save a purchase.

  1. In the EC2 console, select cloudpath-saa-lab10-cliente, choose Connect, the Session Manager tab and Connect again. If it tells you the instance isn't ready, wait a couple of minutes.
  2. In the terminal, copy and paste the commands from the stack's ComandoContrasena and ComandoConexion outputs (Outputs tab). The first one reads the password from Secrets Manager with the instance's role and the second one opens the MySQL console.
  3. Create a table and query it:

sql CREATE TABLE pedidos (id INT PRIMARY KEY AUTO_INCREMENT, producto VARCHAR(50), creado TIMESTAMP DEFAULT CURRENT_TIMESTAMP); INSERT INTO pedidos (producto) VALUES ('teclado'), ('monitor'), ('mouse'); SELECT * FROM pedidos;

Check that the database can't be reached from outside

runs outside your VPC, so it is useful for testing what someone outside would see. The command in step 9 tries to open a connection to the database's port 3306 for 5 seconds and writes whether it managed to. For example, it is the same test someone on the internet who found the address of the store's database would run.

  1. Now try to reach the database from outside the VPC. Open CloudShell and test the port with the endpoint you wrote down:

bash timeout 5 bash -c "</dev/tcp/<endpoint>/3306" && echo "abierto" || echo "no conecta"

The result has to be "no conecta", because the database has no public IP and its security group doesn't accept anything that doesn't come from the client's group.

If your account is a new experience account, all of this works the same way in us-east-2. What you couldn't do there is a read replica in another Region, because projects don't support cross-Region features. A read replica is a copy of the database that is kept up to date and only serves queries. For example, the store could put a replica in another Region so its reports read from the copy and don't load the main database.

Why doesn't it connect?

From CloudShell you test port 3306 of the lab database and the result is "no conecta". Why?

How to know you're done

The lab is complete when the query in step 8 returns three rows without you having typed the password at any point. To check the database from CloudShell:

aws rds describe-db-instances --db-instance-identifier cloudpath-saa-lab10-rds-mysql \
  --query "DBInstances[0].[DBInstanceStatus,PubliclyAccessible,StorageEncrypted,MasterUserSecret.SecretStatus]" \
  --output table

The result has to show available, False, True and active.

Delete what you created

To delete the lab, in CloudFormation select cloudpath-saa-lab10-rds and choose Delete. The template deletes the database without a final snapshot and without automated backups, terminates the client and deletes the VPC. A snapshot is a full copy of the database stored separately, and automated backups are the copies RDS makes on its own; in a lab you don't need them because the data is test data, but a real store would want its snapshot before deleting its database. When you delete a database that manages its secret, RDS also deletes the secret, so check in Secrets Manager that the rds!db-... secret no longer appears.

On the exam

On the exam, questions about the data tier can include a condition in the scenario that points to a service, such as these:

  • The condition that the database credentials, which are the username and password, must not be in the code, which points to Secrets Manager. If it also asks to "rotate automatically", Secrets Manager wins over Parameter Store, another AWS service that stores configuration values.
  • The condition that "only the application tier should reach the database", which is solved with a security group that references the application's group, because a network ACL, which sets rules for a whole subnet by address, doesn't do that.
  • The condition to "access an instance without opening ports or using a bastion host", which points to Session Manager. A bastion host is a server reachable from the internet that is used only as a door to get into the private servers.

For example, if the scenario says a store wants its program to read the database password without storing it in the code and wants the password to change on its own every so often, the answer is Secrets Manager.

Where do you store the password?

An application connects to an Amazon RDS database. The credentials must not be in the code and they have to rotate automatically. What do you choose?

Next

  • Tutorial: Create a web server and an Amazon RDS DB instance: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/TUT_WebAppWithRDS.html
  • cloud path practice exams: /examenes?lang=en