summaryrefslogtreecommitdiff
path: root/org-buetow-ecs/fluxpostgreservice.tf
blob: b49bca952fd4227af4fdbc75959f5d50c1539378 (plain)
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
resource "aws_lb" "fluxpostgres_nlb" {
  name               = "fluxpostgres-nlb"
  internal           = true
  load_balancer_type = "network"
  ip_address_type    = "dualstack"
  security_groups = [
    aws_security_group.fluxpostgres.id,
  ]
  subnets = [
    data.terraform_remote_state.base.outputs.public_subnet_a_id,
    data.terraform_remote_state.base.outputs.public_subnet_b_id,
    data.terraform_remote_state.base.outputs.public_subnet_c_id,
  ]
}

resource "aws_lb_listener" "fluxpostgres_tcp" {
  load_balancer_arn = aws_lb.fluxpostgres_nlb.arn
  protocol          = "TCP"
  port              = 5432

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.fluxpostgres_tcp.arn
  }
}

resource "aws_lb_target_group" "fluxpostgres_tcp" {
  name        = "fluxpostgres-tcp"
  port        = 5432
  protocol    = "TCP"
  vpc_id      = data.terraform_remote_state.base.outputs.vpc_id
  target_type = "ip"
}

resource "aws_route53_record" "a_record_fluxpostgres" {
  zone_id = data.terraform_remote_state.base.outputs.buetow_internal_zone_id
  name    = "fluxpostgres.buetow.internal."
  type    = "A"

  alias {
    name                   = aws_lb.fluxpostgres_nlb.dns_name
    zone_id                = aws_lb.fluxpostgres_nlb.zone_id
    evaluate_target_health = true
  }
}

resource "aws_route53_record" "aaaa_record_fluxpostgres" {
  zone_id = data.terraform_remote_state.base.outputs.buetow_internal_zone_id
  name    = "fluxpostgres.buetow.internal."
  type    = "AAAA"

  alias {
    name                   = aws_lb.fluxpostgres_nlb.dns_name
    zone_id                = aws_lb.fluxpostgres_nlb.zone_id
    evaluate_target_health = true
  }
}

resource "aws_ecs_task_definition" "fluxpostgres" {
  family                   = "fluxpostgres"
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = "256"
  memory                   = "512"
  execution_role_arn       = aws_iam_role.ecs_execution_role.arn

  volume {
    name = "fluxpostgres-efs-volume"
    efs_volume_configuration {
      file_system_id = data.terraform_remote_state.base.outputs.self_hosted_services_efs_id
      root_directory = "/ecs/fluxpostgres/"
    }
  }

  container_definitions = jsonencode([{
    name  = "fluxpostgres",
    image = "postgres:15",
    portMappings = [
      {
        containerPort = 5432,
        hostPort      = 5432,
        protocol      = "tcp"
      }
    ],
    environment = [
      {
        name  = "POSTGRES_USER",
        value = "miniflux"
      },
      {
        name  = "POSTGRES_PASSWORD",
        value = "ONLYFORTESTING"
      }
    ],
    mountPoints = [
      {
        sourceVolume  = "fluxpostgres-efs-volume"
        containerPath = "/var/lib/postgresql/data"
        readOnly      = false
      }
    ],
    "logConfiguration" : {
      "logDriver" : "awslogs",
      "options" : {
        "awslogs-group" : "/ecs/containers",
        "awslogs-region" : "eu-central-1",
        "awslogs-stream-prefix" : "fluxpostgres"
      }
    }
  }])
}

resource "aws_security_group" "fluxpostgres" {
  name        = "allow-fluxpostgres"
  description = "Allow traffic on fluxpostgres ports"
  vpc_id      = data.terraform_remote_state.base.outputs.vpc_id

  ingress {
    description      = "Allow inbound TCP traffic on fluxpostgresQL port"
    from_port        = 5432
    to_port          = 5432
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  ingress {
    description      = "Allow inbound UDP traffic on fluxpostgresQL port"
    from_port        = 5432
    to_port          = 5432
    protocol         = "udp"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  # TODO: Required?
  #egress {
  #  from_port        = 0
  #  to_port          = 0
  #  protocol         = "-1" # Allows all outbound traffic
  #  cidr_blocks      = ["0.0.0.0/0"]
  #  ipv6_cidr_blocks = ["::/0"]
  #}

  tags = {
    Name = "allow-fluxpostgres"
  }
}
resource "aws_ecs_service" "fluxpostgres" {
  name            = "fluxpostgres"
  cluster         = aws_ecs_cluster.ecs_cluster.id
  task_definition = aws_ecs_task_definition.fluxpostgres.arn
  launch_type     = "FARGATE"
  desired_count   = 1

  load_balancer {
    target_group_arn = aws_lb_target_group.fluxpostgres_tcp.arn
    container_name   = "fluxpostgres"
    container_port   = 5432
  }

  network_configuration {
    subnets = [
      data.terraform_remote_state.base.outputs.public_subnet_a_id,
      data.terraform_remote_state.base.outputs.public_subnet_b_id,
      data.terraform_remote_state.base.outputs.public_subnet_c_id,
    ]
    security_groups  = [aws_security_group.fluxpostgres.id]
    assign_public_ip = false
  }
}