main.tf 17.4 KB
Newer Older
1 2
// Configure remote state
terraform {
3 4 5 6 7
  backend "s3" {
    bucket = "gitlab-com-infrastructure"
    key    = "terraform/aws-snowplow/terraform.tfstate"
    region = "us-east-1"
  }
8 9 10 11 12
}

// Use credentials from environment or shared credentials file
provider "aws" {
  region  = "us-east-1"
13
  version = "~> 1.41"
14 15
}

16
// Data Templates
17
data "template_file" "user_data_collector" {
18
  template = "${file("${path.module}/templates/collector-user-data.sh")}"
19 20 21
}

data "template_file" "user_data_enricher" {
22
  template = "${file("${path.module}/templates/enricher-user-data.sh")}"
23 24
}

25 26 27 28 29 30 31 32
data "template_file" "iam_policy_collector" {
  template = "${file("${path.module}/templates/iam_policy_collector.json")}"
}

data "template_file" "iam_policy_enricher" {
  template = "${file("${path.module}/templates/iam_policy_enricher.json")}"
}

33 34 35 36 37 38 39 40
data "template_file" "iam_policy_firehose_enriched_bad" {
  template = "${file("${path.module}/templates/iam_policy_firehose_enriched_bad.json")}"
}

data "template_file" "iam_policy_firehose_enriched_good" {
  template = "${file("${path.module}/templates/iam_policy_firehose_enriched_good.json")}"
}

41 42 43 44
data "template_file" "iam_policy_lambda" {
  template = "${file("${path.module}/templates/iam_policy_lambda.json")}"
}

45 46 47 48 49 50 51 52
data "template_file" "iam_role_collector" {
  template = "${file("${path.module}/templates/iam_role_collector.json")}"
}

data "template_file" "iam_role_enricher" {
  template = "${file("${path.module}/templates/iam_role_enricher.json")}"
}

53 54 55 56
data "template_file" "iam_role_lambda" {
  template = "${file("${path.module}/templates/iam_role_lambda.json")}"
}

57 58 59 60 61 62 63 64
data "template_file" "iam_role_firehose_delivery" {
  template = "${file("${path.module}/templates/iam_role_firehose_delivery.json")}"
}

data "template_file" "snowplow_s3_bucket_policy" {
  template = "${file("${path.module}/templates/gitlab-com-snowplow-events.policy.json")}"
}

65 66
// Policies
resource "aws_iam_policy" "snowplow_collector_policy" {
67
  description = "Policy the allows the collector to access other AWS services such as Kinesis."
68 69
  name        = "snowplow-collector-policy"
  path        = "/"
Cameron McFarland's avatar
Cameron McFarland committed
70

71
  policy = "${data.template_file.iam_policy_collector.rendered}"
72 73 74
}

resource "aws_iam_policy" "snowplow_enricher_policy" {
Cameron McFarland's avatar
Cameron McFarland committed
75
  description = ""
76 77
  name        = "snowplow-enricher-policy"
  path        = "/"
Cameron McFarland's avatar
Cameron McFarland committed
78

79
  policy = "${data.template_file.iam_policy_enricher.rendered}"
80 81
}

82 83 84 85 86 87 88 89
resource "aws_iam_policy" "snowplow_lambda_policy" {
  description = ""
  name        = "AWSLambdaBasicExecutionRole-b1df0a33-ac33-47d3-930b-e8e0bf9443ef"
  path        = "/service-role/"

  policy = "${data.template_file.iam_policy_lambda.rendered}"
}

90 91 92 93 94 95 96 97 98 99 100 101
resource "aws_iam_role_policy" "snowplow_firehose_enriched_bad_policy" {
  name   = "firehose_enriched_bad"
  policy = "${data.template_file.iam_policy_firehose_enriched_bad.rendered}"
  role   = "${aws_iam_role.snowplow_firehose_delivery_role.id}"
}

resource "aws_iam_role_policy" "snowplow_firehose_enriched_good_policy" {
  name   = "firehose_enriched_good"
  policy = "${data.template_file.iam_policy_firehose_enriched_good.rendered}"
  role   = "${aws_iam_role.snowplow_firehose_delivery_role.id}"
}

