How do I integrate AWS App Runner with a database using environment variables?
In this guide, we will demonstrate how to integrate AWS App Runner with a database by using environment variables. We will use Pulumi to define and deploy the infrastructure. The AWS App Runner service will be configured to connect to an AWS RDS database, and the necessary connection details will be provided as environment variables.
We will create:
- An RDS MySQL database.
- An App Runner service that connects to the database using environment variables.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsNative from "@pulumi/aws-native";
// Create an RDS MySQL database
const dbName = "mydb";
const dbUsername = "admin";
const dbPassword = "password123";
const dbInstance = new aws.rds.Instance("mydb-instance", {
allocatedStorage: 20,
engine: "mysql",
engineVersion: "8.0",
instanceClass: "db.t3.micro",
name: dbName,
username: dbUsername,
password: dbPassword,
skipFinalSnapshot: true,
});
// Create an App Runner service
const appRunnerService = new awsNative.apprunner.Service("myAppRunnerService", {
serviceName: "my-app-runner-service",
sourceConfiguration: {
imageRepository: {
imageIdentifier: "public.ecr.aws/aws-containers/hello-app-runner:latest",
imageRepositoryType: "ECR_PUBLIC",
imageConfiguration: {
runtimeEnvironmentVariables: [
{ name: "DB_HOST", value: dbInstance.endpoint.apply(e => e) },
{ name: "DB_NAME", value: dbName },
{ name: "DB_USER", value: dbUsername },
{ name: "DB_PASS", value: dbPassword },
],
},
},
autoDeploymentsEnabled: true,
},
instanceConfiguration: {
cpu: "1024",
memory: "2048",
},
});
// Export the App Runner service URL
export const serviceUrl = appRunnerService.serviceUrl;
Key Points:
- We created an RDS MySQL database instance with the necessary configuration.
- An App Runner service was set up to use a public container image.
- Environment variables for database connection details were provided to the App Runner service.
- The endpoint of the RDS instance was dynamically retrieved and set as an environment variable.
Summary:
In this guide, we demonstrated how to integrate AWS App Runner with an RDS database using environment variables. By using Pulumi, we defined the necessary infrastructure and configured the App Runner service to connect to the database. This setup allows your application to access the database securely using the provided environment variables.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.