iam.tf 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }