main.tf 19.7 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
data "template_file" "iam_policy_firehose_enriched_bad" {
  template = "${file("${path.module}/templates/iam_policy_firehose_enriched_bad.json")}"
}

37 38 39 40
data "template_file" "iam_policy_firehose_raw_bad" {
  template = "${file("${path.module}/templates/iam_policy_firehose_raw_bad.json")}"
}

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

45 46 47 48
data "template_file" "iam_policy_lambda" {
  template = "${file("${path.module}/templates/iam_policy_lambda.json")}"
}

49 50 51 52 53 54 55 56
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")}"
}

57 58 59 60
data "template_file" "iam_role_lambda" {
  template = "${file("${path.module}/templates/iam_role_lambda.json")}"
}

61 62 63 64 65 66 67 68
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")}"
}

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

75
  policy = "${data.template_file.iam_policy_collector.rendered}"
76 77 78
}

resource "aws_iam_policy" "snowplow_enricher_policy" {
Cameron McFarland's avatar
Cameron McFarland committed
79
  description = ""
80 81
  name        = "snowplow-enricher-policy"
  path        = "/"
Cameron McFarland's avatar
Cameron McFarland committed
82

83
  policy = "${data.template_file.iam_policy_enricher.rendered}"
84 85
}

86 87 88 89 90 91 92 93
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}"
}

94 95 96 97 98 99
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}"
}

100 101 102 103 104 105
resource "aws_iam_role_policy" "snowplow_firehose_raw_bad_policy" {
  name   = "firehose_raw_bad"
  policy = "${data.template_file.iam_policy_firehose_raw_bad.rendered}"
  role   = "${aws_iam_role.snowplow_firehose_delivery_role.id}"
}

106 107 108 109 110 111
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
112 113
// Roles
resource "aws_iam_role" "snowplow_collector_role" {
114 115
  name               = "snowplow-collector-role"
  assume_role_policy = "${data.template_file.iam_role_collector.rendered}"
Cameron McFarland's avatar
Cameron McFarland committed
116 117 118 119 120 121 122

  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_iam_role" "snowplow_enricher_role" {
123 124
  name               = "snowplow-enricher-role"
  assume_role_policy = "${data.template_file.iam_role_enricher.rendered}"
Cameron McFarland's avatar
Cameron McFarland committed
125 126 127 128 129 130

  tags = {
    environment = "SnowPlow"
  }
}

131 132 133 134 135 136 137 138 139 140
resource "aws_iam_role" "snowplow_lambda_role" {
  name               = ""
  assume_role_policy = "${data.template_file.iam_role_lambda.rendered}"
  path               = "/service-role/"

  tags = {
    environment = "SnowPlow"
  }
}

141 142 143 144 145 146 147 148 149 150 151 152
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
153 154 155 156 157 158 159 160 161 162 163
// 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}"
}

164 165 166 167 168
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}"
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
// 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}"
}

184 185 186 187 188 189
resource "aws_s3_bucket_notification" "snowplow_bucket_notifications" {
  bucket = "gitlab-com-snowplow-events"

  queue {
    queue_arn     = "arn:aws:sqs:us-east-1:730570900080:sf-snowpipe-AIDAJ5LKTH5PBBNSL6UC2-14u9uWhgmVmEsNRfTA5B9w"
    events        = ["s3:ObjectCreated:*"]
190
    filter_prefix = ""
191 192 193
  }
}

194 195
// VPC
resource "aws_vpc" "snowplow_vpc" {
Cameron McFarland's avatar
Cameron McFarland committed
196 197
  cidr_block = "10.32.0.0/16"

198
  tags = {
199
    Name        = "SnowPlow VPC"
200 201 202 203 204
    environment = "SnowPlow"
  }
}

// Subnet
205
resource "aws_subnet" "snowplow_subnet_1" {
206 207
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.2.0/24"
208
  availability_zone = "us-east-1a"
Cameron McFarland's avatar
Cameron McFarland committed
209

210
  tags = {
211
    Name        = "SnowPlow Subnet 1"
212 213 214
    environment = "SnowPlow"
  }
}
215

216
resource "aws_subnet" "snowplow_subnet_2" {
217 218
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.1.0/24"
219
  availability_zone = "us-east-1e"
Cameron McFarland's avatar
Cameron McFarland committed
220

221
  tags = {
222
    Name        = "SnowPlow Subnet 2"
223 224 225 226
    environment = "SnowPlow"
  }
}

