Skip to content

Commit 1471670

Browse files
committed
Add EC2 instance for discourse app
1 parent 241c7ec commit 1471670

File tree

9 files changed

+209
-0
lines changed

9 files changed

+209
-0
lines changed

terraform/cert.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data "aws_acm_certificate" "ruby_data_cert" {
2+
domain = "ruby-data.org"
3+
statuses = ["ISSUED"]
4+
}

terraform/discourse.tf

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
variable "app_discourse_count" {
2+
type = "map"
3+
default = {
4+
prod = 2
5+
test = 1
6+
}
7+
}
8+
9+
variable "app_discourse_instance_type" {
10+
type = "map"
11+
default = {
12+
prod = "t2.medium"
13+
test = "t2.micro"
14+
}
15+
}
16+
17+
resource "aws_instance" "app_discourse" {
18+
count = "${lookup(var.app_discourse_count, terraform.workspace)}"
19+
20+
ami = "${data.aws_ami.ubuntu_xenial.id}"
21+
instance_type = "${lookup(var.app_discourse_instance_type, terraform.workspace)}"
22+
23+
disable_api_termination = true
24+
key_name = "discourse"
25+
monitoring = true
26+
27+
vpc_security_group_ids = [
28+
"${aws_security_group.sg_app.id}"
29+
]
30+
31+
subnet_id = "${element(aws_subnet.subnet_main_public.*.id, count.index % length(data.aws_availability_zones.available.names))}"
32+
33+
associate_public_ip_address = true
34+
35+
user_data = "${data.template_file.discourse_user_data.rendered}"
36+
37+
tags {
38+
Name = "${format("app-discourse-${terraform.workspace}-%03d", count.index + 1)}"
39+
}
40+
}
41+
42+
data "template_file" "discourse_user_data" {
43+
template = "${file("templates/discourse_init.sh")}"
44+
45+
vars = {
46+
authorized_keys = "${data.template_file.discourse_authorized_keys.rendered}"
47+
sshd_config_content = "${data.template_file.sshd_config_content.rendered}"
48+
}
49+
}
50+
51+
data "template_file" "discourse_authorized_keys" {
52+
template = "${file(format("templates/discourse-%s_authorized_keys", terraform.workspace))}"
53+
}
54+
55+
data "template_file" "sshd_config_content" {
56+
template = "${file("templates/sshd_config")}"
57+
}

terraform/elb.tf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
resource "aws_elb" "elb_app_discourse" {
2+
name = "app-discourse-${terraform.workspace}"
3+
subnets = [
4+
"${aws_subnet.subnet_main_public.*.id}"
5+
]
6+
instances = [
7+
"${aws_instance.app_discourse.*.id}"
8+
]
9+
10+
listener {
11+
instance_port = 80
12+
instance_protocol = "http"
13+
lb_port = 80
14+
lb_protocol = "http"
15+
}
16+
17+
listener {
18+
instance_port = 80
19+
instance_protocol = "http"
20+
lb_port = 443
21+
lb_protocol = "https"
22+
ssl_certificate_id = "${data.aws_acm_certificate.ruby_data_cert.arn}"
23+
}
24+
25+
health_check {
26+
healthy_threshold = 2
27+
unhealthy_threshold = 2
28+
timeout = 10
29+
target = "HTTP:80/"
30+
interval = 30
31+
}
32+
33+
cross_zone_load_balancing = true
34+
idle_timeout = 400
35+
connection_draining = true
36+
connection_draining_timeout = 400
37+
38+
tags {
39+
Name = "elb-app-discource-${terraform.workspace}"
40+
}
41+
}

terraform/sg.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ resource "aws_security_group" "sg_app" {
3636
cidr_blocks = ["0.0.0.0/0"]
3737
}
3838

