main.tf 3.1 KB

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