77}
88
99module "cloud_custodian_s3" {
10+ count = var. create_bucket ? 1 : 0
1011 source = " terraform-aws-modules/s3-bucket/aws"
1112
1213 bucket = " ${ local . prefix } ec2-instance-state-${ local . account_id } "
@@ -39,15 +40,17 @@ resource "aws_iam_role_policy" "custodian" {
3940}
4041
4142data "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- ]
43+ dynamic "statement" {
44+ for_each = var. create_bucket ? [" ${ module . cloud_custodian_s3 [0 ]. s3_bucket_arn } /*" ] : []
45+ content {
46+ actions = [
47+ " s3:PutObject" ,
48+ ]
49+
50+ resources = [
51+ statement . value
52+ ]
53+ }
5154 }
5255
5356 statement {
@@ -78,11 +81,11 @@ module "cloud_custodian_lambda" {
7881
7982 regions = [local . region ]
8083
81- execution_options = {
84+ execution_options = var . create_bucket ? {
8285 # Not really required but if you run custodian run you need to specify -s/--output-dir you'd then have execution-options
8386 # as part of the config.json with the output_dir that was specified
8487 " output_dir" = " s3://${ local . prefix } ec2-instance-state-${ local . account_id } /output?region=${ local . region } "
85- }
88+ } : {}
8689
8790 force_deploy = var. force_deploy
8891
@@ -94,14 +97,18 @@ module "cloud_custodian_lambda" {
9497 "mode": {
9598 "type": "ec2-instance-state",
9699 "function-prefix": "${ local . prefix } ",
97- "execution-options": {
98- "metrics_enabled": true,
99- "dryrun": false,
100- "log_group": "/cloud-custodian/policies",
101- "output_dir": "s3://${ local . prefix } ec2-instance-state-${ local . account_id } /output",
102- "cache_dir": "s3://${ local . prefix } ec2-instance-state-${ local . account_id } /cache",
103- "cache_period": 15
104- },
100+ "execution-options": ${ jsonencode (merge (
101+ {
102+ metrics_enabled = true
103+ dryrun = false
104+ log_group = " /cloud-custodian/policies"
105+ },
106+ var. create_bucket ? {
107+ output_dir = " s3://${ local . prefix } ec2-instance-state-${ local . account_id } /output"
108+ cache_dir = " s3://${ local . prefix } ec2-instance-state-${ local . account_id } /cache"
109+ cache_period = 15
110+ } : {}
111+ ))} ,
105112 "role": "arn:aws:iam::${ local . account_id } :role/${ local . prefix } ec2-instance-state-lambda",
106113 "events": [
107114 "terminated"
0 commit comments