main.tf 17.3 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 17
// Policies
resource "aws_iam_policy" "snowplow_collector_policy" {
Cameron McFarland's avatar
Cameron McFarland committed
18
  description = "Policy the allows the collector to access other AWS services such as Kinesis."
19 20
  name        = "snowplow-collector-policy"
  path        = "/"
Cameron McFarland's avatar
Cameron McFarland committed
21 22

  policy = <<EOF
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
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "kinesis:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
EOF
}

resource "aws_iam_policy" "snowplow_enricher_policy" {
Cameron McFarland's avatar
Cameron McFarland committed
52
  description = ""
53 54
  name        = "snowplow-enricher-policy"
  path        = "/"
Cameron McFarland's avatar
Cameron McFarland committed
55 56

  policy = <<EOF
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
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "dynamodb:*"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-east-1:855262394183:table/SnowplowEnrich-gitlab-us-east-1"
            ]
        },
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "kinesis:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
EOF
}

resource "aws_iam_policy" "snowplow_s3loader_policy" {
Cameron McFarland's avatar
Cameron McFarland committed
96 97 98 99 100
  description = "Policy to provide AWS access to snowplow s3loader instances such as S3, DynamoDB, and Kinesis."
  name        = "snowplow-s3loader-policy"
  path        = "/"

  policy = <<EOF
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
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "arn:aws:s3:::gitlab-com-snowplow-test-one/*"
            ]
        },
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "dynamodb:*"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-east-1:855262394183:table/SnowplowS3Loader-gitlab-us-east-1"
            ]
        },
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": [
                "kinesis:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
EOF
}

149 150
// VPC
resource "aws_vpc" "snowplow_vpc" {
Cameron McFarland's avatar
Cameron McFarland committed
151 152
  cidr_block = "10.32.0.0/16"

153
  tags = {
154
    Name        = "SnowPlow VPC"
155 156 157 158 159
    environment = "SnowPlow"
  }
}

// Subnet
160
resource "aws_subnet" "snowplow_subnet_1" {
161 162
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.2.0/24"
163
  availability_zone = "us-east-1a"
Cameron McFarland's avatar
Cameron McFarland committed
164

165
  tags = {
166
    Name        = "SnowPlow Subnet 1"
167 168 169
    environment = "SnowPlow"
  }
}
170

171
resource "aws_subnet" "snowplow_subnet_2" {
172 173
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.1.0/24"
174
  availability_zone = "us-east-1e"
Cameron McFarland's avatar
Cameron McFarland committed
175

176
  tags = {
177
    Name        = "SnowPlow Subnet 2"
178 179 180 181
    environment = "SnowPlow"
  }
}

182
resource "aws_subnet" "snowplow_subnet_3" {
183 184
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.0.0/24"
185
  availability_zone = "us-east-1b"
Cameron McFarland's avatar
Cameron McFarland committed
186

187
  tags = {
188
    Name        = "SnowPlow Subnet 3"
189 190 191 192
    environment = "SnowPlow"
  }
}

193 194 195 196
// Internet Gateway
resource "aws_internet_gateway" "snowplow_gw" {
  vpc_id = "${aws_vpc.snowplow_vpc.id}"

197
  tags = {
198
    Name        = "SnowPlow Gateway"
199 200 201 202
    environment = "SnowPlow"
  }
}

203 204 205 206
// Routing Tables
resource "aws_route_table" "snowplow_route_table" {
  vpc_id = "${aws_vpc.snowplow_vpc.id}"

207
  tags = {
208
    Name        = "SnowPlow Routing Table"
209 210 211
    environment = "SnowPlow"
  }
}
212

Cameron McFarland's avatar
Cameron McFarland committed
213
resource "aws_main_route_table_association" "snowplow_main_route_table_association" {
214
  vpc_id         = "${aws_vpc.snowplow_vpc.id}"
Cameron McFarland's avatar
Cameron McFarland committed
215 216 217 218
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
}

resource "aws_route" "snowplow_route" {
219
  route_table_id         = "${aws_route_table.snowplow_route_table.id}"
Cameron McFarland's avatar
Cameron McFarland committed
220
  destination_cidr_block = "0.0.0.0/0"
221
  gateway_id             = "${aws_internet_gateway.snowplow_gw.id}"
Cameron McFarland's avatar
Cameron McFarland committed
222 223 224 225
}

