ecr.tf 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. locals {
  2. registries = [
  3. "portal_server",
  4. "django_nginx",
  5. ]
  6. }
  7. data "aws_vpc_endpoint_service" "ecr_api_endpoint" {
  8. service = "ecr.api"
  9. }
  10. data "aws_vpc_endpoint_service" "ecr_dkr_endpoint" {
  11. service = "ecr.dkr"
  12. }
  13. resource "aws_iam_instance_profile" "portal_server_instance_profile" {
  14. name = "portal_server-instance-profile"
  15. role = aws_iam_role.portal_server.name
  16. }
  17. resource "aws_iam_role" "portal_server" {
  18. name = "portal-instance-role"
  19. assume_role_policy = <<EOF
  20. {
  21. "Version": "2012-10-17",
  22. "Statement": [
  23. {
  24. "Sid": "",
  25. "Effect": "Allow",
  26. "Principal": {
  27. "Service": [
  28. "ec2.amazonaws.com",
  29. "ssm.amazonaws.com"
  30. ]
  31. },
  32. "Action": "sts:AssumeRole"
  33. }
  34. ]
  35. }
  36. EOF
  37. }
  38. data "aws_iam_policy_document" "portal_server_ecr_policy" {
  39. statement {
  40. actions = [
  41. "ecr:GetAuthorizationToken",
  42. ]
  43. resources = ["*"]
  44. }
  45. statement {
  46. sid = "AllowCommunicationECR"
  47. effect = "Allow"
  48. actions = [
  49. "ecr:BatchCheckLayerAvailability",
  50. "ecr:GetDownloadUrlForLayer",
  51. "ecr:GetRepositoryPolicy",
  52. "ecr:DescribeRepositories",
  53. "ecr:ListImages",
  54. "ecr:DescribeImages",
  55. "ecr:BatchGetImage",
  56. "ecr:InitiateLayerUpload",
  57. "ecr:UploadLayerPart",
  58. "ecr:CompleteLayerUpload",
  59. "ecr:PutImage"
  60. ]
  61. resources = [
  62. "arn:${var.aws_partition}:ecr:${var.aws_region}:${var.common_services_account}:repository/portal_server",
  63. "arn:${var.aws_partition}:ecr:${var.aws_region}:${var.common_services_account}:repository/django_nginx"
  64. ]
  65. }
  66. statement {
  67. sid = "Tags"
  68. effect = "Allow"
  69. actions = [
  70. "ec2:DescribeTags",
  71. "ec2:DescribeInstances"
  72. ]
  73. resources = [
  74. "*"
  75. ]
  76. }
  77. }
  78. resource "aws_iam_policy" "portal_server_ecr_policy" {
  79. name = "portal_server_ecr"
  80. path = "/"
  81. policy = data.aws_iam_policy_document.portal_server_ecr_policy.json
  82. }
  83. resource "aws_iam_role_policy_attachment" "portal_server_ecr" {
  84. role = aws_iam_role.portal_server.name
  85. policy_arn = aws_iam_policy.portal_server_ecr_policy.arn
  86. }
  87. data "aws_iam_policy" "default_instance_policy_s3_binaries" {
  88. name = "default_instance_s3_binaries"
  89. path_prefix = "/launchroles/"
  90. }
  91. resource "aws_iam_role_policy_attachment" "portal_server_s3_binaries" {
  92. role = aws_iam_role.portal_server.name
  93. policy_arn = data.aws_iam_policy.default_instance_policy_s3_binaries.arn
  94. }
  95. # Assume Role Policy -- Needed for S3 Bucket Access in Cross-Accounts
  96. data "aws_iam_policy_document" "portal_server_assumerole" {
  97. statement {
  98. actions = [
  99. "sts:AssumeRole"
  100. ]
  101. resources = [
  102. "arn:${var.aws_partition}:iam::*:role/service/xdr-${var.environment}-portal-shared-artifacts",
  103. "arn:${var.aws_partition}:iam::*:role/service/xdr-${var.environment}-*-portal-customer-artifacts",
  104. ]
  105. }
  106. }
  107. resource "aws_iam_policy" "portal_server_assumerole_policy" {
  108. name = "portal_server_assumerole"
  109. path = "/launchroles/"
  110. policy = data.aws_iam_policy_document.portal_server_assumerole.json
  111. }
  112. resource "aws_iam_role_policy_attachment" "portal_server_assumerole" {
  113. role = aws_iam_role.portal_server.name
  114. policy_arn = aws_iam_policy.portal_server_assumerole_policy.arn
  115. }