1. Answers
  2. Deploy RabbitMQ with Amazon ECS

How do I use RabbitMQ with Amazon ECS?

In this guide, we will deploy RabbitMQ on Amazon ECS using Pulumi. We will create an ECS cluster, define a task definition for RabbitMQ, and set up a service to run the RabbitMQ container. This setup will allow RabbitMQ to run in a scalable and managed environment provided by ECS.

Key Points:

  • Create an ECS cluster to host the RabbitMQ service.
  • Define an ECS task definition for the RabbitMQ container.
  • Set up an ECS service to manage the RabbitMQ task.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an ECS cluster
const cluster = new aws.ecs.Cluster("rabbitmq-cluster");

// Create an IAM role for the ECS task
const taskRole = new aws.iam.Role("taskRole", {
    assumeRolePolicy: {
        Version: "2012-10-17",
        Statement: [{
            Action: "sts:AssumeRole",
            Principal: {
                Service: "ecs-tasks.amazonaws.com",
            },
            Effect: "Allow",
            Sid: "",
        }],
    },
});

// Attach the necessary policies to the task role
const taskRolePolicy = new aws.iam.RolePolicyAttachment("taskRolePolicy", {
    role: taskRole.name,
    policyArn: aws.iam.ManagedPolicy.AmazonECSTaskExecutionRolePolicy,
});

// Define the ECS task definition for RabbitMQ
const taskDefinition = new aws.ecs.TaskDefinition("rabbitmq-task", {
    family: "rabbitmq",
    cpu: "256",
    memory: "512",
    networkMode: "awsvpc",
    requiresCompatibilities: ["FARGATE"],
    executionRoleArn: taskRole.arn,
    containerDefinitions: JSON.stringify([{
        name: "rabbitmq",
        image: "rabbitmq:3-management",
        essential: true,
        portMappings: [{
            containerPort: 5672,
            hostPort: 5672,
        }, {
            containerPort: 15672,
            hostPort: 15672,
        }],
        environment: [{
            name: "RABBITMQ_DEFAULT_USER",
            value: "admin",
        }, {
            name: "RABBITMQ_DEFAULT_PASS",
            value: "admin",
        }],
    }]),
});

// Create a security group for the service
const securityGroup = new aws.ec2.SecurityGroup("rabbitmq-sg", {
    description: "Allow RabbitMQ traffic",
    ingress: [
        { protocol: "tcp", fromPort: 5672, toPort: 5672, cidrBlocks: ["0.0.0.0/0"] },
        { protocol: "tcp", fromPort: 15672, toPort: 15672, cidrBlocks: ["0.0.0.0/0"] },
    ],
    egress: [
        { protocol: "tcp", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
    ],
});

// Create an ECS service to run the RabbitMQ task
const service = new aws.ecs.Service("rabbitmq-service", {
    cluster: cluster.arn,
    taskDefinition: taskDefinition.arn,
    desiredCount: 1,
    launchType: "FARGATE",
    networkConfiguration: {
        subnets: ["subnet-xxxxxxxx"], // Replace with your subnet IDs
        securityGroups: [securityGroup.id],
    },
});

// Export the service URL
export const serviceUrl = pulumi.interpolate`http://${service.id}.ecs.amazonaws.com`;

Summary

In this guide, we deployed RabbitMQ on Amazon ECS using Pulumi. We created an ECS cluster, defined a task definition for RabbitMQ, and set up a service to manage the RabbitMQ container. This setup ensures that RabbitMQ runs in a scalable and managed environment provided by ECS.

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