resource "aws_route_table_association" "snowplow_route_table_association_1" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
226
  subnet_id      = "${aws_subnet.snowplow_subnet_1.id}"
Cameron McFarland's avatar
Cameron McFarland committed
227 228 229 230
}

resource "aws_route_table_association" "snowplow_route_table_association_2" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
231
  subnet_id      = "${aws_subnet.snowplow_subnet_2.id}"
Cameron McFarland's avatar
Cameron McFarland committed
232 233 234 235
}

resource "aws_route_table_association" "snowplow_route_table_association_3" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
236
  subnet_id      = "${aws_subnet.snowplow_subnet_3.id}"
Cameron McFarland's avatar
Cameron McFarland committed
237 238
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
// Security Groups
resource "aws_security_group" "snowplow_security_group" {
  description            = "For testing snowplow stuff"
  name                   = "SnowPlowTesting"
  vpc_id                 = "${aws_vpc.snowplow_vpc.id}"

  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"
    Name        = "SnowPlowTesting"
  }
}

303
// Kinesis Streams
304
resource "aws_kinesis_stream" "snowplow_raw_good" {
305 306
  name             = "snowplow-raw-good"
  shard_count      = 1
307
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
308

309 310 311 312
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]
313

314 315 316 317 318 319
  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_kinesis_stream" "snowplow_raw_bad" {
320 321
  name             = "snowplow-raw-bad"
  shard_count      = 1
322
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
323

324 325 326 327 328 329 330 331 332 333
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}

334
resource "aws_kinesis_stream" "snowplow_enriched_bad" {
335 336
  name             = "snowplow-enriched-bad"
  shard_count      = 1
337
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
338

339 340 341 342
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]
343

344 345 346 347 348 349
  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_kinesis_stream" "snowplow_enriched_good" {
350 351
  name             = "snowplow-enriched-good"
  shard_count      = 1
352
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
353

354 355 356 357 358 359 360 361 362 363 364
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_kinesis_stream" "snowplow_s3loader_bad" {
365 366
  name             = "snowplow-s3loader-bad"
  shard_count      = 1
367
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
368

369 370 371 372 373 374 375 376 377
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}
378 379

// EC2 Launch Configs
380 381 382 383
data "aws_ami" "amazonlinux2" {
  most_recent = true

  filter {
384
    name   = "name"
385 386 387 388
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }

  filter {
389
    name   = "virtualization-type"
390 391 392 393 394 395 396 397 398
    values = ["hvm"]
  }

  owners = ["137112412989"] # Amazon Images
}

// Security Groups

