-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
113 lines (98 loc) · 2.75 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.33"
}
}
}
provider "aws" {
region = var.region
}
data "aws_iam_policy" "cloud_watch_agent" {
name = "CloudWatchAgentServerPolicy"
}
data "aws_iam_policy_document" "assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "role" {
name = "Minecraft"
managed_policy_arns = [data.aws_iam_policy.cloud_watch_agent.arn]
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
resource "aws_iam_instance_profile" "profile" {
name = "Minecraft"
role = aws_iam_role.role.name
}
data "aws_ami" "amazon_linux_2" {
owners = ["amazon"]
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-2.*"]
}
}
resource "aws_security_group" "minecraft" {
name = "Minecraft"
description = "Minecraft server traffic"
}
resource "aws_security_group_rule" "minecraft" {
type = "ingress"
from_port = 25565
to_port = 25565
protocol = "tcp"
security_group_id = aws_security_group.minecraft.id
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "ssh" {
count = var.ec2_instance_connect ? 1 : 0
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
security_group_id = aws_security_group.minecraft.id
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "egress" {
type = "egress"
from_port = 0
to_port = 0
protocol = "all"
security_group_id = aws_security_group.minecraft.id
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_instance" "minecraft" {
ami = data.aws_ami.amazon_linux_2.id
iam_instance_profile = aws_iam_instance_profile.profile.name
instance_type = var.instance_type
security_groups = [aws_security_group.minecraft.name]
tags = {
Name = "Minecraft"
}
user_data = templatefile(
"scripts/startup.sh",
{
agent_config = templatefile("scripts/cloudwatch_agent_config.json", { log_group_name = aws_cloudwatch_log_group.minecraft.name })
download_url = var.download_url,
service = file("scripts/minecraft.service")
}
)
}
resource "aws_cloudwatch_log_group" "minecraft" {
name = "minecraft"
retention_in_days = 1
}
resource "aws_cloudwatch_log_stream" "minecraft" {
name = aws_instance.minecraft.id
log_group_name = aws_cloudwatch_log_group.minecraft.name
}
resource "aws_eip" "minecraft" {
count = var.static_ip ? 1 : 0
instance = aws_instance.minecraft.id
}