main.tf 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(var.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. resource "aws_ecr_repository" "this-server" {
  34. name = "portal_server"
  35. image_scanning_configuration {
  36. scan_on_push = true
  37. }
  38. }
  39. resource "aws_ecr_repository" "this-nginx" {
  40. name = "django_nginx"
  41. image_scanning_configuration {
  42. scan_on_push = true
  43. }
  44. }
  45. data "aws_iam_policy_document" "ecr_cross_account_policy" {
  46. statement {
  47. sid = "ECRWrite"
  48. effect = "Allow"
  49. actions = [
  50. "ecr:GetAuthorizationToken",
  51. "ecr:GetDownloadUrlForLayer",
  52. "ecr:BatchGetImage",
  53. "ecr:BatchCheckLayerAvailability",
  54. "ecr:PutImage",
  55. "ecr:InitiateLayerUpload",
  56. "ecr:UploadLayerPart",
  57. "ecr:CompleteLayerUpload",
  58. "ecr:DescribeRepositories",
  59. "ecr:ListImages",
  60. "ecr:DescribeImages",
  61. ]
  62. principals {
  63. identifiers = [for a in var.responsible_accounts[var.environment] : "arn:${var.aws_partition}:iam::${a}:root"]
  64. type = "AWS"
  65. }
  66. }
  67. }
  68. resource "aws_ecr_repository_policy" "this-server" {
  69. repository = aws_ecr_repository.this-server.name
  70. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  71. }
  72. resource "aws_ecr_lifecycle_policy" "this-server" {
  73. repository = aws_ecr_repository.this-server.name
  74. policy = file("${path.module}/lifecycle-policy.json")
  75. }
  76. resource "aws_ecr_repository_policy" "this-nginx" {
  77. repository = aws_ecr_repository.this-nginx.name
  78. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  79. }
  80. resource "aws_ecr_lifecycle_policy" "this-nginx" {
  81. repository = aws_ecr_repository.this-nginx.name
  82. policy = file("${path.module}/lifecycle-policy.json")
  83. }
  84. resource "aws_codebuild_webhook" "this" {
  85. project_name = var.name
  86. filter_group {
  87. filter {
  88. type = "EVENT"
  89. pattern = "PUSH"
  90. }
  91. filter {
  92. type = "HEAD_REF"
  93. pattern = "^refs\\/heads\\/release\\/.*$"
  94. }
  95. }
  96. depends_on = [aws_codebuild_project.this_no_artifact]
  97. }
  98. resource "github_repository_webhook" "this" {
  99. active = true
  100. events = ["push"]
  101. repository = data.github_repository.this.name
  102. configuration {
  103. url = aws_codebuild_webhook.this.payload_url
  104. secret = aws_codebuild_webhook.this.secret
  105. content_type = "json"
  106. insecure_ssl = false
  107. }
  108. }