|
| 1 | +data "aws_caller_identity" "current" {} |
| 2 | + |
| 3 | +locals { |
| 4 | + region = "eu-west-1" |
| 5 | + account_id = data.aws_caller_identity.current.account_id |
| 6 | + prefix = "custodian-dev-" |
| 7 | +} |
| 8 | + |
| 9 | +module "cloud_custodian_s3" { |
| 10 | + source = "terraform-aws-modules/s3-bucket/aws" |
| 11 | + |
| 12 | + bucket = "${local.prefix}schedule-${local.account_id}" |
| 13 | + force_destroy = true |
| 14 | +} |
| 15 | + |
| 16 | +resource "aws_iam_role" "custodian" { |
| 17 | + name = "${local.prefix}schedule" |
| 18 | + assume_role_policy = <<EOF |
| 19 | +{ |
| 20 | + "Version": "2012-10-17", |
| 21 | + "Statement": [ |
| 22 | + { |
| 23 | + "Action": "sts:AssumeRole", |
| 24 | + "Principal": { |
| 25 | + "Service": "lambda.amazonaws.com" |
| 26 | + }, |
| 27 | + "Effect": "Allow", |
| 28 | + "Sid": "" |
| 29 | + } |
| 30 | + ] |
| 31 | +} |
| 32 | +EOF |
| 33 | +} |
| 34 | + |
| 35 | +resource "aws_iam_role_policy" "custodian" { |
| 36 | + role = aws_iam_role.custodian.id |
| 37 | + |
| 38 | + policy = data.aws_iam_policy_document.custodian.json |
| 39 | +} |
| 40 | + |
| 41 | +data "aws_iam_policy_document" "custodian" { |
| 42 | + statement { |
| 43 | + |
| 44 | + actions = [ |
| 45 | + "s3:PutObject", |
| 46 | + ] |
| 47 | + |
| 48 | + resources = [ |
| 49 | + "${module.cloud_custodian_s3.s3_bucket_arn}/*", |
| 50 | + ] |
| 51 | + } |
| 52 | + |
| 53 | + statement { |
| 54 | + |
| 55 | + actions = [ |
| 56 | + "logs:PutLogEvents", |
| 57 | + "logs:DescribeLogGroups", |
| 58 | + "logs:CreateLogStream", |
| 59 | + "logs:CreateLogGroup", |
| 60 | + "cloudwatch:PutMetricData", |
| 61 | + ] |
| 62 | + |
| 63 | + resources = ["*"] |
| 64 | + } |
| 65 | + |
| 66 | + statement { |
| 67 | + |
| 68 | + actions = [ |
| 69 | + "ec2:DescribeImages" |
| 70 | + ] |
| 71 | + |
| 72 | + resources = ["*"] |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +resource "aws_iam_role" "scheduler" { |
| 77 | + name = "${local.prefix}scheduler" |
| 78 | + assume_role_policy = <<EOF |
| 79 | +{ |
| 80 | + "Version": "2012-10-17", |
| 81 | + "Statement": [ |
| 82 | + { |
| 83 | + "Action": "sts:AssumeRole", |
| 84 | + "Principal": { |
| 85 | + "Service": "scheduler.amazonaws.com" |
| 86 | + }, |
| 87 | + "Effect": "Allow", |
| 88 | + "Sid": "" |
| 89 | + } |
| 90 | + ] |
| 91 | +} |
| 92 | +EOF |
| 93 | +} |
| 94 | + |
| 95 | +resource "aws_iam_role_policy" "scheduler" { |
| 96 | + role = aws_iam_role.scheduler.id |
| 97 | + |
| 98 | + policy = data.aws_iam_policy_document.scheduler.json |
| 99 | +} |
| 100 | + |
| 101 | +data "aws_iam_policy_document" "scheduler" { |
| 102 | + statement { |
| 103 | + actions = [ |
| 104 | + "lambda:InvokeFunction", |
| 105 | + ] |
| 106 | + |
| 107 | + resources = [ |
| 108 | + "arn:aws:lambda:${local.region}:${local.account_id}:function:${local.prefix}*", |
| 109 | + ] |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +resource "aws_scheduler_schedule_group" "custodian" { |
| 114 | + name = "${local.prefix}schedule-group" |
| 115 | +} |
| 116 | + |
| 117 | +module "cloud_custodian_lambda" { |
| 118 | + source = "../../" |
| 119 | + |
| 120 | + regions = [local.region] |
| 121 | + |
| 122 | + execution_options = { |
| 123 | + # Not really required but if you run custodian run you need to specify -s/--output-dir you'd then have execution-options |
| 124 | + # as part of the config.json with the output_dir that was specified |
| 125 | + "output_dir" = "s3://${local.prefix}schedule-${local.account_id}/output?region=${local.region}" |
| 126 | + } |
| 127 | + |
| 128 | + policies = templatefile("${path.module}/templates/policy.yaml.tpl", { |
| 129 | + prefix = local.prefix |
| 130 | + account_id = local.account_id |
| 131 | + }) |
| 132 | + |
| 133 | + depends_on = [ |
| 134 | + module.cloud_custodian_s3, |
| 135 | + aws_iam_role.custodian, |
| 136 | + aws_iam_role.scheduler, |
| 137 | + aws_scheduler_schedule_group.custodian, |
| 138 | + ] |
| 139 | +} |
0 commit comments