Cameron McFarland's avatar
Cameron McFarland committed
102 103
// Roles
resource "aws_iam_role" "snowplow_collector_role" {
104 105
  name               = "snowplow-collector-role"
  assume_role_policy = "${data.template_file.iam_role_collector.rendered}"
Cameron McFarland's avatar
Cameron McFarland committed
106 107 108 109 110 111 112

  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_iam_role" "snowplow_enricher_role" {
113 114
  name               = "snowplow-enricher-role"
  assume_role_policy = "${data.template_file.iam_role_enricher.rendered}"
Cameron McFarland's avatar
Cameron McFarland committed
115 116 117 118 119 120

  tags = {
    environment = "SnowPlow"
  }
}

121 122 123 124 125 126 127 128 129 130
resource "aws_iam_role" "snowplow_lambda_role" {
  name               = ""
  assume_role_policy = "${data.template_file.iam_role_lambda.rendered}"
  path               = "/service-role/"

  tags = {
    environment = "SnowPlow"
  }
}

131 132 133 134 135 136 137 138 139 140 141 142
resource "aws_iam_role" "snowplow_firehose_delivery_role" {
  name               = ""
  assume_role_policy = "${data.template_file.iam_role_lambda.rendered}"
  path               = "/"

  assume_role_policy = "${data.template_file.iam_role_firehose_delivery.rendered}"

  tags = {
    environment = "SnowPlow"
  }
}

Cameron McFarland's avatar
Cameron McFarland committed
143 144 145 146 147 148 149 150 151 152 153
// Role Policy Attachments
resource "aws_iam_role_policy_attachment" "collector_role_policy_attachment" {
  role       = "${aws_iam_role.snowplow_collector_role.name}"
  policy_arn = "${aws_iam_policy.snowplow_collector_policy.arn}"
}

resource "aws_iam_role_policy_attachment" "enricher_role_policy_attachment" {
  role       = "${aws_iam_role.snowplow_enricher_role.name}"
  policy_arn = "${aws_iam_policy.snowplow_enricher_policy.arn}"
}

154 155 156 157 158
resource "aws_iam_role_policy_attachment" "lambda_role_policy_attachment" {
  role       = "${aws_iam_role.snowplow_lambda_role.name}"
  policy_arn = "${aws_iam_policy.snowplow_lambda_policy.arn}"
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
// S3 Buckets
resource "aws_s3_bucket" "snowplow_s3_bucket" {
  bucket = "gitlab-com-snowplow-events"

  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_s3_bucket_policy" "snowplow_s3_bucket_policy" {
  bucket = "${aws_s3_bucket.snowplow_s3_bucket.id}"

  policy = "${data.template_file.snowplow_s3_bucket_policy.rendered}"
}

174 175
// VPC
resource "aws_vpc" "snowplow_vpc" {
Cameron McFarland's avatar
Cameron McFarland committed
176 177
  cidr_block = "10.32.0.0/16"

178
  tags = {
179
    Name        = "SnowPlow VPC"
180 181 182 183 184
    environment = "SnowPlow"
  }
}

// Subnet
185
resource "aws_subnet" "snowplow_subnet_1" {
186 187
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.2.0/24"
188
  availability_zone = "us-east-1a"
Cameron McFarland's avatar
Cameron McFarland committed
189

190
  tags = {
191
    Name        = "SnowPlow Subnet 1"
192 193 194
    environment = "SnowPlow"
  }
}
195

196
resource "aws_subnet" "snowplow_subnet_2" {
197 198
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.1.0/24"
199
  availability_zone = "us-east-1e"
Cameron McFarland's avatar
Cameron McFarland committed
200

201
  tags = {
202
    Name        = "SnowPlow Subnet 2"
203 204 205 206
    environment = "SnowPlow"
  }
}

207
resource "aws_subnet" "snowplow_subnet_3" {
208 209
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.0.0/24"
210
  availability_zone = "us-east-1b"
Cameron McFarland's avatar
Cameron McFarland committed
211

212
  tags = {
213
    Name        = "SnowPlow Subnet 3"
214 215 216 217
    environment = "SnowPlow"
  }
}

218 219 220 221 222 223
resource "aws_subnet" "snowplow_subnet_4" {
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.3.0/24"
  availability_zone = "us-east-1c"

  tags = {
224
    Name        = "SnowPlow Subnet 4"
225 226 227 228
    environment = "SnowPlow"
  }
}

229 230 231 232
// Internet Gateway
resource "aws_internet_gateway" "snowplow_gw" {
  vpc_id = "${aws_vpc.snowplow_vpc.id}"

233
  tags = {
234
    Name        = "SnowPlow Gateway"
235 236 237 238
    environment = "SnowPlow"
  }
}

239 240 241 242
// Routing Tables
resource "aws_route_table" "snowplow_route_table" {
  vpc_id = "${aws_vpc.snowplow_vpc.id}"

243
  tags = {
244
    Name        = "SnowPlow Routing Table"
245 246 247
    environment = "SnowPlow"
  }
}
248

249
resource "aws_main_route_table_association" "snowplow_main_route_table_association" {
250
  vpc_id         = "${aws_vpc.snowplow_vpc.id}"
251 252 253 254
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
}

resource "aws_route" "snowplow_route" {
255
  route_table_id         = "${aws_route_table.snowplow_route_table.id}"
256
  destination_cidr_block = "0.0.0.0/0"
257
  gateway_id             = "${aws_internet_gateway.snowplow_gw.id}"
258 259 260 261
}

resource "aws_route_table_association" "snowplow_route_table_association_1" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
262
  subnet_id      = "${aws_subnet.snowplow_subnet_1.id}"
263 264 265 266
}

