iam.moose-hf.tf 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # the 'splunk-addon-for-aws' role is created in all accounts via
  2. # the base/account_standards module.
  3. #
  4. # Then, there is an instance profile (for use in the partition holding moose)
  5. # and a user account (for use in the partion _not_ holding moose) that
  6. # with keys for moose.
  7. #
  8. # That instance profile/user is allowed to assumerole into the
  9. # 'splunk-addon-for-aws' role in the other accounts.
  10. ######################
  11. # Access keys
  12. #
  13. # For rotation purposes, there are two of these. Delete the oldest one,
  14. # add a new one (with a higher version number), and then update the output
  15. #
  16. # Possible futue improvement:
  17. # We could specify a pgp_key attribute, and then the secret will be encrypted
  18. # in both the state file and in the output. If we used the salt PGP key,
  19. # no user would ever have to see the secret key.
  20. resource "aws_iam_access_key" "moose-hf-v0" {
  21. user = aws_iam_user.moose-hf.name
  22. }
  23. resource "aws_iam_access_key" "moose-hf-v1" {
  24. user = aws_iam_user.moose-hf.name
  25. }
  26. output access_keys {
  27. value = {
  28. "current" = {
  29. "aws_access_key_id": aws_iam_access_key.moose-hf-v1.id
  30. "aws_secret_access_key": aws_iam_access_key.moose-hf-v1.secret
  31. },
  32. "previous" = {
  33. "aws_access_key_id": aws_iam_access_key.moose-hf-v0.id
  34. "aws_secret_access_key": aws_iam_access_key.moose-hf-v0.secret
  35. }
  36. }
  37. sensitive = true
  38. }
  39. ######################
  40. # The policy is attached to both the user and the instance profile
  41. resource "aws_iam_policy" "moose-hf" {
  42. name = "moose-hf"
  43. path = "/instance/"
  44. description = "Policy to allow the moose HF to assume roles"
  45. policy = <<EOF
  46. {
  47. "Version": "2012-10-17",
  48. "Statement": [
  49. {
  50. "Effect": "Allow",
  51. "Action": [
  52. "sts:AssumeRole",
  53. "logs:DescribeLogGroups",
  54. "logs:DescribeLogStreams",
  55. "logs:GetLogEvents"
  56. ],
  57. "Resource": "*"
  58. }
  59. ]
  60. }
  61. EOF
  62. }
  63. ######################
  64. # The instance profile
  65. resource "aws_iam_instance_profile" "moose-hf" {
  66. name = "moose-hf"
  67. role = aws_iam_role.moose-hf.name
  68. }
  69. resource "aws_iam_role" "moose-hf" {
  70. name = "moose-hf"
  71. path = "/instance/"
  72. assume_role_policy = <<EOF
  73. {
  74. "Version": "2012-10-17",
  75. "Statement": [
  76. {
  77. "Action": "sts:AssumeRole",
  78. "Principal": {
  79. "Service": "ec2.amazonaws.com"
  80. },
  81. "Effect": "Allow",
  82. "Sid": ""
  83. }
  84. ]
  85. }
  86. EOF
  87. }
  88. resource "aws_iam_role_policy_attachment" "moose-hf" {
  89. role = aws_iam_role.moose-hf.name
  90. policy_arn = aws_iam_policy.moose-hf.arn
  91. }
  92. ######################
  93. # the user
  94. #
  95. # Note: CIS requires that policies _NOT_ be directly attached to a user. Users must
  96. # be members of groups, and those groups can have policies.
  97. resource "aws_iam_user" "moose-hf" {
  98. name = "moose-hf"
  99. path = "/instance/"
  100. tags = merge(var.standard_tags, var.tags)
  101. }
  102. resource "aws_iam_group" "moose-hf" {
  103. name = "moose-hf"
  104. path = "/instance/"
  105. }
  106. resource "aws_iam_user_group_membership" "moose-hf" {
  107. user = aws_iam_user.moose-hf.name
  108. groups = [ aws_iam_group.moose-hf.name ]
  109. }
  110. resource "aws_iam_group_policy_attachment" "moose-hf-group" {
  111. group = aws_iam_group.moose-hf.name
  112. policy_arn = aws_iam_policy.moose-hf.arn
  113. }