การเข้าถึงฐานข้อมูลใน VPC
เรียนรู้แนวทางปฏิบัติที่ดีในการเชื่อมต่อฟังก์ชัน Lambda กับฐานข้อมูลเชิงสัมพันธ์ เช่น RDS และแหล่งจัดเก็บข้อมูลอื่น ๆ ที่อยู่ภายในเครือข่าย VPC ส่วนตัว
การเข้าถึงฐานข้อมูลใน VPC เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Connect Lambda to Private DBs
Welcome! In this lesson, you'll learn how to securely connect your Lambda functions to databases that reside within your private Amazon Virtual Private Cloud (VPC).
This is crucial for serverless applications that need to interact with sensitive or internal data stores like Amazon RDS or other databases that aren't publicly accessible.
Lambda & Your Private Database
You've already configured your Lambda function to operate within a VPC (from the previous lesson). Now, we'll focus on how that Lambda can reach a database that's also inside the same VPC.
This setup ensures secure, internal communication, keeping your database isolated from the public internet.
Security Groups: The Gatekeepers
Security Groups are fundamental here. Think of them as virtual firewalls for your instances and resources within a VPC.
- They control both inbound (incoming) and outbound (outgoing) traffic.
- Both your Lambda function and your database will have associated security groups.
- These groups must be configured to explicitly allow communication between them.
Database Inbound Rules
Your database's security group needs an inbound rule to allow connections from your Lambda function.
- Type: Custom TCP
- Port Range: The specific port your database listens on (e.g.,
3306for MySQL,5432for PostgreSQL). - Source: Crucially, this should be the Security Group ID of your Lambda function.
This ensures only your Lambda can initiate connections to the database.
Lambda Outbound Rules
Conversely, your Lambda function's security group needs an outbound rule to allow it to send traffic to your database.
- Type: Custom TCP
- Port Range: Again, the database's specific port (e.g.,
3306). - Destination: This should be the Security Group ID of your database.
This allows your Lambda to successfully reach out and connect to the database.
Subnets & Network Reachability
Beyond security groups, ensure your Lambda function is deployed into subnets that have proper network routing access to the subnets where your database instances reside.
- Typically, both Lambda and the database will be placed in private subnets within the same VPC.
- AWS handles the internal routing within the VPC, but proper subnet association is key for reachability.
Connecting from Lambda Code
Inside your Lambda function's code, you'll use standard database drivers and connection strings, just like any other application.
You'll need these key details:
- Database endpoint: (e.g.,
your-db.xxxx.rds.amazonaws.com) - Port: (e.g.,
3306) - Database name
- Username
- Password
Java DB Connection Example
Here's a conceptual Java snippet demonstrating how a Lambda might attempt to connect to a database. In a real application, you would handle credentials securely.
This example shows the basic structure; it won't run successfully without a live database and environment variables set.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
String dbUrl = System.getenv("DB_URL");
String dbUser = System.getenv("DB_USER");
String dbPass = System.getenv("DB_PASS");
if (dbUrl == null || dbUser == null || dbPass == null) {
System.out.println("Error: DB environment variables not set.");
return;
}
try {
System.out.println("Attempting to connect to database...");
Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
System.out.println("Connection successful!");
conn.close();
} catch (SQLException se) {
System.err.println("Database connection error: " + se.getMessage());
} catch (Exception e) {
System.err.println("An unexpected error occurred: " + e.getMessage());
}
}
}Securely Manage Credentials
Never hardcode database credentials directly in your Lambda code! This is a major security risk.
Best practices for managing sensitive information:
- AWS Secrets Manager: The recommended way to store, retrieve, and rotate database credentials securely.
- Environment Variables: Suitable for non-sensitive configuration, but not for passwords or API keys.
Always prioritize security for sensitive data.
Check Your Understanding
Imagine your Lambda function (associated with Security Group SG-A) needs to connect to an RDS database (associated with Security Group SG-B) on port 3306.
Which inbound rule must be configured on SG-B (the database's security group) to allow this connection?
Recap: Database Access in VPC
Great job! You've learned the essentials of connecting your Lambda functions to private databases within your VPC.
- Both your Lambda and database must be in the same VPC.
- Security groups are vital for controlling traffic flow between them.
- Configure inbound rules on the database's SG and outbound rules on Lambda's SG.
- Always manage database credentials securely, ideally using AWS Secrets Manager.
This knowledge is key to building secure and robust serverless applications!
คำถามที่พบบ่อย
บทเรียน “การเข้าถึงฐานข้อมูลใน VPC” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเข้าถึงฐานข้อมูลใน VPC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเข้าถึงฐานข้อมูลใน VPC”
เรียนรู้แนวทางปฏิบัติที่ดีในการเชื่อมต่อฟังก์ชัน Lambda กับฐานข้อมูลเชิงสัมพันธ์ เช่น RDS และแหล่งจัดเก็บข้อมูลอื่น ๆ ที่อยู่ภายในเครือข่าย VPC ส่วนตัว คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การเข้าถึงฐานข้อมูลใน VPC” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม
ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- Lambda ใน VPC สำหรับทรัพยากรส่วนตัว
- การเข้าถึงฐานข้อมูลใน VPC
- แนวทางปฏิบัติที่ดีด้านความปลอดภัยเครือข่าย
- เกตเวย์ NAT และการเข้าถึงอินเทอร์เน็ตจาก VPC