main.tf 8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Configure remote state
terraform {
  backend "s3" {}
}

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

12 13
// Policies
resource "aws_iam_policy" "snowplow_collector_policy" {
Cameron McFarland's avatar
Cameron McFarland committed
14
  description = "Policy the allows the collector to access other AWS services such as Kinesis."
15 16
  name        = "snowplow-collector-policy"
  path        = "/"
Cameron McFarland's avatar
Cameron McFarland committed
17 18

  policy = <<EOF
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
{
    "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
48 49 50 51 52
  description = ""
  name        = "snowplow-enricher-policy"
  path        = "/"

  policy = <<EOF
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
{
    "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
92 93 94 95 96
  description = "Policy to provide AWS access to snowplow s3loader instances such as S3, DynamoDB, and Kinesis."
  name        = "snowplow-s3loader-policy"
  path        = "/"

  policy = <<EOF
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
{
    "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
}

145 146
// VPC
resource "aws_vpc" "snowplow_vpc" {
Cameron McFarland's avatar
Cameron McFarland committed
147 148
  cidr_block = "10.32.0.0/16"

149
  tags = {
Cameron McFarland's avatar
Cameron McFarland committed
150
    Name        = "SnowPlow VPC"
151 152 153 154 155
    environment = "SnowPlow"
  }
}

// Subnet
156
resource "aws_subnet" "snowplow_subnet_1" {
Cameron McFarland's avatar
Cameron McFarland committed
157 158
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.2.0/24"
159
  availability_zone = "us-east-1a"
Cameron McFarland's avatar
Cameron McFarland committed
160

161
  tags = {
Cameron McFarland's avatar
Cameron McFarland committed
162
    Name        = "SnowPlow Subnet 1"
163 164 165
    environment = "SnowPlow"
  }
}
166

167
resource "aws_subnet" "snowplow_subnet_2" {
Cameron McFarland's avatar
Cameron McFarland committed
168 169
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.1.0/24"
170
  availability_zone = "us-east-1e"
Cameron McFarland's avatar
Cameron McFarland committed
171

172
  tags = {
Cameron McFarland's avatar
Cameron McFarland committed
173
    Name        = "SnowPlow Subnet 2"
174 175 176 177
    environment = "SnowPlow"
  }
}

178
resource "aws_subnet" "snowplow_subnet_3" {
Cameron McFarland's avatar
Cameron McFarland committed
179 180
  vpc_id            = "${aws_vpc.snowplow_vpc.id}"
  cidr_block        = "10.32.0.0/24"
181
  availability_zone = "us-east-1b"
Cameron McFarland's avatar
Cameron McFarland committed
182

183
  tags = {
Cameron McFarland's avatar
Cameron McFarland committed
184
    Name        = "SnowPlow Subnet 3"
185 186 187 188
    environment = "SnowPlow"
  }
}

189 190 191 192
// Internet Gateway
resource "aws_internet_gateway" "snowplow_gw" {
  vpc_id = "${aws_vpc.snowplow_vpc.id}"

193
  tags = {
Cameron McFarland's avatar
Cameron McFarland committed
194
    Name        = "SnowPlow Gateway"
195 196 197 198
    environment = "SnowPlow"
  }
}

199 200 201 202
// Routing Tables
resource "aws_route_table" "snowplow_route_table" {
  vpc_id = "${aws_vpc.snowplow_vpc.id}"

203
  tags = {
Cameron McFarland's avatar
Cameron McFarland committed
204
    Name        = "SnowPlow Routing Table"
205 206 207
    environment = "SnowPlow"
  }
}
208

Cameron McFarland's avatar
Cameron McFarland committed
209 210 211 212 213 214
resource "aws_main_route_table_association" "snowplow_main_route_table_association" {
  vpc_id         = "${aws_vpc.snowplow_vpc.id}"
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
}

resource "aws_route" "snowplow_route" {
Cameron McFarland's avatar
Cameron McFarland committed
215
  route_table_id         = "${aws_route_table.snowplow_route_table.id}"
Cameron McFarland's avatar
Cameron McFarland committed
216
  destination_cidr_block = "0.0.0.0/0"
Cameron McFarland's avatar
Cameron McFarland committed
217
  gateway_id             = "${aws_internet_gateway.snowplow_gw.id}"
Cameron McFarland's avatar
Cameron McFarland committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}

resource "aws_route_table_association" "snowplow_route_table_association_1" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
  subnet_id      = "${aws_subnet.snowplow_subnet_1.id}"
}

resource "aws_route_table_association" "snowplow_route_table_association_2" {
  route_table_id = "${aws_route_table.snowplow_route_table.id}"
  subnet_id      = "${aws_subnet.snowplow_subnet_2.id}"
}

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

235
// Kinesis Streams
236 237
resource "aws_kinesis_stream" "snowplow_raw_good" {
  name             = "snowplow-raw-good"
238 239
  shard_count      = 1
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
240

241 242 243 244
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]
245

246 247 248 249 250 251 252 253 254
  tags = {
    environment = "SnowPlow"
  }
}

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

256 257 258 259 260 261 262 263 264 265
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}

266 267
resource "aws_kinesis_stream" "snowplow_enriched_bad" {
  name             = "snowplow-enriched-bad"
268 269
  shard_count      = 1
  retention_period = 48
Cameron McFarland's avatar
Cameron McFarland committed
270

271 272 273 274
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]
275

276 277 278 279 280 281 282 283 284
  tags = {
    environment = "SnowPlow"
  }
}

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

286 287 288 289 290 291 292 293 294 295 296 297 298 299
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}

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

301 302 303 304 305 306 307 308 309
  shard_level_metrics = [
    "IncomingBytes",
    "OutgoingBytes",
  ]

  tags = {
    environment = "SnowPlow"
  }
}
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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

// EC2 Launch Configs
//resource "aws_launch_configuration" "snowplow_collector_launch_config" {
//}

// EC2 Auto Scaling Groups
resource "aws_autoscaling_group" "snowplow_collector_autoscaling_group" {
  launch_configuration = "SnowPlowCollector-v3"
  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" {
  launch_configuration = "SnowPlowEnricher-v2"
  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" {
  launch_configuration = "SnowPlowS3Loader-v1"
  max_size             = "1"
  min_size             = "1"

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

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