sitespeed.tf 3.92 KB
Newer Older
1 2
# Configure the AWS Provider
provider "aws" {
3 4 5
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region = "${var.aws_region}"
6 7
}

8 9 10 11
# Create a new IAM user
resource "aws_iam_user" "build_pipeline_user" {
  name = "build_pipeline_user"
}
12

13 14 15
# Create IAM access key for the new user
resource "aws_iam_access_key" "build_pipeline_user_key" {
  user = "${aws_iam_user.build_pipeline_user.name}"
16 17
}

18 19
# Create the SNS topics
resource "aws_sns_topic" "provision-topic" {
Ben Patterson committed
20
  name = "edx-pipeline-provision-topic"
21
}
22 23
resource "aws_sns_topic" "sitespeed-topic" {
  name = "edx-pipeline-sitespeed-topic"
24 25
}

26 27 28 29
# Create the SQS queues, including giving permission to
# the SNS topics to send messages to the queue
resource "aws_sqs_queue" "provision-queue" {
  name = "${var.provision_queue_name}"
30 31 32 33
  delay_seconds = "${var.queue_delay_seconds}"
  max_message_size = "${var.queue_max_message_size}"
  message_retention_seconds = "${var.queue_message_retention_seconds}"
  receive_wait_time_seconds = "${var.queue_receive_wait_time_seconds}"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  policy = <<EOF
{
  "Version":"2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "SQS:SendMessage",
      "Principal": "*",
      "Resource": "${format("arn:aws:sqs:%s:%s:%s", var.aws_region, var.aws_account_id, var.provision_queue_name)}",
      "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "${aws_sns_topic.provision-topic.arn}"
        }
      }
    }
  ]
50
}
51
EOF
52 53
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
resource "aws_sqs_queue" "sitespeed-queue" {
  name = "${var.sitespeed_queue_name}"
  delay_seconds = "${var.queue_delay_seconds}"
  max_message_size = "${var.queue_max_message_size}"
  message_retention_seconds = "${var.queue_message_retention_seconds}"
  receive_wait_time_seconds = "${var.queue_receive_wait_time_seconds}"
  policy = <<EOF
{
  "Version":"2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "SQS:SendMessage",
      "Principal": "*",
      "Resource": "${format("arn:aws:sqs:%s:%s:%s", var.aws_region, var.aws_account_id, var.sitespeed_queue_name)}",
      "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "${aws_sns_topic.sitespeed-topic.arn}"
        }
      }
    }
  ]
76
}
77
EOF
78 79
}

80 81 82 83 84 85 86 87 88 89
# Subscribe the SQS queues to the SNS topics
resource "aws_sns_topic_subscription" "provision-subscription" {
  topic_arn = "${aws_sns_topic.provision-topic.arn}"
  protocol  = "sqs"
  endpoint  = "${aws_sqs_queue.provision-queue.arn}"
}
resource "aws_sns_topic_subscription" "sitespeed-subscription" {
  topic_arn = "${aws_sns_topic.sitespeed-topic.arn}"
  protocol  = "sqs"
  endpoint  = "${aws_sqs_queue.sitespeed-queue.arn}"
90 91
}

92 93 94 95 96 97 98 99
# Allow the IAM user to publish to the SNS topics
# and to read and delete from the SQS queues.
# Jenkins and the build-trigger heroku app will be
# configured to use its key.
resource "aws_iam_user_policy" "user-pipeline-policy" {
  name = "${var.environment}-${var.deployment}-${var.service}-sender"
  user = "${aws_iam_user.build_pipeline_user.name}"
  policy = <<EOF
100 101 102 103
{
  "Version": "2012-10-17",
  "Statement": [
    {
104
      "Effect": "Allow",
105 106 107
      "Action": [
        "sns:Publish"
      ],
108 109 110
      "Resource": "${aws_sns_topic.provision-topic.arn}"
    },
    {
111
      "Effect": "Allow",
112 113 114 115 116
      "Action": [
        "sqs:ReceiveMessage",
        "sqs:DeleteMessage"
      ],
      "Resource": "${aws_sqs_queue.provision-queue.arn}"
117 118
    },
    {
119
      "Effect": "Allow",
120 121 122
      "Action": [
        "sns:Publish"
      ],
123 124 125
      "Resource": "${aws_sns_topic.sitespeed-topic.arn}"
    },
    {
126
      "Effect": "Allow",
127 128 129 130 131
      "Action": [
        "sqs:ReceiveMessage",
        "sqs:DeleteMessage"
      ],
      "Resource": "${aws_sqs_queue.sitespeed-queue.arn}"
132 133 134 135 136 137
    }
  ]
}
EOF
}

138 139 140 141 142 143 144 145
# Output the AWS key and secret for the new user to the console.
# Note that it will also be available in the terraform.tfstate file.
output "key" {
    value = "${aws_iam_access_key.build_pipeline_user_key.id}"
}
output "secret" {
    value = "${aws_iam_access_key.build_pipeline_user_key.secret}"
}