iam.tf 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. resource "aws_iam_role" "codebuild_service_role" {
  2. name = "codebuild_${var.name}_role"
  3. path = "/aws_services/"
  4. assume_role_policy = <<EOF
  5. {
  6. "Version": "2012-10-17",
  7. "Statement": [
  8. {
  9. "Effect": "Allow",
  10. "Principal": {
  11. "Service": [
  12. "codebuild.amazonaws.com"
  13. ]
  14. },
  15. "Action": "sts:AssumeRole"
  16. }
  17. ]
  18. }
  19. EOF
  20. }
  21. resource "aws_iam_role_policy_attachment" "codebuild_service_policy_attach" {
  22. role = aws_iam_role.codebuild_service_role.name
  23. policy_arn = aws_iam_policy.codebuild_service_policy.arn
  24. }
  25. # Some things about this policy I'm not perfectly sure about, like
  26. # should the account number be hardcoded? Also, it reads like we'll have to
  27. # update it each time we have a new repository added to codecommit - that
  28. # or we'll need to authorize the codebuild role to be able to pull from any
  29. # codecommit repo. Which may be fine?
  30. resource "aws_iam_policy" "codebuild_service_policy" {
  31. name = "codebuild_${var.name}_policy"
  32. description = "Policy for AWS codebuild for ${var.name}"
  33. path = "/aws_services/"
  34. policy = <<EOF
  35. {
  36. "Version": "2012-10-17",
  37. "Statement": [
  38. {
  39. "Effect": "Allow",
  40. "Resource": [
  41. "arn:${var.aws_partition}:logs:${var.aws_region}:${var.aws_account_id}:log-group:/aws/codebuild/*"
  42. ],
  43. "Action": [
  44. "logs:CreateLogGroup",
  45. "logs:CreateLogStream",
  46. "logs:PutLogEvents"
  47. ]
  48. },
  49. {
  50. "Effect": "Allow",
  51. "Resource": [
  52. "arn:${var.aws_partition}:s3:::codepipeline-${var.aws_region}-*"
  53. ],
  54. "Action": [
  55. "s3:PutObject",
  56. "s3:GetObject",
  57. "s3:GetObjectVersion"
  58. ]
  59. },
  60. {
  61. "Effect": "Allow",
  62. "Resource": [
  63. "arn:${var.aws_partition}:lambda:${var.aws_region}:${var.aws_account_id}:function:portal_*"
  64. ],
  65. "Action": [
  66. "lambda:UpdateFunctionCode"
  67. ]
  68. },
  69. {
  70. "Effect": "Allow",
  71. "Resource": [
  72. "arn:${var.aws_partition}:codecommit:${var.aws_region}:${var.aws_account_id}:*"
  73. ],
  74. "Action": [
  75. "codecommit:GitPull"
  76. ]
  77. },
  78. {
  79. "Effect": "Allow",
  80. "Resource": [
  81. "arn:${var.aws_partition}:s3:::xdr-${var.environment}-codebuild-${var.name}/*",
  82. "arn:${var.aws_partition}:s3:::*"
  83. ],
  84. "Action": [
  85. "s3:PutObject",
  86. "s3:GetObject*",
  87. "s3:ListBucket",
  88. "s3:DeleteObject"
  89. ]
  90. },
  91. {
  92. "Sid": "WriteToECR",
  93. "Effect": "Allow",
  94. "Resource": [
  95. "*"
  96. ],
  97. "Action": [
  98. "ecr:GetAuthorizationToken",
  99. "ecr:BatchCheckLayerAvailability",
  100. "ecr:CompleteLayerUpload",
  101. "ecr:GetAuthorizationToken",
  102. "ecr:InitiateLayerUpload",
  103. "ecr:PutImage",
  104. "ecr:UploadLayerPart"
  105. ]
  106. },
  107. {
  108. "Sid": "PullFromECR",
  109. "Effect": "Allow",
  110. "Resource": [
  111. "*"
  112. ],
  113. "Action": [
  114. "ecr:GetDownloadUrlForLayer",
  115. "ecr:BatchGetImage",
  116. "ecr:BatchCheckLayerAvailability"
  117. ]
  118. }
  119. ]
  120. }
  121. EOF
  122. }