iam.tf 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # IAM Roles in All Accounts
  2. #############################
  3. # Default instance profile
  4. #
  5. # Basic profile to allow basic things
  6. resource "aws_iam_instance_profile" "default_instance_profile" {
  7. name = "msoc-default-instance-profile"
  8. role = aws_iam_role.default_instance_role.name
  9. }
  10. resource "aws_iam_role" "default_instance_role" {
  11. name = "msoc-default-instance-role"
  12. assume_role_policy = <<EOF
  13. {
  14. "Version": "2012-10-17",
  15. "Statement": [
  16. {
  17. "Sid": "",
  18. "Effect": "Allow",
  19. "Principal": {
  20. "Service": [
  21. "ec2.amazonaws.com",
  22. "ssm.amazonaws.com"
  23. ]
  24. },
  25. "Action": "sts:AssumeRole"
  26. }
  27. ]
  28. }
  29. EOF
  30. }
  31. data "aws_iam_policy_document" "default_instance_policy_doc" {
  32. statement {
  33. effect = "Allow"
  34. actions = [
  35. "ec2:DescribeTags"
  36. ]
  37. resources = [
  38. "*"
  39. ]
  40. }
  41. }
  42. resource "aws_iam_policy" "default_instance_policy" {
  43. name = "default_instance_tag_read"
  44. path = "/launchroles/"
  45. description = "This policy allows a EC2 server to read tags"
  46. policy = data.aws_iam_policy_document.default_instance_policy_doc.json
  47. }
  48. resource "aws_iam_role_policy_attachment" "default_instance_AmazonEC2RoleforSSM" {
  49. role = aws_iam_role.default_instance_role.name
  50. policy_arn = "arn:${var.aws_partition}:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
  51. }
  52. resource "aws_iam_role_policy_attachment" "default_instance_default_policy_attach" {
  53. role = aws_iam_role.default_instance_role.name
  54. policy_arn = aws_iam_policy.default_instance_policy.arn
  55. }
  56. resource "aws_iam_role_policy_attachment" "default_instance_cloudwatch_policy_attach" {
  57. role = aws_iam_role.default_instance_role.name
  58. policy_arn = aws_iam_policy.cloudwatch_events.arn
  59. }
  60. ##########################
  61. # cloudwatch events
  62. data "aws_iam_policy_document" "cloudwatch_events" {
  63. statement {
  64. sid = "1"
  65. actions = [
  66. "events:PutRule"
  67. ]
  68. resources = [ "*" ]
  69. }
  70. }
  71. resource "aws_iam_policy" "cloudwatch_events" {
  72. name = "cloudwatch_events"
  73. description = "Creation of cloudwatch events"
  74. policy = data.aws_iam_policy_document.cloudwatch_events.json
  75. }
  76. ##########################
  77. # dlm_lifecycle
  78. #
  79. # This is to setup the needed IAM role and premissions for the AWS feature Data Lifecycle Manager (DLM) lifecycle policy so we can have it do "backups" on our EBS
  80. # Docs can be found here https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/snapshot-lifecycle.html
  81. # Chris Lynch 1/25/2019
  82. resource "aws_iam_role" "dlm_lifecycle_role" {
  83. name = "dlm-lifecycle-role"
  84. assume_role_policy = <<EOF
  85. {
  86. "Version": "2012-10-17",
  87. "Statement": [
  88. {
  89. "Action": "sts:AssumeRole",
  90. "Principal": {
  91. "Service": "dlm.amazonaws.com"
  92. },
  93. "Effect": "Allow",
  94. "Sid": ""
  95. }
  96. ]
  97. }
  98. EOF
  99. }
  100. resource "aws_iam_role_policy" "dlm_lifecycle" {
  101. name = "dlm-lifecycle-policy"
  102. role = aws_iam_role.dlm_lifecycle_role.id
  103. policy = <<EOF
  104. {
  105. "Version": "2012-10-17",
  106. "Statement": [
  107. {
  108. "Effect": "Allow",
  109. "Action": [
  110. "ec2:CreateSnapshot",
  111. "ec2:DeleteSnapshot",
  112. "ec2:DescribeVolumes",
  113. "ec2:DescribeSnapshots"
  114. ],
  115. "Resource": "*"
  116. },
  117. {
  118. "Effect": "Allow",
  119. "Action": [
  120. "ec2:CreateTags"
  121. ],
  122. "Resource": "arn:${var.aws_partition}:ec2:*::snapshot/*"
  123. }
  124. ]
  125. }
  126. EOF
  127. }
  128. ##########################
  129. # moose
  130. #
  131. # See https://docs.splunk.com/Documentation/AddOns/released/AWS/ConfigureAWSpermissions
  132. locals {
  133. trusted_principals_govcloud = [
  134. "arn:${var.aws_partition}:iam::${local.c2_account}:role/instance/moose-hf",
  135. "arn:${var.aws_partition}:iam::${local.c2_account}:user/instance/moose-hf"
  136. ]
  137. trusted_principals_commercial = [
  138. "arn:${var.aws_partition}:iam::${var.legacy_account}:role/splunk-aws-instance-role",
  139. "arn:${var.aws_partition}:iam::${local.c2_account}:user/instance/moose-hf",
  140. ]
  141. trusted_principals = var.aws_partition == "aws" ? local.trusted_principals_commercial : local.trusted_principals_govcloud
  142. }
  143. resource "aws_iam_role" "splunk_addon_for_aws" {
  144. name = "splunk-addon-for-aws"
  145. path = "/instance/"
  146. assume_role_policy = <<EOF
  147. {
  148. "Version": "2012-10-17",
  149. "Statement": [
  150. {
  151. "Sid": "",
  152. "Effect": "Allow",
  153. "Principal": {
  154. "AWS": ${jsonencode(local.trusted_principals)}
  155. },
  156. "Action": "sts:AssumeRole"
  157. }
  158. ]
  159. }
  160. EOF
  161. }
  162. resource "aws_iam_role_policy" "splunk_addon_for_aws" {
  163. name = "splunk-addon-for-aws"
  164. role = aws_iam_role.splunk_addon_for_aws.id
  165. policy = <<EOF
  166. {
  167. "Version": "2012-10-17",
  168. "Statement": [
  169. {
  170. "Effect": "Allow",
  171. "Action": [
  172. "sqs:GetQueueAttributes",
  173. "sqs:ListQueues",
  174. "sqs:ReceiveMessage",
  175. "sqs:GetQueueUrl",
  176. "sqs:SendMessage",
  177. "sqs:DeleteMessage",
  178. "s3:ListBucket",
  179. "s3:GetObject",
  180. "s3:GetBucketLocation",
  181. "s3:ListAllMyBuckets",
  182. "s3:GetBucketTagging",
  183. "s3:GetAccelerateConfiguration",
  184. "s3:GetBucketLogging",
  185. "s3:GetLifecycleConfiguration",
  186. "s3:GetBucketCORS",
  187. "config:DeliverConfigSnapshot",
  188. "config:DescribeConfigRules",
  189. "config:DescribeConfigRuleEvaluationStatus",
  190. "config:GetComplianceDetailsByConfigRule",
  191. "config:GetComplianceSummaryByConfigRule",
  192. "iam:GetUser",
  193. "iam:ListUsers",
  194. "iam:GetAccountPasswordPolicy",
  195. "iam:ListAccessKeys",
  196. "iam:GetAccessKeyLastUsed",
  197. "autoscaling:Describe*",
  198. "cloudwatch:Describe*",
  199. "cloudwatch:Get*",
  200. "cloudwatch:List*",
  201. "sns:Get*",
  202. "sns:List*",
  203. "sns:Publish",
  204. "logs:DescribeLogGroups",
  205. "logs:DescribeLogStreams",
  206. "logs:GetLogEvents",
  207. "ec2:DescribeInstances",
  208. "ec2:DescribeReservedInstances",
  209. "ec2:DescribeSnapshots",
  210. "ec2:DescribeRegions",
  211. "ec2:DescribeKeyPairs",
  212. "ec2:DescribeNetworkAcls",
  213. "ec2:DescribeSecurityGroups",
  214. "ec2:DescribeSubnets",
  215. "ec2:DescribeVolumes",
  216. "ec2:DescribeVpcs",
  217. "ec2:DescribeImages",
  218. "ec2:DescribeAddresses",
  219. "lambda:ListFunctions",
  220. "rds:DescribeDBInstances",
  221. "cloudfront:ListDistributions",
  222. "elasticloadbalancing:DescribeLoadBalancers",
  223. "elasticloadbalancing:DescribeInstanceHealth",
  224. "elasticloadbalancing:DescribeTags",
  225. "elasticloadbalancing:DescribeTargetGroups",
  226. "elasticloadbalancing:DescribeTargetHealth",
  227. "elasticloadbalancing:DescribeListeners",
  228. "inspector:Describe*",
  229. "inspector:List*",
  230. "kinesis:Get*",
  231. "kinesis:DescribeStream",
  232. "kinesis:ListStreams",
  233. "kms:Decrypt",
  234. "sts:AssumeRole"
  235. ],
  236. "Resource": [
  237. "*"
  238. ]
  239. }
  240. ]
  241. }
  242. EOF
  243. }