forked from darrelldavis/terraform-aws-minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
231 lines (189 loc) · 5.54 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// This module creates a single EC2 instance for running a Minecraft server
// Default network
data "aws_vpc" "default" {
default = true
}
data "aws_subnet_ids" "default" {
vpc_id = local.vpc_id
}
data "aws_caller_identity" "aws" {}
locals {
vpc_id = length(var.vpc_id) > 0 ? var.vpc_id : data.aws_vpc.default.id
subnet_id = length(var.subnet_id) > 0 ? var.subnet_id : sort(data.aws_subnet_ids.default.ids)[0]
tf_tags = {
Terraform = true,
By = data.aws_caller_identity.aws.arn
}
}
// Keep labels, tags consistent
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
namespace = var.namespace
stage = var.environment
name = var.name
delimiter = "-"
label_order = ["environment", "stage", "name", "attributes"]
tags = merge(var.tags, local.tf_tags)
}
// Amazon Linux2 AMI - can switch this to default by editing the EC2 resource below
data "aws_ami" "amazon-linux-2" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
}
// Find latest Ubuntu AMI, use as default if no AMI specified
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
// S3 bucket for persisting minecraft
resource "random_string" "s3" {
length = 12
special = false
upper = false
}
#data "aws_s3_bucket" "selected" {
# bucket = local.bucket
#}
locals {
using_existing_bucket = signum(length(var.bucket_name)) == 1
bucket = length(var.bucket_name) > 0 ? var.bucket_name : "${module.label.id}-${random_string.s3.result}"
}
module "s3" {
source = "terraform-aws-modules/s3-bucket/aws"
create_bucket = local.using_existing_bucket ? false : true
bucket = local.bucket
acl = "private"
force_destroy = var.bucket_force_destroy
versioning = {
enabled = var.bucket_object_versioning
}
# S3 bucket-level Public Access Block configuration
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
tags = module.label.tags
}
// IAM role for S3 access
resource "aws_iam_role" "allow_s3" {
name = "${module.label.id}-allow-ec2-to-s3"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_instance_profile" "mc" {
name = "${module.label.id}-instance-profile"
role = aws_iam_role.allow_s3.name
}
resource "aws_iam_role_policy" "mc_allow_ec2_to_s3" {
name = "${module.label.id}-allow-ec2-to-s3"
role = aws_iam_role.allow_s3.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["arn:aws:s3:::${local.bucket}"]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": ["arn:aws:s3:::${local.bucket}/*"]
}
]
}
EOF
}
// Script to configure the server - this is where most of the magic occurs!
data "template_file" "user_data" {
template = file("${path.module}/user_data.sh")
vars = {
mc_root = var.mc_root
mc_bucket = local.bucket
mc_backup_freq = var.mc_backup_freq
mc_version = var.mc_version
mc_type = var.mc_type
java_mx_mem = var.java_mx_mem
java_ms_mem = var.java_ms_mem
}
}
// Security group for our instance - allows SSH and minecraft
module "ec2_security_group" {
source = "[email protected]:terraform-aws-modules/terraform-aws-security-group.git?ref=master"
name = "${var.name}-ec2"
description = "Allow SSH and TCP ${var.mc_port}"
vpc_id = local.vpc_id
ingress_cidr_blocks = [ var.allowed_cidrs ]
ingress_rules = [ "ssh-tcp"]
ingress_with_cidr_blocks = [
{
from_port = var.mc_port
to_port = var.mc_port
protocol = "tcp"
description = "Minecraft server"
cidr_blocks = var.allowed_cidrs
},
]
egress_rules = ["all-all"]
tags = module.label.tags
}
// Create EC2 ssh key pair
resource "tls_private_key" "ec2_ssh" {
count = length(var.key_name) > 0 ? 0 : 1
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "ec2_ssh" {
count = length(var.key_name) > 0 ? 0 : 1
key_name = "${var.name}-ec2-ssh-key"
public_key = tls_private_key.ec2_ssh[0].public_key_openssh
}
locals {
_ssh_key_name = length(var.key_name) > 0 ? var.key_name : aws_key_pair.ec2_ssh[0].key_name
}
// EC2 instance for the server - tune instance_type to fit your performance and budget requirements
module "ec2_minecraft" {
source = "git::https://github.com/terraform-aws-modules/terraform-aws-ec2-instance.git?ref=master"
name = "${var.name}-public"
# instance
key_name = local._ssh_key_name
ami = var.ami != "" ? var.ami : data.aws_ami.ubuntu.image_id
instance_type = var.instance_type
iam_instance_profile = aws_iam_instance_profile.mc.id
user_data = data.template_file.user_data.rendered
# network
subnet_id = local.subnet_id
vpc_security_group_ids = [ module.ec2_security_group.this_security_group_id ]
associate_public_ip_address = var.associate_public_ip_address
tags = module.label.tags
}