1. Answers
  2. Deploy RabbitMQ with AWS Fargate

How do I use RabbitMQ with AWS Fargate?

Overview

In this guide, we’ll walk through the steps to deploy RabbitMQ using AWS Fargate. AWS Fargate lets you run containers without managing the underlying physical infrastructure, and RabbitMQ is a popular message broker. We’ll define our infrastructure to create a RabbitMQ service managed by Fargate with high availability.

Key components we’ll be deploying:

  1. IAM Roles: Necessary permissions for ECS and Fargate tasks.
  2. VPC and Subnets: Networking configuration to host our Fargate service.
  3. Security Groups: Controls access to the RabbitMQ instance.
  4. ECS Cluster: Central place to manage our Fargate tasks.
  5. ECS Task Definition: Describe the Docker container configuration.
  6. ECS Service: Ensures the RabbitMQ task runs continuously.

Code

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Networking components
const main = new aws.ec2.Vpc("main", {cidrBlock: "10.0.0.0/16"});
const subnet1 = new aws.ec2.Subnet("subnet1", {
    vpcId: main.id,
    cidrBlock: "10.0.1.0/24",
    availabilityZone: "us-west-2a",
});
const subnet2 = new aws.ec2.Subnet("subnet2", {
    vpcId: main.id,
    cidrBlock: "10.0.2.0/24",
    availabilityZone: "us-west-2b",
});
// Security Group for RabbitMQ
const rabbitmqSg = new aws.ec2.SecurityGroup("rabbitmq_sg", {
    vpcId: main.id,
    ingress: [{
        fromPort: 5672,
        toPort: 5672,
        protocol: "tcp",
        cidrBlocks: ["0.0.0.0/0"],
    }],
    egress: [{
        fromPort: 0,
        toPort: 0,
        protocol: "-1",
        cidrBlocks: ["0.0.0.0/0"],
    }],
});
// ECS Cluster
const mainCluster = new aws.ecs.Cluster("main", {name: "rabbitmq-cluster"});
// IAM role for ECS Task Execution
const ecsTaskExecutionRole = new aws.iam.Role("ecs_task_execution_role", {
    name: "ecsTaskExecutionRole",
    assumeRolePolicy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Action: "sts:AssumeRole",
            Effect: "Allow",
            Principal: {
                Service: "ecs-tasks.amazonaws.com",
            },
        }],
    }),
});
const ecsTaskExecutionRolePolicy = new aws.iam.RolePolicyAttachment("ecs_task_execution_role_policy", {
    role: ecsTaskExecutionRole.name,
    policyArn: "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
});
// ECS Task Definition
const rabbitmq = new aws.ecs.TaskDefinition("rabbitmq", {
    family: "rabbitmq-task",
    networkMode: "awsvpc",
    requiresCompatibilities: ["FARGATE"],
    cpu: "256",
    memory: "512",
    executionRoleArn: ecsTaskExecutionRole.arn,
    containerDefinitions: JSON.stringify([{
        name: "rabbitmq",
        image: "rabbitmq:3-management",
        essential: true,
        portMappings: [{
            containerPort: 5672,
            hostPort: 5672,
        }],
    }]),
});
// ECS Service
const rabbitmqService = new aws.ecs.Service("rabbitmq", {
    name: "rabbitmq-service",
    cluster: mainCluster.id,
    taskDefinition: rabbitmq.arn,
    desiredCount: 1,
    launchType: "FARGATE",
    networkConfiguration: {
        subnets: [
            subnet1.id,
            subnet2.id,
        ],
        securityGroups: [rabbitmqSg.id],
        assignPublicIp: true,
    },
});
export const vpcId = main.id;
export const clusterName = mainCluster.name;
export const taskDefinition = rabbitmq.arn;
export const serviceName = rabbitmqService.name;

Key Points

  • IAM Roles: Managed permissions for ECS Task execution.
  • VPC and Subnets: Setup of networking for the RabbitMQ service.
  • Security Groups: Control traffic access to RabbitMQ.
  • ECS Cluster and Services: Management of the RabbitMQ deployment with high availability.

Summary

In this guide, we implemented a RabbitMQ service on AWS Fargate. We covered setting up IAM roles, VPC, security groups, ECS cluster, task definitions, and ECS service. This deployment ensures RabbitMQ runs in a scalable, highly available, and secure environment managed by Fargate.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up