main.tf 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_submodules_config {
  16. fetch_submodules = false
  17. }
  18. }
  19. environment {
  20. compute_type = "BUILD_GENERAL1_SMALL"
  21. image = var.codebuild_image
  22. type = "LINUX_CONTAINER"
  23. privileged_mode = true
  24. }
  25. artifacts {
  26. type = "NO_ARTIFACTS"
  27. }
  28. tags = merge(local.standard_tags, var.tags)
  29. # Govcloud incompatible with "project visibility"
  30. # See https://github.com/hashicorp/terraform-provider-aws/issues/22473#issuecomment-1081187035
  31. lifecycle { ignore_changes = [project_visibility] }
  32. }
  33. # image_tag_mutability = "IMMUTABLE"
  34. # MSOCI-2182 - This breaks the push process for new changes to the portal servers.
  35. # The codebuild code depends on being able to tag a new image with the latest tag.
  36. # tfsec:ignore:aws-ecr-enforce-immutable-repository Allow mutable tags for now - TO-DO
  37. resource "aws_ecr_repository" "this-server" {
  38. # checkov:skip=CKV_AWS_136: Risk is low for AES-256 encryption
  39. # checkov:skip=CKV_AWS_51: see tfsec explanation above
  40. name = "portal_server"
  41. image_scanning_configuration {
  42. scan_on_push = true
  43. }
  44. # tfsec:ignore:aws-ecr-repository-customer-key Risk is low for AES-256 encryption
  45. encryption_configuration {
  46. encryption_type = "AES256"
  47. }
  48. }
  49. # image_tag_mutability = "IMMUTABLE"
  50. # MSOCI-2182 - This breaks the push process for new changes to the portal servers.
  51. # The codebuild code depends on being able to tag a new image with the latest tag.
  52. # tfsec:ignore:aws-ecr-enforce-immutable-repository Allow mutable tags for now - TO-DO
  53. resource "aws_ecr_repository" "this-nginx" {
  54. # checkov:skip=CKV_AWS_136: Risk is low for AES-256 encryption
  55. # checkov:skip=CKV_AWS_51: see tfsec explanation above
  56. name = "django_nginx"
  57. image_scanning_configuration {
  58. scan_on_push = true
  59. }
  60. # tfsec:ignore:aws-ecr-repository-customer-key Risk is low for AES-256 encryption
  61. encryption_configuration {
  62. encryption_type = "AES256"
  63. }
  64. }
  65. data "aws_iam_policy_document" "ecr_cross_account_policy" {
  66. statement {
  67. sid = "ECRWrite"
  68. effect = "Allow"
  69. actions = [
  70. "ecr:GetAuthorizationToken",
  71. "ecr:GetDownloadUrlForLayer",
  72. "ecr:BatchGetImage",
  73. "ecr:BatchCheckLayerAvailability",
  74. "ecr:PutImage",
  75. "ecr:InitiateLayerUpload",
  76. "ecr:UploadLayerPart",
  77. "ecr:CompleteLayerUpload",
  78. "ecr:DescribeRepositories",
  79. "ecr:ListImages",
  80. "ecr:DescribeImages",
  81. ]
  82. principals {
  83. identifiers = [for a in local.responsible_accounts[var.environment] : "arn:${var.aws_partition}:iam::${a}:root"]
  84. type = "AWS"
  85. }
  86. }
  87. }
  88. resource "aws_ecr_repository_policy" "this-server" {
  89. repository = aws_ecr_repository.this-server.name
  90. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  91. }
  92. resource "aws_ecr_lifecycle_policy" "this-server" {
  93. repository = aws_ecr_repository.this-server.name
  94. policy = file("${path.module}/lifecycle-policy.json")
  95. }
  96. resource "aws_ecr_repository_policy" "this-nginx" {
  97. repository = aws_ecr_repository.this-nginx.name
  98. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  99. }
  100. resource "aws_ecr_lifecycle_policy" "this-nginx" {
  101. repository = aws_ecr_repository.this-nginx.name
  102. policy = file("${path.module}/lifecycle-policy.json")
  103. }
  104. resource "aws_codebuild_webhook" "this" {
  105. project_name = var.name
  106. filter_group {
  107. filter {
  108. type = "EVENT"
  109. pattern = "PUSH"
  110. }
  111. filter {
  112. type = "HEAD_REF"
  113. pattern = "^refs\\/heads\\/release\\/.*$"
  114. }
  115. }
  116. depends_on = [aws_codebuild_project.this_no_artifact]
  117. }
  118. resource "github_repository_webhook" "this" {
  119. active = true
  120. events = ["push"]
  121. repository = data.github_repository.this.name
  122. configuration {
  123. url = aws_codebuild_webhook.this.payload_url
  124. secret = aws_codebuild_webhook.this.secret
  125. content_type = "json"
  126. insecure_ssl = false
  127. }
  128. }