main.tf 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  16. environment {
  17. compute_type = "BUILD_GENERAL1_SMALL"
  18. image = var.codebuild_image
  19. type = "LINUX_CONTAINER"
  20. privileged_mode = true
  21. }
  22. artifacts {
  23. type = "NO_ARTIFACTS"
  24. }
  25. tags = merge(var.standard_tags, var.tags)
  26. }
  27. resource "aws_ecr_repository" "this-api" {
  28. name = "portal-api"
  29. }
  30. resource "aws_ecr_repository" "this-nginx" {
  31. name = "portal-nginx"
  32. }
  33. data "aws_iam_policy_document" "ecr_cross_account_policy" {
  34. statement {
  35. sid = "ECRWrite"
  36. effect = "Allow"
  37. actions = [
  38. "ecr:GetAuthorizationToken",
  39. "ecr:GetDownloadUrlForLayer",
  40. "ecr:BatchGetImage",
  41. "ecr:BatchCheckLayerAvailability",
  42. "ecr:PutImage",
  43. "ecr:InitiateLayerUpload",
  44. "ecr:UploadLayerPart",
  45. "ecr:CompleteLayerUpload",
  46. "ecr:DescribeRepositories",
  47. "ecr:ListImages",
  48. "ecr:DescribeImages",
  49. ]
  50. principals {
  51. identifiers = [ for a in var.responsible_accounts[var.environment]: "arn:${var.aws_partition}:iam::${a}:root" ]
  52. type = "AWS"
  53. }
  54. }
  55. }
  56. resource "aws_ecr_repository_policy" "this-api" {
  57. repository = aws_ecr_repository.this-api.name
  58. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  59. }
  60. resource "aws_ecr_lifecycle_policy" "this-api" {
  61. repository = aws_ecr_repository.this-api.name
  62. policy = file("${path.module}/lifecycle-policy.json")
  63. }
  64. resource "aws_ecr_repository_policy" "this-nginx" {
  65. repository = aws_ecr_repository.this-nginx.name
  66. policy = data.aws_iam_policy_document.ecr_cross_account_policy.json
  67. }
  68. resource "aws_ecr_lifecycle_policy" "this-nginx" {
  69. repository = aws_ecr_repository.this-nginx.name
  70. policy = file("${path.module}/lifecycle-policy.json")
  71. }
  72. resource "aws_codebuild_webhook" "this" {
  73. project_name = var.name
  74. branch_filter = var.webhook_branch_filter
  75. depends_on = [ aws_codebuild_project.this_no_artifact ]
  76. }
  77. resource "github_repository_webhook" "this" {
  78. active = true
  79. events = ["push"]
  80. repository = data.github_repository.this.name
  81. configuration {
  82. url = aws_codebuild_webhook.this.payload_url
  83. secret = aws_codebuild_webhook.this.secret
  84. content_type = "json"
  85. insecure_ssl = false
  86. }
  87. }