iam.tf 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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": "AssumeRoleAnywhere",
  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. data "aws_iam_policy_document" "default_instance_policy_s3_binaries_doc" {
  49. statement {
  50. sid = "AccessTheBucketItself"
  51. effect = "Allow"
  52. resources = ["arn:${var.aws_partition}:s3:::${var.binaries_bucket}"]
  53. actions = [
  54. "s3:ListBucket",
  55. "s3:GetBucketLocation",
  56. ]
  57. }
  58. statement {
  59. sid = "GetFromTheBucket"
  60. effect = "Allow"
  61. resources = ["arn:${var.aws_partition}:s3:::${var.binaries_bucket}/*"]
  62. actions = [
  63. "s3:GetObject",
  64. "s3:GetObjectAcl",
  65. ]
  66. }
  67. statement {
  68. sid = "UseTheKey"
  69. effect = "Allow"
  70. resources = [
  71. "arn:${var.aws_partition}:kms:${var.aws_region}:${var.common_services_account}:${var.binaries_key}"
  72. ]
  73. actions = [
  74. "kms:Decrypt",
  75. "kms:DescribeKey"
  76. ]
  77. }
  78. }
  79. resource "aws_iam_policy" "default_instance_policy_s3_binaries" {
  80. name = "default_instance_s3_binaries"
  81. path = "/launchroles/"
  82. description = "This policy allows a EC2 server to read from the s3 binaries bucket"
  83. policy = data.aws_iam_policy_document.default_instance_policy_s3_binaries_doc.json
  84. }
  85. resource "aws_iam_role_policy_attachment" "default_instance_AmazonEC2RoleforSSM" {
  86. role = aws_iam_role.default_instance_role.name
  87. policy_arn = "arn:${var.aws_partition}:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
  88. }
  89. resource "aws_iam_role_policy_attachment" "default_instance_default_policy_attach" {
  90. role = aws_iam_role.default_instance_role.name
  91. policy_arn = aws_iam_policy.default_instance_policy.arn
  92. }
  93. resource "aws_iam_role_policy_attachment" "default_instance_s3_policy_attach" {
  94. role = aws_iam_role.default_instance_role.name
  95. policy_arn = aws_iam_policy.default_instance_policy_s3_binaries.arn
  96. }
  97. resource "aws_iam_role_policy_attachment" "default_instance_cloudwatch_policy_attach" {
  98. role = aws_iam_role.default_instance_role.name
  99. policy_arn = aws_iam_policy.cloudwatch_events.arn
  100. }
  101. ##########################
  102. # cloudwatch events
  103. data "aws_iam_policy_document" "cloudwatch_events" {
  104. statement {
  105. sid = "1"
  106. actions = [
  107. "events:PutRule"
  108. ]
  109. resources = [ "*" ]
  110. }
  111. }
  112. resource "aws_iam_policy" "cloudwatch_events" {
  113. name = "cloudwatch_events"
  114. description = "Creation of cloudwatch events"
  115. policy = data.aws_iam_policy_document.cloudwatch_events.json
  116. }
  117. ##########################
  118. # dlm_lifecycle
  119. #
  120. # 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
  121. # Docs can be found here https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/snapshot-lifecycle.html
  122. # Chris Lynch 1/25/2019
  123. resource "aws_iam_role" "dlm_lifecycle_role" {
  124. name = "dlm-lifecycle-role"
  125. assume_role_policy = <<EOF
  126. {
  127. "Version": "2012-10-17",
  128. "Statement": [
  129. {
  130. "Action": "sts:AssumeRole",
  131. "Principal": {
  132. "Service": "dlm.amazonaws.com"
  133. },
  134. "Effect": "Allow",
  135. "Sid": ""
  136. }
  137. ]
  138. }
  139. EOF
  140. }
  141. resource "aws_iam_role_policy" "dlm_lifecycle" {
  142. name = "dlm-lifecycle-policy"
  143. role = aws_iam_role.dlm_lifecycle_role.id
  144. policy = <<EOF
  145. {
  146. "Version": "2012-10-17",
  147. "Statement": [
  148. {
  149. "Effect": "Allow",
  150. "Action": [
  151. "ec2:CreateSnapshot",
  152. "ec2:DeleteSnapshot",
  153. "ec2:DescribeVolumes",
  154. "ec2:DescribeSnapshots",
  155. "ec2:DescribeImages",
  156. "ec2:DescribeInstances",
  157. "ec2:DescribeImageAttribute"
  158. ],
  159. "Resource": "*"
  160. },
  161. {
  162. "Effect": "Allow",
  163. "Action": [
  164. "ec2:CreateTags"
  165. ],
  166. "Resource": [
  167. "arn:${var.aws_partition}:ec2:*::snapshot/*",
  168. "arn:${var.aws_partition}:ec2:*::image/*"
  169. ]
  170. },
  171. {
  172. "Effect": "Allow",
  173. "Action": "ec2:DeleteSnapshot",
  174. "Resource": "arn:${var.aws_partition}:ec2:*::snapshot/*"
  175. },
  176. {
  177. "Effect": "Allow",
  178. "Action": [
  179. "ec2:ResetImageAttribute",
  180. "ec2:DeregisterImage",
  181. "ec2:CreateImage",
  182. "ec2:CopyImage",
  183. "ec2:ModifyImageAttribute"
  184. ],
  185. "Resource": "*"
  186. },
  187. {
  188. "Effect": "Allow",
  189. "Action": [
  190. "kms:ReEncrypt*",
  191. "kms:GenerateDataKey*",
  192. "kms:Encrypt",
  193. "kms:DescribeKey",
  194. "kms:Decrypt",
  195. "kms:Create*"
  196. ],
  197. "Resource": "*"
  198. }
  199. ]
  200. }
  201. EOF
  202. }
  203. ##########################
  204. # moose
  205. #
  206. # See https://docs.splunk.com/Documentation/AddOns/released/AWS/ConfigureAWSpermissions
  207. locals {
  208. trusted_principals_govcloud = [
  209. "arn:${var.aws_partition}:iam::${local.c2_account}:role/instance/moose-hf",
  210. "arn:${var.aws_partition}:iam::${local.c2_account}:user/instance/moose-hf"
  211. ]
  212. trusted_principals_commercial = [
  213. "arn:${var.aws_partition}:iam::${var.legacy_account}:role/splunk-aws-instance-role",
  214. "arn:${var.aws_partition}:iam::${local.c2_account}:user/instance/moose-hf",
  215. ]
  216. trusted_principals = var.aws_partition == "aws" ? local.trusted_principals_commercial : local.trusted_principals_govcloud
  217. }
  218. resource "aws_iam_role" "splunk_addon_for_aws" {
  219. name = "splunk-addon-for-aws"
  220. path = "/instance/"
  221. assume_role_policy = <<EOF
  222. {
  223. "Version": "2012-10-17",
  224. "Statement": [
  225. {
  226. "Sid": "",
  227. "Effect": "Allow",
  228. "Principal": {
  229. "AWS": ${jsonencode(local.trusted_principals)}
  230. },
  231. "Action": "sts:AssumeRole"
  232. }
  233. ]
  234. }
  235. EOF
  236. }
  237. resource "aws_iam_role_policy" "splunk_addon_for_aws" {
  238. name = "splunk-addon-for-aws"
  239. role = aws_iam_role.splunk_addon_for_aws.id
  240. policy = <<EOF
  241. {
  242. "Version": "2012-10-17",
  243. "Statement": [
  244. {
  245. "Effect": "Allow",
  246. "Action": [
  247. "sqs:GetQueueAttributes",
  248. "sqs:ListQueues",
  249. "sqs:ReceiveMessage",
  250. "sqs:GetQueueUrl",
  251. "sqs:SendMessage",
  252. "sqs:DeleteMessage",
  253. "s3:ListBucket",
  254. "s3:GetObject",
  255. "s3:GetBucketLocation",
  256. "s3:ListAllMyBuckets",
  257. "s3:GetBucketTagging",
  258. "s3:GetAccelerateConfiguration",
  259. "s3:GetBucketLogging",
  260. "s3:GetLifecycleConfiguration",
  261. "s3:GetBucketCORS",
  262. "config:DeliverConfigSnapshot",
  263. "config:DescribeConfigRules",
  264. "config:DescribeConfigRuleEvaluationStatus",
  265. "config:GetComplianceDetailsByConfigRule",
  266. "config:GetComplianceSummaryByConfigRule",
  267. "iam:GetUser",
  268. "iam:ListUsers",
  269. "iam:GetAccountPasswordPolicy",
  270. "iam:ListAccessKeys",
  271. "iam:GetAccessKeyLastUsed",
  272. "autoscaling:Describe*",
  273. "cloudwatch:Describe*",
  274. "cloudwatch:Get*",
  275. "cloudwatch:List*",
  276. "sns:Get*",
  277. "sns:List*",
  278. "sns:Publish",
  279. "logs:DescribeLogGroups",
  280. "logs:DescribeLogStreams",
  281. "logs:GetLogEvents",
  282. "ec2:DescribeInstances",
  283. "ec2:DescribeReservedInstances",
  284. "ec2:DescribeSnapshots",
  285. "ec2:DescribeRegions",
  286. "ec2:DescribeKeyPairs",
  287. "ec2:DescribeNetworkAcls",
  288. "ec2:DescribeSecurityGroups",
  289. "ec2:DescribeSubnets",
  290. "ec2:DescribeVolumes",
  291. "ec2:DescribeVpcs",
  292. "ec2:DescribeImages",
  293. "ec2:DescribeAddresses",
  294. "lambda:ListFunctions",
  295. "rds:DescribeDBInstances",
  296. "cloudfront:ListDistributions",
  297. "elasticloadbalancing:DescribeLoadBalancers",
  298. "elasticloadbalancing:DescribeInstanceHealth",
  299. "elasticloadbalancing:DescribeTags",
  300. "elasticloadbalancing:DescribeTargetGroups",
  301. "elasticloadbalancing:DescribeTargetHealth",
  302. "elasticloadbalancing:DescribeListeners",
  303. "inspector:Describe*",
  304. "inspector:List*",
  305. "kinesis:Get*",
  306. "kinesis:DescribeStream",
  307. "kinesis:ListStreams",
  308. "kms:Decrypt",
  309. "sts:AssumeRole"
  310. ],
  311. "Resource": [
  312. "*"
  313. ]
  314. }
  315. ]
  316. }
  317. EOF
  318. }