How do I deploy the docker.io/library/rabbitmq:latest docker image on AWS EC2 with TypeScript?
In this guide, we will use Pulumi with TypeScript to deploy the docker.io/library/rabbitmq:latest
Docker image on an AWS EC2 instance. This involves creating an EC2 instance, configuring it to run Docker, and pulling the RabbitMQ Docker image to run the RabbitMQ server.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create an EC2 key pair
const keyPair = new aws.ec2.KeyPair("rabbitmq-keypair", {
publicKey: "ssh-rsa YOUR_SSH_PUBLIC_KEY_HERE"
});
// Create a security group to allow HTTP and SSH access
const securityGroup = new aws.ec2.SecurityGroup("rabbitmq-sg", {
ingress: [
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, // SSH
{ protocol: "tcp", fromPort: 5672, toPort: 5672, cidrBlocks: ["0.0.0.0/0"] }, // RabbitMQ
{ protocol: "tcp", fromPort: 15672, toPort: 15672, cidrBlocks: ["0.0.0.0/0"] } // RabbitMQ Management
]
});
// Define the EC2 instance
const server = new aws.ec2.Instance("rabbitmq-server", {
instanceType: "t2.micro",
ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
keyName: keyPair.keyName,
securityGroups: [securityGroup.name],
userData: `#!/bin/bash
yum update -y
amazon-linux-extras install docker -y
service docker start
usermod -a -G docker ec2-user
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:latest
`
});
// Export the public IP of the instance
export const publicIp = server.publicIp;
export const publicDns = server.publicDns;
Key Points
- Key Pair Creation: We create an EC2 key pair to allow SSH access to the instance.
- Security Group Configuration: A security group is created to allow SSH access and RabbitMQ ports (5672 for AMQP protocol and 15672 for the management console).
- EC2 Instance Configuration: An EC2 instance is defined using the Amazon Linux 2 AMI, and a user data script is provided to install Docker and run the RabbitMQ Docker container.
- Public IP Export: The public IP and DNS of the EC2 instance are exported for easy access.
Summary
We set up an AWS EC2 instance using Pulumi with TypeScript, configured it to run Docker, and deployed the RabbitMQ Docker image. This setup allows RabbitMQ to be accessible over the internet on the specified ports.
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.