Skip to content

Commit

Permalink
fix rds
Browse files Browse the repository at this point in the history
  • Loading branch information
mnarayan1 committed Jun 1, 2023
1 parent b55f40b commit 45dbf99
Showing 1 changed file with 246 additions and 0 deletions.
246 changes: 246 additions & 0 deletions _notebooks/2023-06-1-rdstutorial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# RDS Tutorial\n",
"> Amazon RDS tutorial with Spring Boot\n",
"\n",
"- toc: true \n",
"- badges: true\n",
"- comments: true\n",
"- categories: [jupyter]"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# What is RDS? \n",
"\n",
"Simple Definition: Rather than having a database stored on a local file, the RDS database is stored on the cloud. \n",
"\n",
"## Further Research/More Information: \n",
"- RDS: Relational Database Service\n",
" - a web service provided by Amazon Web Services (AWS) that allows individuals to set up, operate, and scale a database while storing it in the cloud\n",
" - RDS makes it easy to set up, operate, and scale a MySQL database instance in the case of AP Computer Science at Del Norte High Schools\n",
"\n",
"\n",
"- By using RDS, individuals can offload the administrative tasks of database management\n",
" - examples: hardware provisioning, software patching, backups, and database scaling, to AW\n",
" - RDS takes care of the underlying infrastructure allowing more time to work on website features\n",
"\n",
"- Key Features and Benefits of RDS:\n",
" - Managed service: AWS manages the infrastructure, database setup, patching, and backups, reducing your administrative burden.\n",
" - Scalability: RDS allows you to scale your database instance up or down based on a users needs, which ensures efficiency\n",
" - Security: RDS provides built-in security features, such as encryption at rest and in transit, network isolation, and user access control\n",
" - Automated software patching: RDS can automatically apply patches and updates to the database software, reducing downtime and ensuring security.\n",
" - Stores information in the cloud instead of just locally --> can not be easily deleted and ruined\n",
"\n",
"\n",
"- Overall Takeaway:\n",
" - RDS simplifies the process of deploying and managing relational databases, making it a popular choice for many organizations seeking a scalable and reliable database solution in the cloud\n",
"\n",
"## Even More Information\n",
"- Follow these Youtube Links for More Information on RDS on AWS\n",
" - https://www.youtube.com/watch?v=GvUaA9cygUk\n",
" - https://www.youtube.com/watch?v=tLp8pPNdDXQ&pp=ygUHcmRzIGF3cw%3D%3D\n",
" - https://www.youtube.com/watch?v=vp_uulb5phM&pp=ygUHcmRzIGF3cw%3D%3D"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**This step is optional if you don't want to link your RDS database to the EC2 instance. If you already have an EC2 instance for your class, you don't need to do this**\n",
"\n",
"# Step 1: Create an EC2 Instance\n",
"\n",
"Create an Amazon EC2 instance that you will use to connect to your database.\n",
"\n",
"*To create EC2 instance*\n",
"1. Sign in to the AWS Management Console and open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.\n",
"2. In the upper-right corner of the AWS Management Console, choose the AWS Region in which you want to create the EC2 instance.\n",
"3. Choose EC2 Dashboard, and then choose Launch instance, as shown in the following image (The Launch an instance page opens)\n",
"<img src=\"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/Tutorial_WebServer_11.png\">\n",
"\n",
"\n",
"4. Choose the following settings on the Launch an instance page.\n",
" - Under Name and tags, for Name, enter ec2-database-connect.\n",
" - Under Application and OS Images (Amazon Machine Image), choose Amazon Linux, and then choose the Amazon Linux 2023 AMI. Keep the default selections for the other choices.)\n",
" <br>\n",
" <img src=\"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/Tutorial_WebServer_12.png\">\n",
"\n",
" - Under Instance type, choose t2.micro.\n",
" - Under Key pair (login), choose a Key pair name to use an existing key pair. To create a new key pair for the Amazon EC2 instance, choose *Create new key pair* and then use the *Create key pair* window to create it.\n",
" - For Allow SSH traffic in Network settings, choose the source of SSH connections to the EC2 instance.\n",
" - Note: You can choose My IP if the displayed IP address is correct for SSH connections. Otherwise, you can determine the IP address to use to connect to EC2 instances in your VPC using Secure Shell (SSH). To determine your public IP address, in a different browser window or tab, you can use the service at https://checkip.amazonaws.com. An example of an IP address is 192.0.2.1/32.\n",
" - In many cases, you might connect through an internet service provider (ISP) or from behind your firewall without a static IP address. If so, make sure to determine the range of IP addresses used by client computers.\n",
"\n",
" - *WARNING*: If you use 0.0.0.0/0 for SSH access, you make it possible for all IP addresses to access your public EC2 instances using SSH. This approach is acceptable for a short time in a test environment, but it's unsafe for production environments. In production, authorize only a specific IP address or range of addresses to access your EC2 instances using SSH.\n",
"\n",
" - Example for Network Settings Section:\n",
" <img src=\"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/EC2_RDS_Connect_NtwkSettings.png\">\n",
"\n",
" - Leave the default values for the remaining sections.\n",
" - Review a summary of your EC2 instance configuration in the *Summary* panel, and when you're ready, choose *Launch instance*.\n",
"\n",
"5. On the Launch Status page, note the identifier for your new EC2 instance, for example: i-1234567890abcdef0.\n",
" <img src=\"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/getting-started-ec2-id.png\">\n",
"\n",
"\n",
"6. Choose the EC2 instance identifier to open the list of EC2 instances, and then select your EC2 instance.\n",
"\n",
"7. In the *Details* tab, note the following values, which you need when you connect using SSH:\n",
" - In *Instance summary*, note the value for *Public IPv4 DNS*.\n",
" <img src=\"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/easy-create-ec2-public-dns.png\">\n",
"\n",
"\n",
" - In *Instance details*, note the value for *Key pair name*.\n",
" <img src=\"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/easy-create-ec2-key-pair.png\">\n",
"\n",
"\n",
"8. Wait until the Instance state for your EC2 instance has a status of *Running* before continuing."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Step 2: Create a PostgreSQL DB instance\n",
"\n",
"- The basic building block of Amazon RDS is the DB instance. This environment is where you run your PostgreSQL databases.\n",
"- In this example, you use *Easy create* to create a DB instance running the PostgreSQL database engine with a db.t3.micro DB instance class.\n",
"- If you are a visual learner, you can watch this <a href=\"https://www.youtube.com/watch?v=GSu1g9jvFhY\">video</a> to see how to create the database step-by-step. Just use postgresql instead of MySQL. \n",
"\n",
"**To create a PostgreSQL DB instance with Easy create**\n",
"1. Sign in to the AWS Management Console and open the Amazon RDS console at https://console.aws.amazon.com/rds/.\n",
"2. In the upper-right corner of the Amazon RDS console, choose the AWS Region in which you want to create the DB instance.\n",
"3. In the navigation pane, choose Databases.\n",
"4. Choose Create database and make sure that Easy create is chosen\n",
"<img src=\"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/easy-create-option.png\">\n",
"\n",
"5. In Configuration, choose PostgreSQL.\n",
"\n",
"6. For DB instance size, choose Free tier.\n",
"\n",
"7. For DB instance identifier, enter database-test1.\n",
"\n",
"8. For Master username, enter a name for the master user, or keep the default name (postgres).\n",
" - The Create database page should look similar to the following image.\n",
"<img src= \"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/easy-create-postgresql.png\">\n",
"\n",
"9. To use an automatically generated master password for the DB instance, select Auto generate a password.\n",
" - To enter your master password, make sure Auto generate a password is cleared, and then enter the same password in Master password and Confirm password.\n",
"\n",
"10. To set up a connection with the EC2 instance you created previously, open Set up EC2 connection - optional.\n",
" - Select Connect to an EC2 compute resource. Choose the EC2 instance you created previously.\n",
" <img src= \"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/EC2_RDS_Setup_Conn-EasyCreate.png\">\n",
"\n",
"11. Open View default settings for Easy create.\n",
" <img src= \"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/easy-create-view-default-postgres.png\">\n",
" You can examine the default settings used with Easy create. The Editable after database is created column shows which options you can change after you create the database.\n",
" - If a setting has No in that column, and you want a different setting, you can use Standard create to create the DB instance.\n",
" - If a setting has Yes in that column, and you want a different setting, you can either use Standard create to create the DB instance, or modify the DB instance after you create it to change the setting.\n",
"\n",
"12. Choose Create database.\n",
" - To view the master username and password for the DB instance, choose View credential details.\n",
" - You can use the username and password that appears to connect to the DB instance as the master user.\n",
"\n",
" **Important**\n",
" - You can't view the master user password again. If you don't record it, you might have to change it.\n",
" - If you need to change the master user password after the DB instance is available, you can modify the DB instance to do so. For more information about modifying a DB instance, see Modifying an Amazon RDS DB instance.\n",
"\n",
"13. In the Databases list, choose the name of the new PostgreSQL DB instance to show its details.\n",
" - The DB instance has a status of Creating until it is ready to use.\n",
"<img scr = \"https://docs.aws.amazon.com/images/AmazonRDS/latest/UserGuide/images/Postgres-Launch06.png\">\n",
" - When the status changes to Available, you can connect to the DB instance. Depending on the DB instance class and the amount of storage, it can take up to 20 minutes before the new instance is available.\n",
"\n",
" **Important: You need to make your database publically available and have the following security rules in order to connect it to your backend**\n",
" - <a href=\"https://www.techtarget.com/searchcloudcomputing/tip/How-to-create-Amazon-EC2-security-groups#:~:text=IT%20teams%20can%20create%20a,assigned%20to%20a%20specific%20VPC.\">Tutorial for creating security groups</a>\n",
" - <a href=\"https://serverfault.com/questions/655042/how-do-i-link-a-security-group-to-my-aws-rds-instance\">Link the security group to your RDS Database</a>\n",
"\n",
" When creating the security group, add these inbound rules:\n",
" - Type: HTTP, Protocol: TCP, Port Range: 80, Source: Anywhere\n",
" - Type: SSH, Protocol: TCP, Port range: 22, Source: Anywhere\n",
" - Type: Custom TCP, Port Range: 542 (this is the Postgresql port), Source: Anywhere\n",
"\n",
" Once you've created your database, use the ``psql`` command to check your database is running and accessible. \n",
"\n",
" ``psql -h <rds link> -p <port> -U <username>``\n",
"\n",
" Once you're on the psql command line, use the following command to create a database:\n",
"\n",
" ``CREATE DATABASE databasename;`` \n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Step 3: Connecting the RDS Database to Spring Boot\n",
"\n",
"<a href=\"https://nirajsonawane.github.io/2022/04/18/Spring-Boot-AWS-RDS/\">This article</a> is a useful resources for this step. However, before you begin this step, make sure you are able to access your database. \n",
"\n",
"1. Add the following dependences to your ``pom.xml`` file. \n",
"\n",
"```\n",
"<dependency>\n",
"\t<groupId>org.springframework.boot</groupId>\n",
"\t<artifactId>spring-boot-starter-data-jpa</artifactId>\n",
"</dependency>\n",
"<dependency>\n",
"\t<groupId>org.springframework.boot</groupId>\n",
"\t<artifactId>spring-boot-starter-web</artifactId>\n",
"</dependency>\n",
"<dependency>\n",
"\t<groupId>org.postgresql</groupId>\n",
"\t<artifactId>postgresql</artifactId>\n",
"\t<scope>runtime</scope>\n",
"</dependency>\n",
"\n",
"```\n",
"\n",
"2. Add the following database configurations to your ``application.properties`` file. \n",
"\n",
"```\n",
"spring.datasource.url=jdbc:postgresql://<linke to database>:<database port>/<database you created with the CREATE DATABASE command>\n",
"spring.database.driverClassName=org.postgresql.Driver\n",
"spring.datasource.username=<rds username>\n",
"spring.datasource.password=<rds password>\n",
"spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect\n",
"spring.jpa.hibernate.ddl-auto=update\n",
"spring.jpa.hibernate.show-sql=true\n",
"spring.profiles.active = dev\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 45dbf99

Please sign in to comment.