-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul-server.tf
107 lines (90 loc) · 2.37 KB
/
consul-server.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
provider "aws" {
region = "eu-central-1"
}
resource "aws_instance" "consul-server" {
ami = "ami-0e872aee57663ae2d"
instance_type = "t2.micro"
# User data script to install and configure Consul on Ubuntu
user_data = <<-EOF
#!/bin/bash
sudo apt-get update -y
sudo apt-get install -y wget unzip
# Download and install Consul
CONSUL_VERSION="1.11.0"
wget https://releases.hashicorp.com/consul/1.11.0/consul_1.11.0_linux_amd64.zip
unzip consul_1.11.0_linux_amd64.zip
sudo mv consul /usr/local/bin/
sudo chmod +x /usr/local/bin/consul
# Create Consul configuration directory and files
sudo mkdir -p /etc/consul.d /var/consul
sudo tee /etc/consul.d/consul.hcl > /dev/null <<-EOC
data_dir = "/var/consul"
bind_addr = "$(curl http://169.254.169.254/latest/meta-data/local-ipv4)"
client_addr = "0.0.0.0"
server = true
bootstrap_expect = 1
EOC
# Create Consul service
sudo tee /etc/systemd/system/consul.service > /dev/null <<-EOS
[Unit]
Description=Consul Agent
Requires=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOS
# Enable and start the Consul service
sudo systemctl enable consul
sudo systemctl start consul
EOF
tags = {
Name = "ConsulServer"
}
vpc_security_group_ids = [aws_security_group.consul_sg.id]
}
resource "aws_security_group" "consul_sg" {
name = "consul-sg"
description = "Allow SSH, HTTP, and Consul traffic"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8500
to_port = 8500
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8301
to_port = 8302
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8301
to_port = 8302
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}