resource "aws_route_table_association" "snowplow_route_table_association_2" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
267
  subnet_id      = "${aws_subnet.snowplow_subnet_2.id}"
268 269 270 271
}

resource "aws_route_table_association" "snowplow_route_table_association_3" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
272
  subnet_id      = "${aws_subnet.snowplow_subnet_3.id}"
273 274
}

275 276 277 278 279
resource "aws_route_table_association" "snowplow_route_table_association_4" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
  subnet_id      = "${aws_subnet.snowplow_subnet_4.id}"
}

280 281
// Security Groups
resource "aws_security_group" "snowplow_security_group" {
282 283
  description = "For snowplow stuff"
  name        = "SnowPlow"
284
  vpc_id      = "${aws_vpc.snowplow_vpc.id}"
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299
  egress {
    from_port = "0"
    to_port   = "0"
    protocol  = "-1"

    cidr_blocks = [
      "0.0.0.0/0",
    ]

    ipv6_cidr_blocks = [
      "::/0",
    ]
  }

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  ingress {
    from_port   = 8000
    to_port     = 8000
    protocol    = "tcp"
    description = "Collector"

    cidr_blocks = [
      "0.0.0.0/0",
    ]

    ipv6_cidr_blocks = [
      "::/0",
    ]
  }

  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    description = "Enricher"

    cidr_blocks = [
      "0.0.0.0/0",
    ]

    ipv6_cidr_blocks = [
      "::/0",
    ]
  }

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    description = "SSH for Admin IPv4"

    cidr_blocks = [
      "0.0.0.0/0",
    ]
  }

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    description = "SSH for Admin IPv6"

    ipv6_cidr_blocks = [
      "::/0",
    ]
  }

  tags = {
    environment = "SnowPlow"
354
    Name        = "SnowPlow"
355 356 357
  }
}

358
// Kinesis Streams
359
resource "aws_kinesis_stream" "snowplow_raw_good" {
360 361
  name             = "snowplow-raw-good"
  shard_count      = 1
362
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
363

364 365 366 367
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]
368

369 370 371 372 373 374
  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_kinesis_stream" "snowplow_raw_bad" {
375 376
  name             = "snowplow-raw-bad"
  shard_count      = 1
377
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
378

379 380 381 382 383 384 385 386 387 388
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}

389
resource "aws_kinesis_stream" "snowplow_enriched_bad" {
390 391
  name             = "snowplow-enriched-bad"
  shard_count      = 1
392
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
393

394 395 396 397
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]
398

399 400 401 402 403 404
  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_kinesis_stream" "snowplow_enriched_good" {
405 406
  name             = "snowplow-enriched-good"
  shard_count      = 1
407
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
408

409 410 411 412 413 414 415 416 417 418
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}

419 420
// Kinesis Firehose

421
// EC2 Launch Configs
422 423 424 425
data "aws_ami" "amazonlinux2" {
  most_recent = true

  filter {
426
    name   = "name"
427 428 429 430
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }

  filter {
431
    name   = "virtualization-type"
432 433 434 435 436 437 438
    values = ["hvm"]
  }

  owners = ["137112412989"] # Amazon Images
}