227
resource "aws_subnet" "snowplow_subnet_3" {
228 229
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.0.0/24"
230
  availability_zone = "us-east-1b"
Cameron McFarland's avatar
Cameron McFarland committed
231

232
  tags = {
233
    Name        = "SnowPlow Subnet 3"
234 235 236 237
    environment = "SnowPlow"
  }
}

238 239 240 241 242 243
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 = {
244
    Name        = "SnowPlow Subnet 4"
245 246 247 248
    environment = "SnowPlow"
  }
}

249 250 251 252
// Internet Gateway
resource "aws_internet_gateway" "snowplow_gw" {
  vpc_id = "${aws_vpc.snowplow_vpc.id}"

253
  tags = {
254
    Name        = "SnowPlow Gateway"
255 256 257 258
    environment = "SnowPlow"
  }
}

259 260 261 262
// Routing Tables
resource "aws_route_table" "snowplow_route_table" {
  vpc_id = "${aws_vpc.snowplow_vpc.id}"

263
  tags = {
264
    Name        = "SnowPlow Routing Table"
265 266 267
    environment = "SnowPlow"
  }
}
268

269
resource "aws_main_route_table_association" "snowplow_main_route_table_association" {
270
  vpc_id         = "${aws_vpc.snowplow_vpc.id}"
271 272 273 274
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
}

resource "aws_route" "snowplow_route" {
275
  route_table_id         = "${aws_route_table.snowplow_route_table.id}"
276
  destination_cidr_block = "0.0.0.0/0"
277
  gateway_id             = "${aws_internet_gateway.snowplow_gw.id}"
278 279 280 281
}

resource "aws_route_table_association" "snowplow_route_table_association_1" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
282
  subnet_id      = "${aws_subnet.snowplow_subnet_1.id}"
283 284 285 286
}

resource "aws_route_table_association" "snowplow_route_table_association_2" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
287
  subnet_id      = "${aws_subnet.snowplow_subnet_2.id}"
288 289 290 291
}

resource "aws_route_table_association" "snowplow_route_table_association_3" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
292
  subnet_id      = "${aws_subnet.snowplow_subnet_3.id}"
293 294
}

295 296 297 298 299
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}"
}

300 301
// Security Groups
resource "aws_security_group" "snowplow_security_group" {
302 303
  description = "For snowplow stuff"
  name        = "SnowPlow"
304
  vpc_id      = "${aws_vpc.snowplow_vpc.id}"
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319
  egress {
    from_port = "0"
    to_port   = "0"
    protocol  = "-1"

    cidr_blocks = [
      "0.0.0.0/0",
    ]

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

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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  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"
374
    Name        = "SnowPlow"
375 376 377
  }
}

378
// Kinesis Streams
379
resource "aws_kinesis_stream" "snowplow_raw_good" {
380
  name             = "snowplow-raw-good"
381
  shard_count      = 4
382
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
383

384 385 386 387
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]
388

389 390 391 392 393 394
  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_kinesis_stream" "snowplow_raw_bad" {
395 396
  name             = "snowplow-raw-bad"
  shard_count      = 1
397
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
398

399 400 401 402 403 404 405 406 407 408
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}

409
resource "aws_kinesis_stream" "snowplow_enriched_bad" {
410 411
  name             = "snowplow-enriched-bad"
  shard_count      = 1
412
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
413

414 415 416 417
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]
418

419 420 421 422 423 424
  tags = {
    environment = "SnowPlow"
  }
}

resource "aws_kinesis_stream" "snowplow_enriched_good" {
425 426
  name             = "snowplow-enriched-good"
  shard_count      = 1
427
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
428

429 430 431 432 433 434 435 436 437 438
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}

439
// EC2 Launch Configs
440 441 442 443
data "aws_ami" "amazonlinux2" {
  most_recent = true

  filter {
444
    name   = "name"
445 446 447 448
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }

  filter {
449
    name   = "virtualization-type"
450 451 452 453 454 455 456
    values = ["hvm"]
  }

  owners = ["137112412989"] # Amazon Images
}

