main.tf 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. data "github_repository" "this" {
  2. name = var.name
  3. }
  4. resource "aws_codebuild_project" "this_no_artifact" {
  5. count = var.artifact_s3_bucket == "" ? 1 : 0
  6. name = var.name
  7. description = "Container for ${var.name}"
  8. service_role = var.service_role
  9. encryption_key = var.kms_key
  10. badge_enabled = var.badge_enabled
  11. source {
  12. type = "GITHUB_ENTERPRISE"
  13. location = data.github_repository.this.http_clone_url
  14. report_build_status = true
  15. git_clone_depth = 1
  16. git_submodules_config {
  17. fetch_submodules = var.fetch_submodules
  18. }
  19. }
  20. source_version = var.source_version
  21. environment {
  22. compute_type = "BUILD_GENERAL1_SMALL"
  23. image = var.codebuild_image
  24. type = "LINUX_CONTAINER"
  25. privileged_mode = true
  26. }
  27. artifacts {
  28. type = "NO_ARTIFACTS"
  29. }
  30. tags = merge(local.standard_tags, var.tags)
  31. # Govcloud incompatible with "project visibility"
  32. # See https://github.com/hashicorp/terraform-provider-aws/issues/22473#issuecomment-1081187035
  33. lifecycle { ignore_changes = [project_visibility] }
  34. }
  35. # image_tag_mutability = "IMMUTABLE"
  36. # MSOCI-2182 - This breaks the push process for new changes to the portal servers.
  37. # The codebuild code depends on being able to tag a new image with the latest tag.
  38. # tfsec:ignore:aws-ecr-enforce-immutable-repository Allow mutable tags for now - TO-DO
  39. resource "aws_ecr_repository" "this" {
  40. # checkov:skip=CKV_AWS_136: Risk is low for AES-256 encryption
  41. # checkov:skip=CKV_AWS_51: see tfsec explanation above
  42. name = var.name
  43. image_scanning_configuration {
  44. scan_on_push = true
  45. }
  46. # tfsec:ignore:aws-ecr-repository-customer-key Risk is low for AES-256 encryption
  47. encryption_configuration {
  48. encryption_type = "AES256"
  49. }
  50. }
  51. data "aws_iam_policy_document" "ecr_cross_account_policy" {
  52. statement {
  53. sid = "ECRWrite"
  54. effect = "Allow"
  55. actions = [
  56. "ecr:GetAuthorizationToken",
  57. "ecr:GetDownloadUrlForLayer",
  58. "ecr:BatchGetImage",
  59. "ecr:BatchCheckLayerAvailability",
  60. "ecr:PutImage",
  61. "ecr:InitiateLayerUpload",
  62. "ecr:UploadLayerPart",
  63. "ecr:CompleteLayerUpload",
  64. "ecr:DescribeRepositories",
  65. "ecr:ListImages",
  66. "ecr:DescribeImages",
  67. ]
  68. principals {
  69. type = "AWS"
  70. identifiers = sort([for a in local.responsible_accounts[var.environment] : "arn:${var.aws_partition}:iam::${a}:root"])
  71. }
  72. }
  73. # Allow codebuild access
  74. statement {
  75. sid = "CodeBuildAccessPrincipal"
  76. effect = "Allow"
  77. actions = [
  78. "ecr:GetDownloadUrlForLayer",
  79. "ecr:BatchGetImage",
  80. "ecr:BatchCheckLayerAvailability",
  81. ]
  82. principals {
  83. type = "Service"
  84. identifiers = ["codebuild.amazonaws.com"]
  85. }
  86. }
  87. }
  88. resource "aws_ecr_repository_policy" "this" {
  89. repository = aws_ecr_repository.this.name
  90. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  91. }
  92. resource "aws_ecr_lifecycle_policy" "this" {
  93. repository = aws_ecr_repository.this.name
  94. policy = file("${path.module}/default-lifecycle-policy.json")
  95. }
  96. resource "aws_codebuild_webhook" "this" {
  97. project_name = var.name
  98. branch_filter = var.webhook_branch_filter
  99. depends_on = [aws_codebuild_project.this_no_artifact]
  100. }
  101. resource "github_repository_webhook" "this" {
  102. count = var.enable_webhooks ? 1 : 0
  103. active = true
  104. events = ["push"]
  105. repository = data.github_repository.this.name
  106. configuration {
  107. url = aws_codebuild_webhook.this.payload_url
  108. secret = aws_codebuild_webhook.this.secret
  109. content_type = "json"
  110. insecure_ssl = false
  111. }
  112. }