resource "aws_launch_configuration" "snowplow_collector_launch_config" {
439
  image_id                    = "${data.aws_ami.amazonlinux2.id}"
440
  instance_type               = "m5.large"
441
  associate_public_ip_address = "true"
442
  enable_monitoring           = "false"
443
  iam_instance_profile        = "${aws_iam_role.snowplow_collector_role.id}"
444
  key_name                    = "cmcfarland"
445 446

  security_groups = [
447
    "${aws_security_group.snowplow_security_group.id}",
448 449
  ]

450
  user_data = "${data.template_file.user_data_collector.rendered}"
451 452 453 454 455 456 457 458 459 460 461

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_launch_configuration" "snowplow_enricher_launch_config" {
  image_id                    = "${data.aws_ami.amazonlinux2.id}"
  instance_type               = "t2.micro"
  associate_public_ip_address = "true"
  enable_monitoring           = "false"
462
  iam_instance_profile        = "${aws_iam_role.snowplow_enricher_role.id}"
463
  key_name                    = "cmcfarland"
464 465

  security_groups = [
466
    "${aws_security_group.snowplow_security_group.id}",
467 468
  ]

469
  user_data = "${data.template_file.user_data_enricher.rendered}"
470 471 472 473 474 475

  lifecycle {
    create_before_destroy = true
  }
}

476 477 478 479 480 481 482 483 484 485 486 487
// EC2 Target Group
resource "aws_lb_target_group" "snowplow_collector_lb_target_group" {
  name     = "SnowPlowNLBTargetGroup"
  port     = 8000
  protocol = "TCP"
  vpc_id   = "${aws_vpc.snowplow_vpc.id}"

  tags = {
    environment = "SnowPlow"
  }
}

488 489
// EC2 Auto Scaling Groups
resource "aws_autoscaling_group" "snowplow_collector_autoscaling_group" {
490
  launch_configuration = "${aws_launch_configuration.snowplow_collector_launch_config.id}"
491 492 493
  max_size             = "12"
  min_size             = "0"
  desired_capacity     = "8"
494

495 496
  target_group_arns = [
    "${aws_lb_target_group.snowplow_collector_lb_target_group.id}",
497 498
  ]

499 500 501 502 503 504 505
  vpc_zone_identifier = [
    "${aws_subnet.snowplow_subnet_1.id}",
    "${aws_subnet.snowplow_subnet_2.id}",
    "${aws_subnet.snowplow_subnet_3.id}",
    "${aws_subnet.snowplow_subnet_4.id}",
  ]

506 507 508 509 510 511 512 513 514
  enabled_metrics = [
    "GroupStandbyInstances",
    "GroupTotalInstances",
    "GroupPendingInstances",
    "GroupTerminatingInstances",
    "GroupDesiredCapacity",
    "GroupInServiceInstances",
    "GroupMinSize",
    "GroupMaxSize",
515 516
  ]

517 518 519 520 521 522 523 524 525 526 527 528 529 530
  tag {
    key                 = "environment"
    value               = "SnowPlow"
    propagate_at_launch = true
  }

  tag {
    key                 = "Name"
    value               = "SnowPlowAutoCollector"
    propagate_at_launch = true
  }
}

resource "aws_autoscaling_group" "snowplow_enricher_autoscaling_group" {
531
  launch_configuration = "${aws_launch_configuration.snowplow_enricher_launch_config.id}"
532
  max_size             = "3"
533
  min_size             = "0"
534
  desired_capacity     = "3"
535

536 537 538 539 540 541 542
  vpc_zone_identifier = [
    "${aws_subnet.snowplow_subnet_1.id}",
    "${aws_subnet.snowplow_subnet_2.id}",
    "${aws_subnet.snowplow_subnet_3.id}",
    "${aws_subnet.snowplow_subnet_4.id}",
  ]

543 544 545 546 547 548 549 550 551 552 553
  enabled_metrics = [
    "GroupStandbyInstances",
    "GroupTotalInstances",
    "GroupPendingInstances",
    "GroupTerminatingInstances",
    "GroupDesiredCapacity",
    "GroupInServiceInstances",
    "GroupMinSize",
    "GroupMaxSize",
  ]

554 555 556 557 558 559 560 561 562 563 564 565 566
  tag {
    key                 = "environment"
    value               = "SnowPlow"
    propagate_at_launch = true
  }

  tag {
    key                 = "Name"
    value               = "SnowPlowAutoEnricher"
    propagate_at_launch = true
  }
}

