ecr_repo.tf 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # tfsec:ignore:aws-ecr-enforce-immutable-repository
  2. resource "aws_ecr_repository" "this" {
  3. # checkov:skip=CKV_AWS_51: see tfsec explanation above
  4. name = var.name
  5. tags = merge(var.standard_tags, var.tags)
  6. # image_tag_mutability = "IMMUTABLE"
  7. # Allow mutable tags for now - TO-DO
  8. # MSOCI-2182 - This breaks the push process for new changes to the portal servers.
  9. # The codebuild code depends on being able to tag a new image with the latest tag.
  10. image_scanning_configuration {
  11. scan_on_push = true
  12. }
  13. # tfsec:ignore:aws-ecr-repository-customer-key Risk is low for KMS AES-256 encryption
  14. encryption_configuration {
  15. encryption_type = "AES256"
  16. }
  17. }
  18. data "aws_iam_policy_document" "ecr_repository_policy" {
  19. statement {
  20. sid = "LetCodebuildServiceUseTheseImages"
  21. effect = "Allow"
  22. principals {
  23. type = "Service"
  24. identifiers = ["codebuild.amazonaws.com"]
  25. }
  26. actions = [
  27. "ecr:GetDownloadUrlForLayer",
  28. "ecr:BatchGetImage",
  29. "ecr:BatchCheckLayerAvailability"
  30. ]
  31. }
  32. statement {
  33. sid = "LetCodebuildIAMRolePushImagesHere"
  34. effect = "Allow"
  35. principals {
  36. type = "AWS"
  37. identifiers = [var.codebuild_assume_role_arn]
  38. }
  39. actions = [
  40. "ecr:BatchCheckLayerAvailability",
  41. "ecr:BatchGetImage",
  42. "ecr:CompleteLayerUpload",
  43. "ecr:DescribeImages",
  44. "ecr:DescribeRepositories",
  45. "ecr:GetAuthorizationToken",
  46. "ecr:GetDownloadUrlForLayer",
  47. "ecr:InitiateLayerUpload",
  48. "ecr:ListImages",
  49. "ecr:PutImage",
  50. "ecr:UploadLayerPart",
  51. ]
  52. }
  53. }
  54. #Allow codebuild to access the ECR Repository to use the images
  55. resource "aws_ecr_repository_policy" "this" {
  56. repository = aws_ecr_repository.this.name
  57. policy = data.aws_iam_policy_document.ecr_repository_policy.json
  58. }