1. Answers
  2. Using Consul with Amazon EC2

How do I use Consul with Amazon EC2?

In this guide, we will demonstrate how to integrate Consul with Amazon EC2 instances using Pulumi. We will create an EC2 instance and install Consul on it. Consul is a tool for service discovery and configuration, and it can be integrated with EC2 instances to manage services in a scalable and automated way.

Detailed Steps

  1. Create a VPC: We will create a Virtual Private Cloud (VPC) to host our EC2 instances.
  2. Create a Security Group: This will define the firewall rules for our EC2 instances.
  3. Create an EC2 Instance: We will launch an EC2 instance where Consul will be installed.
  4. Install Consul on the EC2 Instance: Using a user data script, we will install and configure Consul on the instance.

Key Points

  • We use Pulumi to define and manage our AWS infrastructure.
  • The EC2 instance will have a user data script that installs and configures Consul.
  • Security groups are used to control access to the EC2 instance.

Pulumi Program

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

// Create a VPC
const vpc = new aws.ec2.Vpc("consul-vpc", {
    cidrBlock: "10.0.0.0/16",
    tags: {
        Name: "consul-vpc",
    },
});

// Create a subnet
const subnet = new aws.ec2.Subnet("consul-subnet", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
    tags: {
        Name: "consul-subnet",
    },
});

// Create a security group
const securityGroup = new aws.ec2.SecurityGroup("consul-sg", {
    vpcId: vpc.id,
    description: "Allow HTTP and SSH",
    ingress: [
        { protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] },
        { protocol: "tcp", fromPort: 8500, toPort: 8500, cidrBlocks: ["0.0.0.0/0"] }, // Consul HTTP API
    ],
    egress: [
        { protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
    ],
    tags: {
        Name: "consul-sg",
    },
});

// Create an EC2 instance
const instance = new aws.ec2.Instance("consul-instance", {
    instanceType: "t2.micro",
    ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
    subnetId: subnet.id,
    securityGroups: [securityGroup.name],
    userData: `#!/bin/bash
    sudo yum update -y
    sudo yum install -y wget
    wget https://releases.hashicorp.com/consul/1.9.5/consul_1.9.5_linux_amd64.zip
    unzip consul_1.9.5_linux_amd64.zip
    sudo mv consul /usr/local/bin/
    consul agent -dev -client=0.0.0.0 -ui -bind=0.0.0.0`,
    tags: {
        Name: "consul-instance",
    },
});

// Export the public IP of the instance
export const publicIp = instance.publicIp;

// Export the URL of the Consul UI
export const consulUrl = pulumi.interpolate`http://${instance.publicIp}:8500`;

Summary

In this guide, we created a VPC, subnet, and security group using Pulumi. We then launched an EC2 instance and installed Consul on it using a user data script. The security group allowed SSH access and access to the Consul HTTP API. Finally, we exported the public IP of the instance and the URL of the Consul UI. This setup allows you to manage services using Consul on an Amazon EC2 instance.

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