{"id":1061,"date":"2025-07-03T06:27:34","date_gmt":"2025-07-03T06:27:34","guid":{"rendered":"https:\/\/blog.vavencloud.com\/?p=1061"},"modified":"2025-07-03T06:27:34","modified_gmt":"2025-07-03T06:27:34","slug":"how-to-automatically-connect-an-amazon-ec2-instance-to-an-amazon-rds-database","status":"publish","type":"post","link":"https:\/\/blog.vavencloud.com\/?p=1061","title":{"rendered":"How to Automatically Connect an Amazon EC2 Instance to an Amazon RDS Database"},"content":{"rendered":"<p data-start=\"413\" data-end=\"703\">If you&#8217;re building applications on AWS, you often need to connect your EC2 instance (which hosts your app or website) to an Amazon RDS database for data storage. AWS makes this connection easy with an <strong data-start=\"614\" data-end=\"646\">automatic connection feature<\/strong> that configures the necessary security settings for you.<\/p>\n<p data-start=\"705\" data-end=\"846\">In this tutorial, I\u2019ll walk you through <strong data-start=\"745\" data-end=\"812\">how to automatically connect an EC2 instance to an RDS database<\/strong> using the AWS Management Console.<\/p>\n<h2 data-start=\"853\" data-end=\"897\">Why Use the Automatic Connection Feature?<\/h2>\n<p data-start=\"899\" data-end=\"1046\">When connecting EC2 to RDS manually, you have to configure security groups to allow communication between them. This can be tricky and error-prone.<\/p>\n<p data-start=\"1048\" data-end=\"1099\">The <strong data-start=\"1052\" data-end=\"1084\">automatic connection feature<\/strong> takes care of:<\/p>\n<ul data-start=\"1101\" data-end=\"1278\">\n<li data-start=\"1101\" data-end=\"1166\">\n<p data-start=\"1103\" data-end=\"1166\">Creating security groups for your EC2 instance and RDS database<\/p>\n<\/li>\n<li data-start=\"1167\" data-end=\"1242\">\n<p data-start=\"1169\" data-end=\"1242\">Setting up inbound\/outbound rules that allow traffic only between the two<\/p>\n<\/li>\n<li data-start=\"1243\" data-end=\"1278\">\n<p data-start=\"1245\" data-end=\"1278\">Ensuring least privilege security<\/p>\n<\/li>\n<\/ul>\n<p data-start=\"1280\" data-end=\"1382\">This lets you focus on building your application without worrying about network configuration details.<\/p>\n<h2 data-start=\"1389\" data-end=\"1405\">Prerequisites<\/h2>\n<ul data-start=\"1407\" data-end=\"1592\">\n<li data-start=\"1407\" data-end=\"1468\">\n<p data-start=\"1409\" data-end=\"1468\">AWS account with permission to create EC2 and RDS resources<\/p>\n<\/li>\n<li data-start=\"1469\" data-end=\"1516\">\n<p data-start=\"1471\" data-end=\"1516\">Basic familiarity with AWS Management Console<\/p>\n<\/li>\n<li data-start=\"1517\" data-end=\"1592\">\n<p data-start=\"1519\" data-end=\"1592\">EC2 instance and RDS database in the <strong data-start=\"1556\" data-end=\"1575\">same AWS Region<\/strong> and <strong data-start=\"1580\" data-end=\"1592\">same VPC<\/strong><\/p>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1063 size-full\" src=\"https:\/\/blog.vavencloud.com\/wp-content\/uploads\/2025\/07\/ec2-rds-tutorial-architecture.png\" alt=\"\" width=\"1254\" height=\"728\" \/><\/p>\n<h2 data-start=\"1599\" data-end=\"1666\">Option 1: Automatically Connect EC2 to RDS Using the EC2 Console<\/h2>\n<h3 data-start=\"1668\" data-end=\"1702\">Step 1: Launch an EC2 Instance<\/h3>\n<ol data-start=\"1704\" data-end=\"2212\">\n<li data-start=\"1704\" data-end=\"1739\">\n<p data-start=\"1707\" data-end=\"1739\">Open the <strong data-start=\"1716\" data-end=\"1731\">EC2 Console<\/strong> in AWS.<\/p>\n<\/li>\n<li data-start=\"1740\" data-end=\"1770\">\n<p data-start=\"1743\" data-end=\"1770\">Click <strong data-start=\"1749\" data-end=\"1769\">Launch Instances<\/strong>.<\/p>\n<\/li>\n<li data-start=\"1771\" data-end=\"1822\">\n<p data-start=\"1774\" data-end=\"1822\">Select your preferred AMI (e.g., Ubuntu Server).<\/p>\n<\/li>\n<li data-start=\"1823\" data-end=\"1878\">\n<p data-start=\"1826\" data-end=\"1878\">Choose instance type (e.g., t2.micro for free tier).<\/p>\n<\/li>\n<li data-start=\"1879\" data-end=\"1968\">\n<p data-start=\"1882\" data-end=\"1968\">Configure instance details \u2014 make sure to launch the instance <strong data-start=\"1944\" data-end=\"1967\">in your desired VPC<\/strong>.<\/p>\n<\/li>\n<li data-start=\"1969\" data-end=\"2003\">\n<p data-start=\"1972\" data-end=\"2003\">Add storage and tags as needed.<\/p>\n<\/li>\n<li data-start=\"2004\" data-end=\"2187\">\n<p data-start=\"2007\" data-end=\"2039\">In <strong data-start=\"2010\" data-end=\"2038\">Configure security group<\/strong>:<\/p>\n<ul data-start=\"2043\" data-end=\"2187\">\n<li data-start=\"2043\" data-end=\"2107\">\n<p data-start=\"2045\" data-end=\"2107\">You can create a new security group or select an existing one.<\/p>\n<\/li>\n<li data-start=\"2111\" data-end=\"2187\">\n<p data-start=\"2113\" data-end=\"2187\">Don\u2019t worry about database access yet; this will be handled automatically.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li data-start=\"2188\" data-end=\"2212\">\n<p data-start=\"2191\" data-end=\"2212\">Launch your instance.<\/p>\n<\/li>\n<\/ol>\n<h3 data-start=\"2214\" data-end=\"2260\">Step 2: Create or Identify an RDS Database<\/h3>\n<ol data-start=\"2262\" data-end=\"2571\">\n<li data-start=\"2262\" data-end=\"2290\">\n<p data-start=\"2265\" data-end=\"2290\">Open the <strong data-start=\"2274\" data-end=\"2289\">RDS Console<\/strong>.<\/p>\n<\/li>\n<li data-start=\"2291\" data-end=\"2336\">\n<p data-start=\"2294\" data-end=\"2336\">Click <strong data-start=\"2300\" data-end=\"2313\">Databases<\/strong> &gt; <strong data-start=\"2316\" data-end=\"2335\">Create database<\/strong>.<\/p>\n<\/li>\n<li data-start=\"2337\" data-end=\"2391\">\n<p data-start=\"2340\" data-end=\"2391\">Choose <strong data-start=\"2347\" data-end=\"2356\">MySQL<\/strong> or any database engine you prefer.<\/p>\n<\/li>\n<li data-start=\"2392\" data-end=\"2455\">\n<p data-start=\"2395\" data-end=\"2455\">Choose <strong data-start=\"2402\" data-end=\"2421\">Standard create<\/strong> and fill in the necessary fields.<\/p>\n<\/li>\n<li data-start=\"2456\" data-end=\"2519\">\n<p data-start=\"2459\" data-end=\"2519\"><strong data-start=\"2459\" data-end=\"2473\">Important:<\/strong> Choose the <strong data-start=\"2485\" data-end=\"2497\">same VPC<\/strong> as your EC2 instance.<\/p>\n<\/li>\n<li data-start=\"2520\" data-end=\"2571\">\n<p data-start=\"2523\" data-end=\"2571\">Complete other settings and create the database.<\/p>\n<\/li>\n<\/ol>\n<h3 data-start=\"2573\" data-end=\"2630\">Step 3: Automatically Connect the EC2 Instance to RDS<\/h3>\n<ol data-start=\"2632\" data-end=\"3178\">\n<li data-start=\"2632\" data-end=\"2666\">\n<p data-start=\"2635\" data-end=\"2666\">Go back to the <strong data-start=\"2650\" data-end=\"2665\">EC2 Console<\/strong>.<\/p>\n<\/li>\n<li data-start=\"2667\" data-end=\"2695\">\n<p data-start=\"2670\" data-end=\"2695\">Select your EC2 instance.<\/p>\n<\/li>\n<li data-start=\"2696\" data-end=\"2759\">\n<p data-start=\"2699\" data-end=\"2759\">In the <strong data-start=\"2706\" data-end=\"2717\">Actions<\/strong> dropdown, choose <strong data-start=\"2735\" data-end=\"2758\">Connect to database<\/strong>.<\/p>\n<\/li>\n<li data-start=\"2760\" data-end=\"2802\">\n<p data-start=\"2763\" data-end=\"2802\">Select your RDS database from the list.<\/p>\n<\/li>\n<li data-start=\"2803\" data-end=\"3120\">\n<p data-start=\"2806\" data-end=\"2829\">AWS will automatically:<\/p>\n<ul data-start=\"2833\" data-end=\"3120\">\n<li data-start=\"2833\" data-end=\"3016\">\n<p data-start=\"2835\" data-end=\"2873\">Create and assign two security groups:<\/p>\n<ul data-start=\"2879\" data-end=\"3016\">\n<li data-start=\"2879\" data-end=\"2944\">\n<p data-start=\"2881\" data-end=\"2944\">One for the EC2 instance (outbound rule to RDS security group).<\/p>\n<\/li>\n<li data-start=\"2950\" data-end=\"3016\">\n<p data-start=\"2952\" data-end=\"3016\">One for the RDS database (inbound rule from EC2 security group).<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li data-start=\"3020\" data-end=\"3120\">\n<p data-start=\"3022\" data-end=\"3120\">Configure the security group rules to allow traffic on the database port (usually 3306 for MySQL).<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li data-start=\"3121\" data-end=\"3178\">\n<p data-start=\"3124\" data-end=\"3178\">You will see a confirmation once this setup completes.<\/p>\n<\/li>\n<\/ol>\n<h2 data-start=\"3185\" data-end=\"3252\">Option 2: Automatically Connect EC2 to RDS Using the RDS Console<\/h2>\n<p data-start=\"3254\" data-end=\"3335\">Alternatively, you can do the automatic connection starting from the RDS Console.<\/p>\n<ol data-start=\"3337\" data-end=\"3592\">\n<li data-start=\"3337\" data-end=\"3365\">\n<p data-start=\"3340\" data-end=\"3365\">Open the <strong data-start=\"3349\" data-end=\"3364\">RDS Console<\/strong>.<\/p>\n<\/li>\n<li data-start=\"3366\" data-end=\"3390\">\n<p data-start=\"3369\" data-end=\"3390\">Select your database.<\/p>\n<\/li>\n<li data-start=\"3391\" data-end=\"3425\">\n<p data-start=\"3394\" data-end=\"3425\">Click the <strong data-start=\"3404\" data-end=\"3415\">Actions<\/strong> dropdown.<\/p>\n<\/li>\n<li data-start=\"3426\" data-end=\"3460\">\n<p data-start=\"3429\" data-end=\"3460\">Choose <strong data-start=\"3436\" data-end=\"3459\">Connect to instance<\/strong>.<\/p>\n<\/li>\n<li data-start=\"3461\" data-end=\"3489\">\n<p data-start=\"3464\" data-end=\"3489\">Select your EC2 instance.<\/p>\n<\/li>\n<li data-start=\"3490\" data-end=\"3592\">\n<p data-start=\"3493\" data-end=\"3592\">AWS will automatically create the security groups and configure access rules just like in Option 1.<\/p>\n<\/li>\n<\/ol>\n<hr data-start=\"3594\" data-end=\"3597\" \/>\n<h2 data-start=\"3599\" data-end=\"3629\">How It Works Under the Hood<\/h2>\n<ul data-start=\"3631\" data-end=\"3983\">\n<li data-start=\"3631\" data-end=\"3820\">\n<p data-start=\"3633\" data-end=\"3669\"><strong data-start=\"3633\" data-end=\"3656\">Two security groups<\/strong> are created:<\/p>\n<ul data-start=\"3672\" data-end=\"3820\">\n<li data-start=\"3672\" data-end=\"3744\">\n<p data-start=\"3674\" data-end=\"3744\"><strong data-start=\"3674\" data-end=\"3696\">EC2 security group<\/strong> with outbound access to the RDS security group.<\/p>\n<\/li>\n<li data-start=\"3747\" data-end=\"3820\">\n<p data-start=\"3749\" data-end=\"3820\"><strong data-start=\"3749\" data-end=\"3771\">RDS security group<\/strong> with inbound access from the EC2 security group.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li data-start=\"3821\" data-end=\"3904\">\n<p data-start=\"3823\" data-end=\"3904\">This separation allows better security management and <strong data-start=\"3877\" data-end=\"3896\">least privilege<\/strong> access.<\/p>\n<\/li>\n<li data-start=\"3905\" data-end=\"3983\">\n<p data-start=\"3907\" data-end=\"3983\">EC2 can now securely communicate with RDS on the database port (e.g., 3306).<\/p>\n<\/li>\n<\/ul>\n<hr data-start=\"3985\" data-end=\"3988\" \/>\n<h2 data-start=\"3990\" data-end=\"4005\">What\u2019s Next?<\/h2>\n<ul data-start=\"4007\" data-end=\"4307\">\n<li data-start=\"4007\" data-end=\"4113\">\n<p data-start=\"4009\" data-end=\"4113\">Connect to your EC2 instance and test the database connection using your database client or application.<\/p>\n<\/li>\n<li data-start=\"4114\" data-end=\"4190\">\n<p data-start=\"4116\" data-end=\"4190\">Use the RDS database endpoint and credentials in your app\u2019s configuration.<\/p>\n<\/li>\n<li data-start=\"4191\" data-end=\"4307\">\n<p data-start=\"4193\" data-end=\"4307\">If you\u2019re running a WordPress site, for example, you would enter the RDS endpoint and DB credentials during setup.<\/p>\n<\/li>\n<\/ul>\n<hr data-start=\"4309\" data-end=\"4312\" \/>\n<h2 data-start=\"4314\" data-end=\"4324\">Summary<\/h2>\n<p data-start=\"4326\" data-end=\"4421\">Automatically connecting your EC2 instance to your RDS database via the AWS Management Console:<\/p>\n<ul data-start=\"4423\" data-end=\"4610\">\n<li data-start=\"4423\" data-end=\"4484\">\n<p data-start=\"4425\" data-end=\"4484\">Saves time and reduces mistakes configuring security groups<\/p>\n<\/li>\n<li data-start=\"4485\" data-end=\"4551\">\n<p data-start=\"4487\" data-end=\"4551\">Ensures secure, least-privileged communication between resources<\/p>\n<\/li>\n<li data-start=\"4552\" data-end=\"4610\">\n<p data-start=\"4554\" data-end=\"4610\">Makes your AWS architecture cleaner and easier to manage<\/p>\n<\/li>\n<\/ul>\n<p data-start=\"4612\" data-end=\"4664\">\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re building applications on AWS, you often need to connect your EC2 instance (which hosts your app or website) to an Amazon RDS database for data storage. AWS makes this connection easy with an automatic connection feature that configures the necessary security settings for you. In this tutorial, I\u2019ll walk you through how to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1067,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ci-cd-monitoring"],"_links":{"self":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts\/1061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1061"}],"version-history":[{"count":0,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts\/1061\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}