instance_profile.tf 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #############################
  2. # GitHub Enterprise instance profile
  3. #
  4. # Includes policies for GitHub Enterprise:
  5. # * Same policies as the default instance profile
  6. resource "aws_iam_instance_profile" "github_instance_profile" {
  7. name = "xdr-github-instance-profile"
  8. path = "/instance/"
  9. role = aws_iam_role.github_instance_role.name
  10. }
  11. resource "aws_iam_role" "github_instance_role" {
  12. name = "xdr-github-instance-role"
  13. path = "/instance/"
  14. assume_role_policy = <<EOF
  15. {
  16. "Version": "2012-10-17",
  17. "Statement": [
  18. {
  19. "Sid": "",
  20. "Effect": "Allow",
  21. "Principal": {
  22. "Service": [
  23. "ec2.amazonaws.com",
  24. "ssm.amazonaws.com"
  25. ]
  26. },
  27. "Action": "sts:AssumeRole"
  28. }
  29. ]
  30. }
  31. EOF
  32. }
  33. # These 3 are the default profile attachments:
  34. resource "aws_iam_role_policy_attachment" "github_instance_AmazonEC2RoleforSSM" {
  35. role = aws_iam_role.github_instance_role.name
  36. policy_arn = "arn:${var.aws_partition}:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
  37. }
  38. resource "aws_iam_role_policy_attachment" "github_instance_default_policy_attach" {
  39. role = aws_iam_role.github_instance_role.name
  40. policy_arn = "arn:${var.aws_partition}:iam::${var.aws_account_id}:policy/launchroles/default_instance_tag_read"
  41. }
  42. resource "aws_iam_role_policy_attachment" "github_instance_cloudwatch_policy_attach" {
  43. role = aws_iam_role.github_instance_role.name
  44. policy_arn = "arn:${var.aws_partition}:iam::${var.aws_account_id}:policy/cloudwatch_events"
  45. }
  46. # GitHub Enterprise Specific Policy
  47. resource "aws_iam_policy" "github_instance_policy" {
  48. name = "github_instance_policy"
  49. path = "/launchroles/"
  50. description = "This policy allows github-specific functions"
  51. policy = data.aws_iam_policy_document.github_instance_policy_doc.json
  52. }
  53. data "aws_iam_policy_document" "github_instance_policy_doc" {
  54. # Allow using S3 for GH Actions
  55. statement {
  56. sid = "GeneralBucketAccess"
  57. effect = "Allow"
  58. actions = [
  59. "s3:ListAllMyBuckets",
  60. "s3:HeadBucket",
  61. ]
  62. resources = ["*"]
  63. }
  64. statement {
  65. sid = "S3BucketAccess"
  66. effect = "Allow"
  67. actions = [
  68. "s3:PutObject",
  69. "s3:GetObject",
  70. "s3:ListBucketMultipartUploads",
  71. "s3:ListMultipartUploadParts",
  72. "s3:AbortMultipartUpload",
  73. "s3:DeleteObject",
  74. "s3:ListBuckets",
  75. # "s3:GetLifecycleConfiguration",
  76. # "s3:DeleteObjectVersion",
  77. # "s3:ListBucketVersions",
  78. # "s3:GetBucketLogging",
  79. # "s3:RestoreObject",
  80. # "s3:GetBucketVersioning",
  81. # "s3:PutLifecycleConfiguration",
  82. # "s3:GetBucketCORS",
  83. # "s3:GetBucketLocation",
  84. # "s3:GetObjectVersion",
  85. ]
  86. resources = [
  87. "arn:${var.aws_partition}:s3:::xdr-github-enterprise-${var.environment}-github-actions",
  88. "arn:${var.aws_partition}:s3:::xdr-github-enterprise-${var.environment}-github-actions/*",
  89. ]
  90. }
  91. statement {
  92. sid = "KMSKeyAccess"
  93. effect = "Allow"
  94. actions = [
  95. "kms:Decrypt",
  96. "kms:GenerateDataKeyWithoutPlaintext",
  97. "kms:Verify",
  98. "kms:GenerateDataKeyPairWithoutPlaintext",
  99. "kms:GenerateDataKeyPair",
  100. "kms:ReEncryptFrom",
  101. "kms:Encrypt",
  102. "kms:GenerateDataKey",
  103. "kms:ReEncryptTo",
  104. "kms:Sign",
  105. ]
  106. resources = ["*"]
  107. }
  108. }
  109. resource "aws_iam_role_policy_attachment" "github_instance_policy_attach" {
  110. role = aws_iam_role.github_instance_role.name
  111. policy_arn = aws_iam_policy.github_instance_policy.arn
  112. }