resource "aws_launch_configuration" "snowplow_collector_launch_config" {
457
  image_id                    = "${data.aws_ami.amazonlinux2.id}"
458
  instance_type               = "m5.large"
459
  associate_public_ip_address = "true"
460
  enable_monitoring           = "false"
461
  iam_instance_profile        = "${aws_iam_role.snowplow_collector_role.id}"
462
  key_name                    = "cmcfarland"
463 464

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

468
  user_data = "${data.template_file.user_data_collector.rendered}"
469 470 471 472 473 474 475 476 477 478 479

  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"
480
  iam_instance_profile        = "${aws_iam_role.snowplow_enricher_role.id}"
481
  key_name                    = "cmcfarland"
482 483

  security_groups = [
484
    "${aws_security_group.snowplow_security_group.id}",
485 486
  ]

487
  user_data = "${data.template_file.user_data_enricher.rendered}"
488 489 490 491 492 493

  lifecycle {
    create_before_destroy = true
  }
}

494 495 496 497 498 499 500 501 502 503 504 505
// 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"
  }
}

506 507
// EC2 Auto Scaling Groups
resource "aws_autoscaling_group" "snowplow_collector_autoscaling_group" {
508
  launch_configuration = "${aws_launch_configuration.snowplow_collector_launch_config.id}"
Cameron McFarland's avatar
Cameron McFarland committed
509
  max_size             = "24"
510
  min_size             = "0"
Cameron McFarland's avatar
Cameron McFarland committed
511
  desired_capacity     = "12"
512

513 514
  target_group_arns = [
    "${aws_lb_target_group.snowplow_collector_lb_target_group.id}",
515 516
  ]

517 518 519 520 521 522 523
  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}",
  ]

524 525 526 527 528 529 530 531 532
  enabled_metrics = [
    "GroupStandbyInstances",
    "GroupTotalInstances",
    "GroupPendingInstances",
    "GroupTerminatingInstances",
    "GroupDesiredCapacity",
    "GroupInServiceInstances",
    "GroupMinSize",
    "GroupMaxSize",
533 534
  ]

535 536 537 538 539 540 541 542 543 544 545 546 547 548
  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" {
549
  launch_configuration = "${aws_launch_configuration.snowplow_enricher_launch_config.id}"
Cameron McFarland's avatar
Cameron McFarland committed
550
  max_size             = "12"
551
  min_size             = "0"
552
  desired_capacity     = "3"
553

554 555 556 557 558 559 560
  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}",
  ]

561 562 563 564 565 566 567 568 569 570 571
  enabled_metrics = [
    "GroupStandbyInstances",
    "GroupTotalInstances",
    "GroupPendingInstances",
    "GroupTerminatingInstances",
    "GroupDesiredCapacity",
    "GroupInServiceInstances",
    "GroupMinSize",
    "GroupMaxSize",
  ]

572 573 574 575 576 577 578 579 580 581 582 583 584
  tag {
    key                 = "environment"
    value               = "SnowPlow"
    propagate_at_launch = true
  }

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

585 586 587 588 589 590 591 592 593 594
// 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}",
595
    "${aws_subnet.snowplow_subnet_4.id}",
596 597 598 599 600 601 602
  ]

  tags = {
    environment = "SnowPlow"
  }
}

603
// EC2 Load Balancer Listener
604 605 606 607 608 609 610 611 612 613 614 615
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}"
  }
}
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

// 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"
  }
}
634 635 636 637 638 639

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

640 641 642 643 644
  kinesis_source_configuration {
    kinesis_stream_arn = "${aws_kinesis_stream.snowplow_enriched_bad.arn}"
    role_arn           = "${aws_iam_role.snowplow_firehose_delivery_role.arn}"
  }

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
  // 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"

685 686 687 688 689
  kinesis_source_configuration {
    kinesis_stream_arn = "${aws_kinesis_stream.snowplow_enriched_good.arn}"
    role_arn           = "${aws_iam_role.snowplow_firehose_delivery_role.arn}"
  }

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
  // 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"
  }
}
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

resource "aws_kinesis_firehose_delivery_stream" "snowplow_raw_bad_firehose" {
  destination = "extended_s3"
  name        = "SnowPlowRawBad"

  kinesis_source_configuration {
    kinesis_stream_arn = "${aws_kinesis_stream.snowplow_raw_bad.arn}"
    role_arn           = "${aws_iam_role.snowplow_firehose_delivery_role.arn}"
  }

  // 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              = "raw-bad/"
    error_output_prefix = "raw-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"
  }
}