12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # creates a role and schedules a build for each server type
- #
- # Being polite aws users, we randomize the schedule over the hours of the early morning
- resource "random_integer" "hour" {
- min = 5 # Midnight ET
- max = 11 # 6am ET
- }
- resource "random_integer" "minute" {
- min = 0
- max = 59
- }
- resource "aws_cloudwatch_event_rule" "schedule_rule" {
- for_each = local.splunk_server_types
- name = "scheduled_build_${var.repository}_${each.value}"
- schedule_expression = "cron(${random_integer.minute.result} ${random_integer.hour.result} * * ? *)"
- }
- resource "aws_iam_role" "codebuild_role" {
- name_prefix = "splunk_apps_codebuild_role"
- path = "/aws_services/"
- assume_role_policy = <<EOF
- {
- "Version": "2012-10-17",
- "Statement": [
- {
- "Effect": "Allow",
- "Principal": {
- "Service": [
- "events.amazonaws.com",
- "codebuild.amazonaws.com"
- ]
- },
- "Action": "sts:AssumeRole"
- }
- ]
- }
- EOF
- }
- resource "aws_iam_policy" "codebuild_policy" {
- name_prefix = "splunk_apps_policy"
- path = "/aws_services/"
- policy = <<POLICY
- {
- "Version": "2012-10-17",
- "Statement": [
- {
- "Effect": "Allow",
- "Resource": [
- "arn:${var.aws_partition}:logs:${var.aws_region}:${var.aws_account_id}:log-group:/aws/codebuild/*"
- ],
- "Action": [
- "logs:CreateLogGroup",
- "logs:CreateLogStream",
- "logs:PutLogEvents"
- ]
- },
- {
- "Action": [
- "codebuild:StartBuild",
- "codebuild:StopBuild",
- "codebuild:BatchGet*",
- "codebuild:Get*",
- "codebuild:List*",
- "codecommit:GetBranch",
- "codecommit:GetCommit",
- "codecommit:GetRepository",
- "codecommit:ListBranches"
- ],
- "Effect": "Allow",
- "Resource": "*"
- }
- ]
- }
- POLICY
- }
- resource "aws_iam_policy_attachment" "service_role_attachment" {
- name = "splunk_apps_policy_attachment"
- policy_arn = aws_iam_policy.codebuild_policy.arn
- roles = [aws_iam_role.codebuild_role.id]
- }
- resource "aws_cloudwatch_event_target" "trigger_build" {
- for_each = local.splunk_server_types
- target_id = "trigger_build_${var.repository}_${each.value}"
- rule = aws_cloudwatch_event_rule.schedule_rule[each.value].name
- arn = aws_codebuild_project.this[each.value].id
- role_arn = aws_iam_role.codebuild_role.arn
- }
|