How to Connect AWS RDS Postgres to Fargate Service?
Introduction
In this guide, we will set up an AWS RDS PostgreSQL instance and connect it to an AWS Fargate service using Pulumi. This involves creating the RDS instance, setting up the necessary networking components, and configuring the Fargate service to connect to the database.
Step-by-Step Explanation
Step 1: Set Up Networking
- Create a VPC: A Virtual Private Cloud (VPC) is needed to host both the RDS instance and the Fargate service.
- Create Subnets: Create public and private subnets within the VPC.
- Create Security Groups: Define security groups to control the inbound and outbound traffic to the RDS instance and the Fargate service.
Step 2: Create the RDS PostgreSQL Instance
- Define the RDS Instance: Use the
aws.rds.Instance
resource to create the PostgreSQL database. - Configure Database Parameters: Set the necessary parameters such as engine type, instance class, allocated storage, and credentials.
- Associate the RDS Instance with the VPC: Ensure the RDS instance is associated with the appropriate subnets and security groups.
Step 3: Set Up the Fargate Service
- Create an ECS Cluster: Use the
aws.ecs.Cluster
resource to create an ECS cluster. - Define Task Definition: Use the
aws.ecs.TaskDefinition
resource to define the task that will run on Fargate. This includes specifying the container image, environment variables, and other configurations. - Create the Fargate Service: Use the
aws.ecs.Service
resource to create the Fargate service and associate it with the ECS cluster. - Configure Networking: Ensure the Fargate service is associated with the appropriate subnets and security groups.
Step 4: Connect Fargate to RDS
- Set Environment Variables: Configure the Fargate task to include environment variables for the database connection (e.g., database endpoint, username, password).
- Update Security Groups: Ensure the security groups allow traffic between the Fargate service and the RDS instance.
Conclusion
By following these steps, you will have a fully functional AWS RDS PostgreSQL instance connected to an AWS Fargate service. This setup ensures that your application running on Fargate can securely communicate with the PostgreSQL database hosted on RDS.
Full Code Example
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create a VPC
const vpc = new aws.ec2.Vpc("my-vpc", {
cidrBlock: "10.0.0.0/16",
enableDnsHostnames: true,
enableDnsSupport: true,
});
// Create Subnets
const publicSubnet = new aws.ec2.Subnet("public-subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
mapPublicIpOnLaunch: true,
});
const privateSubnet = new aws.ec2.Subnet("private-subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.2.0/24",
});
// Create Security Groups
const rdsSecurityGroup = new aws.ec2.SecurityGroup("rds-sg", {
vpcId: vpc.id,
ingress: [{
protocol: "tcp",
fromPort: 5432,
toPort: 5432,
cidrBlocks: ["0.0.0.0/0"],
}],
egress: [{
protocol: "-1",
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
}],
});
const fargateSecurityGroup = new aws.ec2.SecurityGroup("fargate-sg", {
vpcId: vpc.id,
ingress: [{
protocol: "tcp",
fromPort: 80,
toPort: 80,
cidrBlocks: ["0.0.0.0/0"],
}],
egress: [{
protocol: "-1",
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
}],
});
// Create the RDS PostgreSQL Instance
const rds = new aws.rds.Instance("my-rds", {
engine: "postgres",
instanceClass: "db.t3.micro",
allocatedStorage: 20,
name: "mydatabase",
username: "admin",
password: "password123",
vpcSecurityGroupIds: [rdsSecurityGroup.id],
dbSubnetGroupName: new aws.rds.SubnetGroup("rds-subnet-group", {
subnetIds: [privateSubnet.id],
}).name,
skipFinalSnapshot: true,
});
// Create an ECS Cluster
const cluster = new aws.ecs.Cluster("my-cluster");
// Define Task Definition
const taskDefinition = new aws.ecs.TaskDefinition("my-task", {
family: "my-task-family",
networkMode: "awsvpc",
requiresCompatibilities: ["FARGATE"],
cpu: "256",
memory: "512",
containerDefinitions: JSON.stringify([{
name: "my-container",
image: "nginx",
portMappings: [{
containerPort: 80,
hostPort: 80,
protocol: "tcp",
}],
environment: [
{ name: "DB_HOST", value: rds.endpoint },
{ name: "DB_USER", value: "admin" },
{ name: "DB_PASS", value: "password123" },
],
}]),
});
// Create the Fargate Service
const service = new aws.ecs.Service("my-service", {
cluster: cluster.id,
taskDefinition: taskDefinition.arn,
desiredCount: 1,
launchType: "FARGATE",
networkConfiguration: {
subnets: [publicSubnet.id],
securityGroups: [fargateSecurityGroup.id],
},
});
export const vpcId = vpc.id;
export const rdsEndpoint = rds.endpoint;
export const fargateServiceArn = service.id;
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.