# Accessing private RDS using an EC2 instance as a bastion host

ကျွန်တော် ဒီနေ့ Sharing လုပ်ပေးချင်တဲ့ အကြောင်းအရာကတော့ private subnets သုံးထားတဲ့ RDS Instance တစ်လုံးကို public subnets သုံးထားတဲ့ bastion host ကနေ ဘယ်လို access ရအောင်လုပ်မလဲ ဆိုတာ ကို share ချင်ပါတယ်။

ပထမဆုံး အနေနဲ့ Bastion host ဆိုတာဘယ်လိုမျိုး လဲဆိုတာကို မသိသေးတဲ့ ဆရာကြီးတွေကို အနည်းငယ် ရှင်းပြချင်ပါတယ်။ ကျွန်တော့ အမြင်ကတော့ bastion host ဆိုတာ ရောင်းသူနဲ့ ဝယ်သူကြား အလယ်မှာနေတဲ့ ပွဲစားတစ်ဦးလို့ မြင်ပါတယ်။ ရောင်းတဲ့ သူကလည်း သူ့ရောင်းချင်တဲ့ အရာကို ဝယ်သူဝယ်လာအောင် စကားပြောရတယ်ပေါ့ အဲ့မှာ ဝယ်လက်နဲ့ရောင်းတဲ့သူကြား ကြိုက်ရောင်းကြိုက်ဝယ်မဖြစ်တော့တဲ့အခါ ကြားကနေ ပွဲစားကဝင်လာပီး နှစ်ဦးနှစ်ဖက် အဆင်ပြေအောင် ချိတ်ဆက်ပေးလိုက်တယ်ပေါ့ အဲ့မှာ ရောင်းသူကော ဝယ်သူကော အဆင်ပြေသွားတယ်ပေါ့ နော်။ သူက အလယ်မှာနေပီး လုပ်စရာရှိတဲ့ task တွေကို လုပ်သွားတာမျိုးပါ။

Real world မှာဆိုရင် ကျွန်တော်တို့ ရုံးမှာ project တစ်ခုလုပ်ပီဆိုရင် ကို့ team members တွေအပြင် အခြား team က members တွေ ပါ ဝိုင်းပီး collaboration လုပ်ရတဲ့ အချိန်မျိုးမှာ server တွေကို တိုက်ရိုက် access မပေးပဲအလယ်ကနေ bastion host တစ်လုံးခံပီး bastion ကမှတစ်ဆင့် သက်ဆိုင်ရာ server တွေကို သွားပီး config လုပ်ခိုင်းတယ်ပေါ့နော်။ အဲ့တော့ အနောက်က server တွေအတွက် single point of access ဖြစ်တဲ့ bastion host ကနေလာတဲ့ ssh traffic တစ်ခုကိုပဲ လက်ခံရပါတော့မယ်။ Security point of view ကနေ ကြည့်ရင်လည်း attacking surface တစ်ခုကို လျော့ချလိုက်နိုင်တယ်။အဲ့လောက်ဆိုရင် bastion host ဆိုတာဘာလဲဆိုတာ ရှင်းမယ်လို့ မျှော်လင့်ပါတယ်။

အဲ့ပုံစံ အတိုင်းပဲ ဒီ lab မှာဆိုရင် Private subnets ထဲရောက်နေတဲ့ AWS resources တွေကို Internet ကနေ တိုက်ရိုက်သုံးချင်တဲ့ အခါမျိုးမှာဆိုရင် သုံးလို့မရပါဘူး bastion host ခံပီးမှ သုံးလို့ရပါမယ်။ အရေးကြီးတာက private subnet ထဲက resource ကော public subnet ထဲက resource ကပါ within same vpc ဖြစ်နေဖို့ လိုပါတယ်။ ဒီ lab ထဲမှာဆိုရင် RDS မှာသုံးထားတဲ့ security group ရဲ့ inbound rule မှာ MYSQL access အတွက် bastion host မှာသုံးထားတဲ့ security group ကို allow လုပ်ထားပါတယ်။ ဆိုလိုတာက ဒီ RDS ကို လှမ်းသုံးချင်ရင် အဲ့ security group နဲ့ attach တွဲထားထဲ bastion host ကနေပဲ ရမယ်လို့ ပြောချင်တာပါ။

ကဲဒါဆိုရင် demo လေးလုပ်ကြည့်ရအောင်ပါ။

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511300532/32240a74-e185-4756-bfc7-4c453f7d9c5f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511323301/4394c4fd-c849-42f0-8807-7fb8ceb6515e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511345726/3ca01574-63a7-4a2e-8775-92db82d46de4.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511360892/fe65557c-54f2-4dce-8178-8b2ed88a429a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511375470/cc3b5e7a-5f4b-47b9-bc1a-30176e8d5233.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511393612/2bdea934-7293-4848-b81a-d55f4da337d9.png align="center")

ဒီဟာကတော့ bastion host ရဲ့ security group ကို ချိတ်ဆက်ပုံကိုပြထားတာပါ။

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511420069/6012bc35-82da-4f07-b09f-4d2a1148efc6.png align="center")

