cloudwatch.tf 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # creates a role and schedules a build for each server type
  2. #
  3. # Being polite aws users, we randomize the schedule over the hours of the early morning
  4. resource "random_integer" "hour" {
  5. min = 5 # Midnight ET
  6. max = 11 # 6am ET
  7. }
  8. resource "random_integer" "minute" {
  9. min = 0
  10. max = 59
  11. }
  12. resource "aws_cloudwatch_event_rule" "schedule_rule" {
  13. name = "scheduled_build_docs_${var.repository}"
  14. schedule_expression = "cron(${random_integer.minute.result} ${random_integer.hour.result} * * ? *)"
  15. }
  16. resource "aws_iam_role" "codebuild_role" {
  17. name_prefix = "splunk_docs_codebuild_role"
  18. path = "/aws_services/"
  19. assume_role_policy = <<EOF
  20. {
  21. "Version": "2012-10-17",
  22. "Statement": [
  23. {
  24. "Effect": "Allow",
  25. "Principal": {
  26. "Service": [
  27. "events.amazonaws.com",
  28. "codebuild.amazonaws.com"
  29. ]
  30. },
  31. "Action": "sts:AssumeRole"
  32. }
  33. ]
  34. }
  35. EOF
  36. }
  37. resource "aws_iam_policy" "codebuild_policy" {
  38. name_prefix = "splunk_docs_policy"
  39. path = "/aws_services/"
  40. policy = <<POLICY
  41. {
  42. "Version": "2012-10-17",
  43. "Statement": [
  44. {
  45. "Effect": "Allow",
  46. "Resource": [
  47. "arn:${var.aws_partition}:logs:${var.aws_region}:${var.aws_account_id}:log-group:/aws/codebuild/*"
  48. ],
  49. "Action": [
  50. "logs:CreateLogGroup",
  51. "logs:CreateLogStream",
  52. "logs:PutLogEvents"
  53. ]
  54. },
  55. {
  56. "Action": [
  57. "codebuild:StartBuild",
  58. "codebuild:StopBuild",
  59. "codebuild:BatchGet*",
  60. "codebuild:Get*",
  61. "codebuild:List*",
  62. "codecommit:GetBranch",
  63. "codecommit:GetCommit",
  64. "codecommit:GetRepository",
  65. "codecommit:ListBranches"
  66. ],
  67. "Effect": "Allow",
  68. "Resource": "*"
  69. }
  70. ]
  71. }
  72. POLICY
  73. }
  74. resource "aws_iam_policy_attachment" "service_role_attachment" {
  75. name = "splunk_docs_policy_attachment"
  76. policy_arn = aws_iam_policy.codebuild_policy.arn
  77. roles = [aws_iam_role.codebuild_role.id]
  78. }
  79. resource "aws_cloudwatch_event_target" "trigger_build" {
  80. target_id = "trigger_build_docs_${var.repository}"
  81. rule = aws_cloudwatch_event_rule.schedule_rule.name
  82. arn = aws_codebuild_project.this.id
  83. role_arn = aws_iam_role.codebuild_role.arn
  84. }