Integrate Amazon RDS with ASP.NET Core on Elastic Beanstalk¶
This recipe connects an ASP.NET Core application on Elastic Beanstalk to Amazon RDS using Entity Framework Core. It follows the recommended pattern of keeping the database lifecycle separate from the Elastic Beanstalk environment lifecycle.
Prerequisites¶
- Running .NET Elastic Beanstalk environment.
- Existing Amazon RDS instance reachable from the environment VPC.
- Security group rules allowing database traffic.
- EF Core provider package for your selected engine.
What You'll Build¶
You will configure:
- Environment properties for database connectivity.
- An EF Core
DbContextusing a connection string from environment properties. - A simple database health endpoint.
Steps¶
- Set database connection properties on the environment.
eb setenv DB_HOST="mydb.xxxxx.ap-northeast-2.rds.amazonaws.com" DB_PORT="5432" DB_NAME="guideapi" DB_USER="guideapi" DB_PASSWORD="<db-password>"
- Add EF Core packages.
dotnet add GuideApi.csproj package Microsoft.EntityFrameworkCore
dotnet add GuideApi.csproj package Npgsql.EntityFrameworkCore.PostgreSQL
- Register the
DbContext.
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(
$"Host={builder.Configuration["DB_HOST"]};" +
$"Port={builder.Configuration["DB_PORT"]};" +
$"Database={builder.Configuration["DB_NAME"]};" +
$"Username={builder.Configuration["DB_USER"]};" +
$"Password={builder.Configuration["DB_PASSWORD"]}"));
- Add a connectivity check endpoint.
app.MapGet("/db-check", async (AppDbContext dbContext) =>
{
var canConnect = await dbContext.Database.CanConnectAsync();
return Results.Ok(new { database = canConnect ? "reachable" : "unreachable" });
});
- Deploy and validate connectivity.
flowchart LR
A[Elastic Beanstalk EC2 Instance] --> B[VPC Network Path]
B --> C[Amazon RDS Endpoint]
D[Environment Properties] --> A
E[Security Group Rule] --> C Verification¶
Use these checks after deployment:
Expected outcomes:
- Environment variables are present.
- Security groups and subnets allow database connectivity.
/db-checkreturns a success payload.- Application logs do not expose the raw password.