resource "aws_launch_configuration" "snowplow_collector_launch_config" {
399 400
  image_id                    = "${data.aws_ami.amazonlinux2.id}"
  instance_type               = "t2.micro"
401
  associate_public_ip_address = "true"
402 403
  enable_monitoring           = "false"
  iam_instance_profile        = "snowplow-collector-role"
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 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

  security_groups = [
    "sg-0270f6c10c30d8bf3",
  ]

  user_data = <<EOUD
  #!/bin/bash
  ## This has, so far, been written to run on Amazon Linux 2 AMI.
  ## Install Java 1.8
  yum -y install java-1.8.0-openjdk
  ## Set up user, group, and install location
  groupadd snowplow
  adduser --system --gid snowplow snowplow
  mkdir -p /snowplow
  mkdir -p /snowplow/config
  ## Install SnowPlow Kinesis Collector
  mkdir -p /tmp/build
  cd /tmp/build
  wget -q http://dl.bintray.com/snowplow/snowplow-generic/snowplow_scala_stream_collector_kinesis_0.15.0.zip
  unzip -d /snowplow/bin snowplow_scala_stream_collector_kinesis_0.15.0.zip
  cd /tmp
  rm -rf /tmp/build
  ## Need to copy in a config
  cat > /snowplow/config/collector.hocon <<EOF
  collector {
    interface = "0.0.0.0"
    port = "8000"
    production = true

    p3p {
      policyRef = "/w3c/p3p.xml"
      CP = "NOI DSP COR NID PSA OUR IND COM NAV STA"
    }

    crossDomain {
      enabled = true
      domains = [ "*" ]
      secure = true
    }

    cookie {
      enabled = false
      expiration = "365 days"
      name = "snwplw"
      domain = "gitlab.sinter-collect.com"
    }

    doNotTrackCookie {
      enabled = false
      name = "COLLECTOR_DO_NOT_TRACK_COOKIE_NAME"
      value = "COLLECTOR_DO_NOT_TRACK_COOKIE_VALUE"
    }

    cookieBounce {
      enabled = false
      name = "n3pc"
      fallbackNetworkUserId = "00000000-0000-4000-A000-000000000000"
      forwardedProtocolHeader = "X-Forwarded-Proto"
    }

    redirectMacro {
      enabled = false
    }

    cors {
      accessControlMaxAge = 5 seconds
    }

    rootResponse {
      enabled = false
      statusCode = 302
    }

    prometheusMetrics {
      enabled = false
    }

    streams {
      good = "snowplow-raw-good"
      bad = "snowplow-raw-bad"
      useIpAddressAsPartitionKey = true

      sink {
        enabled = kinesis
        region = "us-east-1"
        threadPoolSize = 10

        aws {
          accessKey = iam
          secretKey = iam
        }

        backoffPolicy {
          minBackoff = 10
          maxBackoff = 300000
        }
      }

      buffer {
        byteLimit = 16384
        recordLimit = 1000
        timeLimit = 10000
      }
    }
  }

  akka {
    loglevel = OFF
    loggers = ["akka.event.slf4j.Slf4jLogger"]

    http.server {
      remote-address-header = on
      raw-request-uri-header = on

      parsing {
        max-uri-length = 32768
        uri-parsing-mode = relaxed
      }
    }
  }
  EOF
  chown -R snowplow:snowplow /snowplow
  ## Star the collector service
  su snowplow -g snowplow -c '/usr/bin/java -jar /snowplow/bin/snowplow-stream-collector-kinesis-0.15.0.jar --config /snowplow/config/collector.hocon'
EOUD

  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"
  iam_instance_profile        = "snowplow-enricher-role"

  security_groups = [
    "sg-0270f6c10c30d8bf3",
  ]

  user_data = <<EOUD
  #!/bin/bash
  ## This has, so far, been written to run on Amazon Linux 2 AMI.
  ## Install Java 1.8
  yum -y install java-1.8.0-openjdk
  ## Set up user, group, and install location
  groupadd snowplow
  adduser --system --gid snowplow snowplow
  mkdir -p /snowplow
  mkdir -p /snowplow/config
  ## Install SnowPlow Kinesis Collector
  mkdir -p /tmp/build
  cd /tmp/build
  wget -q http://dl.bintray.com/snowplow/snowplow-generic/snowplow_stream_enrich_kinesis_0.21.0.zip
  unzip -d /snowplow/bin snowplow_stream_enrich_kinesis_0.21.0.zip
  cd /tmp
  rm -rf /tmp/build
  ## We need an IGLU Resolver config
  cat > /snowplow/config/iglu_resolver.json <<EOJ
  {
    "schema": "iglu:com.snowplowanalytics.iglu/resolver-config/jsonschema/1-0-1",
    "data": {
      "cacheSize": 500,
      "repositories": [
        {
          "name": "Iglu Central",
          "priority": 0,
          "vendorPrefixes": [ "com.snowplowanalytics" ],
          "connection": {
            "http": {
              "uri": "http://iglucentral.com"
            }
          }
        },
        {
          "name": "Iglu Central - GCP Mirror",
          "priority": 1,
          "vendorPrefixes": [ "com.snowplowanalytics" ],
          "connection": {
            "http": {
              "uri": "http://mirror01.iglucentral.com"
            }
          }
        }
      ]
    }
  }
  EOJ
  ## Need to copy in a config
  cat > /snowplow/config/enricher.hocon <<EOF
  enrich {
    production = true

    streams {
      in {
        raw = "snowplow-raw-good"
      }

      out {
        enriched = "snowplow-enriched-good"
        bad = "snowplow-enriched-bad"
        partitionKey = "user_ipaddress"
      }

      sourceSink {
        enabled = kinesis

        aws {
          accessKey = iam
          secretKey = iam
        }

        region = "us-east-1"
        maxRecords = 10000
        initialPosition = TRIM_HORIZON
        backoffPolicy {
          minBackoff = 10
          maxBackoff = 300000
        }
      }

      buffer {
        byteLimit = 16384
        recordLimit = 1000
        timeLimit = 10000
      }

      appName = "SnowplowEnrich-gitlab-us-east-1"
    }
  }
  EOF
  chown -R snowplow:snowplow /snowplow
  ## Star the collector service
  su snowplow -g snowplow -c '/usr/bin/java -jar /snowplow/bin/snowplow-stream-enrich-kinesis-0.21.0.jar --config /snowplow/config/enricher.hocon --resolver file:/snowplow/config/iglu_resolver.json'
EOUD

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_launch_configuration" "snowplow_s3loader_launch_config" {
648 649
  image_id                    = "${data.aws_ami.amazonlinux2.id}"
  instance_type               = "t2.micro"
650
  associate_public_ip_address = "true"
651 652
  enable_monitoring           = "false"
  iam_instance_profile        = "snowplow-s3loader-role"
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 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

  security_groups = [
    "sg-0270f6c10c30d8bf3",
  ]

  user_data = <<EOUD
  #!/bin/bash
  ## This has, so far, been written to run on Amazon Linux 2 AMI.
  ## Install Java 1.8
  yum -y install java-1.8.0-openjdk lzop lzo-devel
  ## Set up user, group, and install location
  groupadd snowplow
  adduser --system --gid snowplow snowplow
  mkdir -p /snowplow
  mkdir -p /snowplow/config
  ## Install SnowPlow Kinesis Collector
  mkdir -p /tmp/build
  cd /tmp/build
  wget -q http://dl.bintray.com/snowplow/snowplow-generic/snowplow_s3_loader_0.6.0.zip
  unzip -d /snowplow/bin snowplow_s3_loader_0.6.0.zip
  cd /tmp
  rm -rf /tmp/build
  ## Need to copy in a config
  cat > /snowplow/config/s3loader.hocon <<EOF
  source = "kinesis"

  sink = "kinesis"

  aws {
    accessKey = "iam"
    secretKey = "iam"
  }

  nsq {
    channelName = "noidea"
    host = "snowplownsq.gitlab.com"
    port = 8085
    lookupPort = 8090
  }

  kinesis {
    initialPosition = "LATEST"
    maxRecords = 5
    region = "us-east-1"
    appName = "SnowplowS3Loader-gitlab-us-east-1"
  }

  streams {
    inStreamName = "snowplow-enriched-good"
    outStreamName = "snowplow-s3loader-bad"
    buffer {
      byteLimit = 16384
      recordLimit = 1000
      timeLimit = 10000
    }
  }

  s3 {
    region = "us-east-1"
    bucket = "gitlab-com-snowplow-test-one"
    format = "gzip"
    maxTimeout = 120000000
  }
  EOF
  chown -R snowplow:snowplow /snowplow
  ## Star the collector service
  su snowplow -g snowplow -c '/usr/bin/java -jar /snowplow/bin/snowplow-s3-loader-0.6.0.jar --config /snowplow/config/s3loader.hocon'
EOUD

  lifecycle {
    create_before_destroy = true
  }
}
726 727 728

// EC2 Auto Scaling Groups
resource "aws_autoscaling_group" "snowplow_collector_autoscaling_group" {
729
  launch_configuration = "${aws_launch_configuration.snowplow_collector_launch_config.id}"
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
  max_size             = "3"
  min_size             = "3"

  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" {
747
  launch_configuration = "${aws_launch_configuration.snowplow_enricher_launch_config.id}"
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
  max_size             = "3"
  min_size             = "3"

  tag {
    key                 = "environment"
    value               = "SnowPlow"
    propagate_at_launch = true
  }

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

resource "aws_autoscaling_group" "snowplow_s3loader_autoscaling_group" {
765
  launch_configuration = "${aws_launch_configuration.snowplow_s3loader_launch_config.id}"
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
  max_size             = "1"
  min_size             = "1"

  tag {
    key                 = "environment"
    value               = "SnowPlow"
    propagate_at_launch = true
  }

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