section-1_12.tf 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Inactivity check and disable function
  2. ## IAM Policy
  3. data "template_file" "root_account_check_policy" {
  4. template = file("${path.module}/templates/lambda_root_account_check_policy.json.tpl")
  5. }
  6. resource "aws_iam_role" "root_account_check" {
  7. name = "${var.resource_name_prefix}-root-account-check"
  8. path = "/lambda/"
  9. assume_role_policy = data.template_file.iam_lambda_assume_role_policy.rendered
  10. }
  11. resource "aws_iam_role_policy" "root_account_check" {
  12. name = "${var.resource_name_prefix}-lambda-root-account-check"
  13. role = aws_iam_role.root_account_check.id
  14. policy = data.template_file.root_account_check_policy.rendered
  15. }
  16. ## /IAM Policy
  17. ## Create the function
  18. data "archive_file" "root_account_check" {
  19. type = "zip"
  20. source_file = "${path.module}/files/root_account_check.py"
  21. output_path = "${var.temp_artifacts_dir}/root_account_check.zip"
  22. }
  23. resource "aws_lambda_function" "root_account_check" {
  24. filename = "${var.temp_artifacts_dir}/root_account_check.zip"
  25. function_name = "${var.resource_name_prefix}-root-account-check"
  26. role = aws_iam_role.root_account_check.arn
  27. handler = "root_account_check.lambda_handler"
  28. source_code_hash = data.archive_file.root_account_check.output_base64sha256
  29. runtime = "python2.7"
  30. timeout = var.lambda_timeout
  31. environment {
  32. variables = {
  33. DRY_RUN = var.lambda_dry_run
  34. AGGRESSIVE = var.lambda_aggressive
  35. INACTIVITY_LIMIT = var.lambda_user_inactivity_limit
  36. IGNORE_IAM_USER_PREFIX = var.lambda_mfa_checker_user_prefix
  37. IGNORE_IAM_USER_SUFFIX = var.lambda_mfa_checker_user_suffix
  38. }
  39. }
  40. tags = merge(var.standard_tags, var.tags)
  41. }
  42. ## /Create the function
  43. ## Schedule the lambda function
  44. resource "aws_cloudwatch_event_rule" "root_account_check" {
  45. name = "${var.resource_name_prefix}-root-account-check"
  46. description = "disables inactive users"
  47. schedule_expression = var.lambda_cron_schedule
  48. }
  49. resource "aws_cloudwatch_event_target" "root_account_check" {
  50. rule = aws_cloudwatch_event_rule.root_account_check.name
  51. target_id = "${var.resource_name_prefix}-root-account-check"
  52. arn = aws_lambda_function.root_account_check.arn
  53. }
  54. resource "aws_lambda_permission" "root_account_check" {
  55. statement_id = "AllowExecutionFromCloudWatch"
  56. action = "lambda:InvokeFunction"
  57. function_name = aws_lambda_function.root_account_check.function_name
  58. principal = "events.amazonaws.com"
  59. source_arn = aws_cloudwatch_event_rule.root_account_check.arn
  60. }
  61. ## /Schedule the lambda function
  62. # /MFA check and disable function