567 568 569 570 571 572 573 574 575 576
// EC2 Load Balancer
resource "aws_lb" "snowplow_lb" {
  name               = "SnowPlowNLB"
  internal           = false
  load_balancer_type = "network"

  subnets = [
    "${aws_subnet.snowplow_subnet_1.id}",
    "${aws_subnet.snowplow_subnet_2.id}",
    "${aws_subnet.snowplow_subnet_3.id}",
577
    "${aws_subnet.snowplow_subnet_4.id}",
578 579 580 581 582 583 584
  ]

  tags = {
    environment = "SnowPlow"
  }
}

585
// EC2 Load Balancer Listener
586 587 588 589 590 591 592 593 594 595 596 597
resource "aws_lb_listener" "snowplow_collector_lb_listener" {
  load_balancer_arn = "${aws_lb.snowplow_lb.arn}"
  port              = "443"
  protocol          = "TLS"
  ssl_policy        = "ELBSecurityPolicy-2016-08"
  certificate_arn   = "arn:aws:acm:us-east-1:855262394183:certificate/1d954bfe-8b8d-46f2-a678-5a3e53cc6225"

  default_action {
    type             = "forward"
    target_group_arn = "${aws_lb_target_group.snowplow_collector_lb_target_group.arn}"
  }
}
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

// Lambda Function
resource "aws_lambda_function" "snowplow_event_formatter_lambda_function" {
  description   = "This adds a newline to the end of each record."
  function_name = "SnowPlowFirehoseFormatter"
  handler       = "lambda_function.lambda_handler"
  role          = "${aws_iam_role.snowplow_lambda_role.arn}"
  runtime       = "python2.7"

  filename         = "lambda/lambda_function_payload.zip"
  source_code_hash = "${base64sha256(file("lambda/lambda_function_payload.zip"))}"
  timeout          = "60"

  tags = {
    environment                = "SnowPlow"
    "lambda-console:blueprint" = "kinesis-firehose-process-record-python"
  }
}
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

// Firehose
resource "aws_kinesis_firehose_delivery_stream" "snowplow_enriched_bad_firehose" {
  destination = "extended_s3"
  name        = "SnowPlowEnrichedBad"

  // Terraform seems to be bad at this?
  // https://github.com/terraform-providers/terraform-provider-aws/issues/6053
  lifecycle {
    ignore_changes = [
      "extended_s3_configuration.0.data_format_conversion_configuration",
      "extended_s3_configuration.0.data_format_conversion_configuration.0.enabled",
    ]
  }

  extended_s3_configuration {
    bucket_arn          = "${aws_s3_bucket.snowplow_s3_bucket.arn}"
    role_arn            = "${aws_iam_role.snowplow_firehose_delivery_role.arn}"
    compression_format  = "GZIP"
    prefix              = "enriched-bad/"
    error_output_prefix = "enriched-bad/"
    s3_backup_mode      = "Disabled"

    processing_configuration {
      enabled = "true"

      processors {
        type = "Lambda"

        parameters {
          parameter_name  = "LambdaArn"
          parameter_value = "${aws_lambda_function.snowplow_event_formatter_lambda_function.arn}:$LATEST"
        }
      }
    }
  }

  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_kinesis_firehose_delivery_stream" "snowplow_enriched_good_firehose" {
  destination = "extended_s3"
  name        = "SnowPlowEnrichedGood"

  // Terraform seems to be bad at this?
  // https://github.com/terraform-providers/terraform-provider-aws/issues/6053
  lifecycle {
    ignore_changes = [
      "extended_s3_configuration.0.data_format_conversion_configuration",
      "extended_s3_configuration.0.data_format_conversion_configuration.0.enabled",
    ]
  }

  extended_s3_configuration {
    bucket_arn          = "${aws_s3_bucket.snowplow_s3_bucket.arn}"
    role_arn            = "${aws_iam_role.snowplow_firehose_delivery_role.arn}"
    compression_format  = "GZIP"
    prefix              = "output/"
    error_output_prefix = "output/"
    s3_backup_mode      = "Disabled"

    processing_configuration {
      enabled = "true"

      processors {
        type = "Lambda"

        parameters {
          parameter_name  = "LambdaArn"
          parameter_value = "${aws_lambda_function.snowplow_event_formatter_lambda_function.arn}:$LATEST"
        }
      }
    }
  }

  tags = {
    environment = "SnowPlow"
  }
}