39+
ingress {
40+
description = "SSH from anywhere"
41+
from_port = 9022
42+
to_port = 9022
43+
protocol = "tcp"
44+
cidr_blocks = ["0.0.0.0/0"]
45+
}
46+
3947
egress {
4048
description = "Allow all outbound traffic"
4149
from_port = 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIq/OTgu6p2oF7UMlaiFjAJQkOICa2yAvFcHRu5qLRgd mrkn@mrkn-mbp15-late2016.local
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIq/OTgu6p2oF7UMlaiFjAJQkOICa2yAvFcHRu5qLRgd mrkn@mrkn-mbp15-late2016.local

terraform/templates/discourse_init.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /bin/bash
2+
3+
mitamae_version=1.5.1
4+
5+
USER_NAME=discourse
6+
USER_UID=900
7+
USER_GID=900
8+
USER_HOME=/home/$$USER_NAME
9+
10+
groupadd -g $$USER_GID $$USER_NAME
11+
useradd -u $$USER_UID -g $$USER_GID -G adm -m -s /bin/bash $$USER_NAME
12+
mkdir -p $$USER_HOME/.ssh
13+
chmod 700 $$USER_HOME/.ssh
14+
echo -n "${authorized_keys}" > $$USER_HOME/.ssh/authorized_keys
15+
chmod 600 $$USER_HOME/.ssh/authorized_keys
16+
chown -R $${USER_NAME}:$${USER_NAME} $$USER_HOME
17+
18+
cat <<SUDO > /etc/sudoers.d/$$USER_NAME
19+
Defaults:%infra !requiretty
20+
Defaults:%infra env_keep += SSH_AUTH_SOCK
21+
Defaults:%infra env_keep += "MITAMAE_ENVIRONMENT MITAMAE_ROLES MITAMAE_HOST MITAMAE_TEAM_NO"
22+
23+
%$$USER_NAME ALL=(ALL) NOPASSWD: ALL
24+
SUDO
25+
26+
apt-get update -y
27+
apt-get install -y curl ssh sudo
28+
29+
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig
30+
cat <<SSHD_CONFIG > /etc/ssh/sshd_config
31+
${sshd_config_content}
32+
SSHD_CONFIG
33+
34+
service ssh restart
35+
36+
mkdir -p /usr/local/sbin
37+
curl -o /usr/local/sbin/mitamae https://github.com/itamae-kitchen/mitamae/releases/download/v$${mitamae_version}/mitamae-x86_64-linux
38+
chmod 755 /usr/local/sbin/mitamae

terraform/templates/sshd_config

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Port 22
2+
Port 9022
3+
4+
Protocol 2
5+
6+
HostKey /etc/ssh/ssh_host_rsa_key
7+
HostKey /etc/ssh/ssh_host_dsa_key
8+
HostKey /etc/ssh/ssh_host_ecdsa_key
9+
HostKey /etc/ssh/ssh_host_ed25519_key
10+
11+
UsePrivilegeSeparation yes
12+
13+
KeyRegenerationInterval 3600
14+
ServerKeyBits 1024
15+
16+
SyslogFacility AUTH
17+
LogLevel INFO
18+
19+
LoginGraceTime 120
20+
PermitRootLogin prohibit-password
21+
StrictModes yes
22+
23+
RSAAuthentication yes
24+
PubkeyAuthentication yes
25+
AuthorizedKeysFile %h/.ssh/authorized_keys
26+
27+
IgnoreRhosts yes
28+
RhostsRSAAuthentication no
29+
HostbasedAuthentication no
30+
31+
PermitEmptyPasswords no
32+
ChallengeResponseAuthentication no
33+
PasswordAuthentication no
34+
35+
X11Forwarding no
36+
PrintMotd yes
37+
PrintLastLog yes
38+
TCPKeepAlive yes
39+
40+
AcceptEnv LANG LC_*
41+
42+
Subsystem sftp /usr/lib/openssh/sftp-server
43+
44+
UsePAM yes

terraform/ubuntu.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
data "aws_ami" "ubuntu_xenial" {
2+
most_recent = true
3+
4+
filter {
5+
name = "name"
6+
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
7+
}
8+
9+
filter {
10+
name = "virtualization-type"
11+
values = ["hvm"]
12+
}
13+
14+
owners = ["099720109477"] # Canonical
15+
}

0 commit comments

Comments
 (0)