ဒီဟာကတော့ RDS ဆောက်တဲ့အပိုင်းပါ။ အဲ့နေရာမှာကျွန်တော်ကတော့ RDS ကို တန်း မလုပ်ပဲ RDS မှာသုံးရမဲ့ subnet group ကို အရင်လုပ်ပါတယ်။ မဆောက်ပဲ တန်းပီး RDS ကို ဆောက်လိုက်တာနဲ့ system ကနေ auto တန်းဆောက်ပေးပါလိမ့်မယ်။ အဲ့အခါကျရင် private subnet ကော public subnet ကောရောပါလာတာမျိုးကြုံရပါတယ်။ ကျွန်တော် ကတော့ သီးသန့် private subnet group တစ်ခုဆောက်ပီး အဲ့တာကိုပဲယူသုံးလိုက်ပါတယ်။ RDS ကိုလည်း public access ပိတ်ထားပါတယ်။

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511503889/f58d7d9a-9c07-452a-b3d3-59212f07168e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511928826/44ccddc3-e60c-4c31-8839-bcbb50ba91a4.png align="center")

DB engine ကိုတော့ MySQL ကိုသုံးထားပါတယ်။

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703511969547/61cace8e-5417-4fff-bb1b-8619a128aae1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512027843/3301b093-0364-4e4a-87ae-448edd5eb3a8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512039770/22bd281b-7d2d-426b-af97-730517547828.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512074803/80294aa5-05df-44c2-a6ab-2982a90ab66c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512099571/e777fd40-79b0-4035-9158-e0bfc0855613.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512344719/412df126-4a61-4b31-a883-62dd4090f064.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512371618/9667b64d-82b8-4820-988b-cce19f8af305.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512391794/8e6c4ce6-629d-48d1-a81c-508ec498816d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512415591/b6a45b38-51c5-44cd-badf-80432d52c75e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512511267/75cb8b56-a551-4a8e-8dd1-7b7b83bd87ce.png align="center")

Public access ကိုပိတ်ထားလိုက်ပါပီ။ DB endpoint ရသွားရင်တောင် public access ပိတ်ထားတဲ့အတွက် လှမ်းဝင်လို့မရတော့ပါဘူး။

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703513676627/5241247b-eaf6-4716-994d-b95621908279.png align="center")

DB connector အနေနဲ့ MySQL Workbench ကိုသုံးထားပါတယ်။

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512434563/3d2113d5-e923-4c55-b2b5-84db03860ce8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512538074/bcb719c0-bf20-46bc-b0b6-93396e0f3a7a.png align="center")

MySQL workbench ဖွင့်ပီးပီဆိုရင် Database tab အောက်က Connect to database ဆိုတာကို နှိပ်လိုက်ပါ။ Stored Connection မှာ Local Instance ကိုမရွေးပဲ တစ်ခြားတစ်ခုကိုရွေးပါ။ Connection method မှာ Standard TCP/IP over SSH ဆိုတာကိုရွေးလိုက်ပါ။ SSH Hostname မှာ bastion host အနေနဲ့ လုပ်ထားတဲ့ EC2 Instance ရဲ့ public ip ကိုထည့်ပါ။ SSH Username က ec2-user ထည့်ပါ ။ အကယ်လို့ ကို့ Instance ရဲ့ AMI က ubuntu သုံးထားရင်တော့ ubuntu ပေါ့။ ပီးရင်အရေးကြီးတာက SSH Key File ပါ အဲ့နေရာမှာ bastion host ရဲ့ key file ကိုထည့်ရပါမယ်။ MySQL Hostname မှာ RDS ရဲ့ Endpoint ကိုထည့်ရပါမယ်။ Username က DB ဆောက်တုံးက ပေးထားတာကိုထည့်ပါ။ Password လည်းထိုနည်းလည်းကောင်းပါပဲ။

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703512611864/c1a5fd1c-153e-4af8-a61c-26c0b0ff1328.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703513487898/514ddfe3-3584-4e32-b80b-a01305351d40.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703513510304/48b8c345-420f-4ef3-bab7-bb4419aa3551.png align="center")

DB ထဲရောက်သွားပီဆိုတော့ စမ်းပီး new schema တစ်ခုဆောက်ကြည့်ပါမယ်။

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703513546259/8b8bb3e9-8c71-4d32-a53e-cebdb21193da.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703513615700/8f5438a0-2695-4af7-9bd7-85df83d022b7.png align="center")

ကဲ ဒီနေ့ lab လေးကို ဒီနားမှာပဲ အဆုံးသတ်ပါရစေ။ တကဲ့ real world မှာ လည်းကြုံခဲ့ရတဲ့ ကျွန်တော်အတွေ့အကြုံ ကို ပြန်လည် sharing လုပ်ချင်ပါ။ ဒီ lab လေးနဲ့ပတ်သတ်ပီး ထပ်သိချင်တာမျိုး ရှိရင် အချိန်မရွေးမေးမြန်း ဝေဖန် အကြုံပြုနိုင်ပါတယ်။ အဆုံးထိဖတ်ရှုပေးတဲ့ အတွက် ကျေးဇူးပါ။ နောက်ထပ်လည်း real world use cases တွေကို ထပ်ပီး ဝေမျှအုံးမှာဖြစ်တဲ့အတွက် Stay tune ပါ ဆရာကြီးတို့။
