├── .DS_Store ├── .eslintignore ├── .eslintrc.json ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── jest.config.ts ├── package-lock.json ├── package.json ├── src ├── index.ts ├── iterator-state-machine.ts └── runner-state-machine.ts ├── test ├── __snapshots__ │ └── aws-logs-comptroller.test.ts.snap ├── aws-logs-comptroller.test.ts ├── default.integ.snapshot │ ├── IntegDefaultTestDeployAssert4E6713E1.assets.json │ ├── IntegDefaultTestDeployAssert4E6713E1.template.json │ ├── StackUnderTest.assets.json │ ├── StackUnderTest.template.json │ ├── integ.json │ ├── manifest.json │ └── tree.json └── integ.default.ts └── tsconfig.dev.json /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elthrasher/aws-logs-comptroller/53d00e83dfd870ffca7646f6d84f8a295bc5ac17/.DS_Store -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | *.d.ts 2 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es2021": true, 4 | "node": true 5 | }, 6 | "extends": "standard-with-typescript", 7 | "overrides": [], 8 | "parserOptions": { 9 | "ecmaVersion": "latest", 10 | "project": "./tsconfig.dev.json", 11 | "sourceType": "module" 12 | }, 13 | "rules": { 14 | "no-new": "off", 15 | "object-curly-newline": [ 16 | "error", 17 | { 18 | "ObjectExpression": "always", 19 | "ObjectPattern": { 20 | "multiline": true 21 | }, 22 | "ImportDeclaration": "never", 23 | "ExportDeclaration": { 24 | "multiline": true, 25 | "minProperties": 3 26 | } 27 | } 28 | ], 29 | "@typescript-eslint/comma-dangle": [ 30 | "error", 31 | "always-multiline" 32 | ], 33 | "@typescript-eslint/semi": [ 34 | "error", 35 | "always" 36 | ], 37 | "@typescript-eslint/strict-boolean-expressions": "off" 38 | } 39 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.js 2 | !jest.config.js 3 | *.d.ts 4 | coverage 5 | node_modules 6 | 7 | # CDK asset staging directory 8 | .cdk.staging 9 | cdk.out 10 | 11 | # jsii 12 | .jsii 13 | dist 14 | lib 15 | tsconfig.json 16 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *.ts 2 | !*.d.ts 3 | coverage 4 | test 5 | tsconfig.dev.json 6 | .eslintignore 7 | .eslintrc.json 8 | 9 | # CDK asset staging directory 10 | .cdk.staging 11 | cdk.out 12 | 13 | # Exclude jsii outdir 14 | dist 15 | 16 | # Include .jsii and .jsii.gz 17 | !.jsii 18 | !.jsii.gz 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS Logs Comptroller 2 | 3 | ## :warning: This construct is designed to delete logs and log groups! **Use at your own risk!** :warning: 4 | 5 | AWS Logs Comptroller is a CDK Construct that can be used to set CloudWatch log retention as well as prune orphaned LogGroups. AWS Logs Comptroller is written entirely using Step Functions and ASL. It makes [service integration calls](https://docs.aws.amazon.com/step-functions/latest/dg/supported-services-awssdk.html) and uses [intrinsic functions](https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html) in lieu of a custom compute layer such as a Lambda Function. 6 | 7 | AWS Logs Comptroller can handle a large number of LogGroups without failure or timeout due to its architecture and has been tested with thousands of LogGroups. It makes many API calls to CloudWatch (to fetch, update and delete LogGroups) and to Lambda (to identify whether or not the LogGroup is attached to an existing Lambda Function) and may cause throttling to other processes that utilize these APIs. 8 | 9 | ```typescript 10 | const app = new App(); 11 | const stack = new Stack(app); 12 | 13 | new AwsLogsComptroller(stack, 'LogsComptroller'); 14 | ``` 15 | 16 | ## Log Retention 17 | 18 | AWS Logs Comptroller will apply a [retention period](https://docs.aws.amazon.com/managedservices/latest/userguide/log-customize-retention.html) to any LogGroups that lack retention. The default for this is seven days but this can be overriden by passing in the `retentionDays` prop. 19 | 20 | ```typescript 21 | const app = new App(); 22 | const stack = new Stack(app); 23 | 24 | new AwsLogsComptroller(stack, 'LogsComptroller', { retentionDays: RetentionDays.ONE_DAY }); 25 | ``` 26 | 27 | ## Removing 'orphaned' Lambda LogGroups 28 | 29 | Lambda automatically creates LogGroups when a function is called. If the function is later deleted, the LogGroup remains. AWS Logs Comptroller will remove any LogGroups that don't have existing Lambda Functions by parsing the name of the function from the LogGroup and attempting a `GetFunction` API call. If that call returns a 404, the LogGroup will be deleted. 30 | 31 | ## Scheduling 32 | 33 | AWS Logs Comptroller can be scheduled using an EventBridge rule. By default, it'll use the default eventbus and run once daily. 34 | 35 | ```typescript 36 | const app = new App(); 37 | const stack = new Stack(app); 38 | 39 | new AwsLogsComptroller(stack, 'LogsComptroller', { schedule: true }); 40 | ``` 41 | 42 | The `schedule` prop can also accept a cron expression. 43 | 44 | ```typescript 45 | const app = new App(); 46 | const stack = new Stack(app); 47 | 48 | new AwsLogsComptroller(stack, 'LogsComptroller', { schedule: Schedule.cron({ day: '1', hour: '4', minute: '0', }) }); 49 | ``` 50 | 51 | Additionally the schedule Rule can be assigned to a custom EventBus. 52 | 53 | ```typescript 54 | const app = new App(); 55 | const stack = new Stack(app); 56 | 57 | const bus = new EventBus(stack, 'MyEventBus'); 58 | new AwsLogsComptroller(stack, 'LogsComptroller', { eventBus: bus, schedule: true }); 59 | ``` -------------------------------------------------------------------------------- /jest.config.ts: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | collectCoverage: true, 3 | globals: { 4 | 'ts-jest': { 5 | tsconfig: 'tsconfig.dev.json', 6 | }, 7 | }, 8 | roots: ['/test'], 9 | testEnvironment: 'node', 10 | testMatch: ['**/*.test.ts'], 11 | transform: { 12 | '^.+\\.tsx?$': 'ts-jest', 13 | }, 14 | }; 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-logs-comptroller", 3 | "author": { 4 | "email": "elthrasher@gmail.com", 5 | "name": "Matt Morgan", 6 | "url": "https://mattmorgan.cloud" 7 | }, 8 | "description": "Set Log Retention and prune orphaned LogGroups on a schedule using Step Functions service integrations and intrinsic functions.", 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/elthrasher/aws-logs-comptroller" 12 | }, 13 | "keywords": [ 14 | "aws", 15 | "awscdk", 16 | "cdk", 17 | "cloudwatch", 18 | "intrinsic", 19 | "logs", 20 | "loggroups", 21 | "retention", 22 | "serverless", 23 | "step functions" 24 | ], 25 | "license": "Apache-2.0", 26 | "version": "0.1.5", 27 | "main": "lib/index.js", 28 | "types": "lib/index.d.ts", 29 | "scripts": { 30 | "build": "jsii", 31 | "build:watch": "jsii --watch", 32 | "clean": "rimraf cdk.out dist lib", 33 | "integ": "tsc --build tsconfig.dev.json && integ-runner", 34 | "lint": "eslint --ext .ts --fix src test", 35 | "package": "jsii-pacmak", 36 | "prebuild": "npm run clean", 37 | "prebuild:watch": "npm run clean", 38 | "prepackage": "npm run build", 39 | "pretest": "npm run lint", 40 | "test": "jest" 41 | }, 42 | "devDependencies": { 43 | "@aws-cdk/integ-runner": "^2.46.0", 44 | "@aws-cdk/integ-tests-alpha": "^2.46.0-alpha.0", 45 | "@types/jest": "^27.5.2", 46 | "@types/node": "^18.11.0", 47 | "@typescript-eslint/eslint-plugin": "^5.40.0", 48 | "@typescript-eslint/parser": "^5.40.0", 49 | "aws-cdk-lib": "2.46.0", 50 | "constructs": "10.1.131", 51 | "eslint": "^8.25.0", 52 | "eslint-config-standard-with-typescript": "^23.0.0", 53 | "jest": "^27.5.1", 54 | "jsii": "^1.69.0", 55 | "jsii-pacmak": "^1.69.0", 56 | "rimraf": "^3.0.2", 57 | "ts-jest": "^27.1.4", 58 | "ts-node": "^10.9.1", 59 | "typescript": "^4.8.4" 60 | }, 61 | "peerDependencies": { 62 | "aws-cdk-lib": ">= 2.46.0", 63 | "constructs": ">= 10.1.131" 64 | }, 65 | "jsii": { 66 | "outdir": "dist", 67 | "versionFormat": "full", 68 | "targets": {}, 69 | "tsc": { 70 | "outDir": "lib", 71 | "rootDir": "src" 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { EventBus, Rule, Schedule } from 'aws-cdk-lib/aws-events'; 2 | import { SfnStateMachine } from 'aws-cdk-lib/aws-events-targets'; 3 | import { RetentionDays } from 'aws-cdk-lib/aws-logs'; 4 | import { Construct } from 'constructs'; 5 | import { getIteratorStateMachine } from './iterator-state-machine'; 6 | 7 | import { getRunner } from './runner-state-machine'; 8 | 9 | export interface AwsLogsComptrollerProps { 10 | readonly eventBus?: EventBus 11 | readonly retentionDays?: RetentionDays 12 | readonly schedule?: boolean | Schedule 13 | } 14 | 15 | export class AwsLogsComptroller extends Construct { 16 | constructor (scope: Construct, id: string, props: AwsLogsComptrollerProps = { 17 | }) { 18 | super(scope, id); 19 | 20 | const { eventBus, retentionDays, schedule } = props; 21 | 22 | const runner = getRunner(scope, retentionDays); 23 | const iterator = getIteratorStateMachine(scope, runner); 24 | 25 | if (schedule) { 26 | new Rule(this, 'LogsComptrollerScheduler', { 27 | eventBus, 28 | schedule: schedule === true 29 | ? Schedule.cron({ 30 | hour: '4', minute: '0', 31 | }) 32 | : schedule, 33 | targets: [new SfnStateMachine(iterator)], 34 | }); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/iterator-state-machine.ts: -------------------------------------------------------------------------------- 1 | import { Choice, 2 | Condition, 3 | IntegrationPattern, 4 | JsonPath, 5 | Pass, 6 | StateMachine, 7 | Succeed, 8 | TaskInput } from 'aws-cdk-lib/aws-stepfunctions'; 9 | import { CallAwsService, StepFunctionsStartExecution } from 'aws-cdk-lib/aws-stepfunctions-tasks'; 10 | import { Construct } from 'constructs'; 11 | 12 | export const getIteratorStateMachine = ( 13 | scope: Construct, 14 | runner: StateMachine, 15 | ): StateMachine => { 16 | /** 17 | * Get first group of LogGroups. 18 | * Pagination is implicit, so we'll get the first 50 groups. 19 | * If there are more than 50 groups, we'll get a NextToken. 20 | */ 21 | const getLogGroups = new CallAwsService(scope, 'GetLogGroups', { 22 | action: 'describeLogGroups', 23 | iamAction: 'logs:DescribeLogGroups', 24 | iamResources: ['*'], 25 | resultPath: '$.LG', 26 | service: 'cloudwatchlogs', 27 | }); 28 | 29 | /** 30 | * Initialize stats object and set LGsSeen to the length of the returned array. 31 | * This will be our running total. 32 | */ 33 | const setLGsSeen = new Pass(scope, 'SetLGsSeen', { 34 | parameters: { 35 | LGsDeleted: 0, 36 | LGsRetained: 0, 37 | LGsSeen: JsonPath.numberAt('States.ArrayLength($.LG.LogGroups)'), 38 | }, 39 | resultPath: '$.Stats', 40 | }); 41 | 42 | /** 43 | * Execute runner state machine to process the current batch of LogGroups. 44 | * Delegate this to a child state machine in order to avoid hitting the maximum event history of 25000 events. 45 | */ 46 | const executeRunner = new StepFunctionsStartExecution( 47 | scope, 48 | 'ExecuteRunner', 49 | { 50 | input: TaskInput.fromObject({ 51 | LogGroups: JsonPath.objectAt('$.LG.LogGroups'), 52 | Stats: JsonPath.objectAt('$.Stats'), 53 | Token: JsonPath.taskToken, 54 | }), 55 | integrationPattern: IntegrationPattern.WAIT_FOR_TASK_TOKEN, 56 | resultPath: '$.Stats', 57 | stateMachine: runner, 58 | }, 59 | ); 60 | 61 | /** 62 | * Choice step determines whether we should start another loop or if the job is done. 63 | */ 64 | const hasNextToken = new Choice(scope, 'HasNextToken?'); 65 | 66 | /** 67 | * Get next batch of LogGroups with the NextToken. 68 | * Will return another NextToken if there are more results available. 69 | */ 70 | const getNextLogGroups = new CallAwsService(scope, 'GetNextLogGroups', { 71 | action: 'describeLogGroups', 72 | iamAction: 'logs:DescribeLogGroups', 73 | iamResources: ['*'], 74 | parameters: { 75 | NextToken: JsonPath.stringAt('$.LG.NextToken'), 76 | }, 77 | resultPath: '$.LG', 78 | service: 'cloudwatchlogs', 79 | }); 80 | 81 | /** 82 | * Append running total of LogGroups after getting a subsequent batch. 83 | */ 84 | const appendTotal = new Pass(scope, 'AppendTotal', { 85 | parameters: { 86 | LGsDeleted: JsonPath.numberAt('$.Stats.LGsDeleted'), 87 | LGsRetained: JsonPath.numberAt('$.Stats.LGsRetained'), 88 | 'LGsSeen.$': 89 | 'States.MathAdd($.Stats.LGsSeen, States.ArrayLength($.LG.LogGroups))', 90 | }, 91 | resultPath: '$.Stats', 92 | }); 93 | 94 | getLogGroups.next( 95 | setLGsSeen.next( 96 | executeRunner.next( 97 | hasNextToken 98 | .when( 99 | Condition.isPresent('$.LG.NextToken'), 100 | getNextLogGroups.next(appendTotal.next(executeRunner)), 101 | ) 102 | .otherwise( 103 | new Succeed(scope, 'Work Complete!', { 104 | outputPath: '$.Stats', 105 | }), 106 | ), 107 | ), 108 | ), 109 | ); 110 | 111 | return new StateMachine(scope, 'LogsComptrollerIterator', { 112 | definition: getLogGroups, 113 | stateMachineName: 'logs-comptroller-iterator', 114 | tracingEnabled: true, 115 | }); 116 | }; 117 | -------------------------------------------------------------------------------- /src/runner-state-machine.ts: -------------------------------------------------------------------------------- 1 | import { PolicyStatement } from 'aws-cdk-lib/aws-iam'; 2 | import { RetentionDays } from 'aws-cdk-lib/aws-logs'; 3 | import { Choice, Condition, Errors, JsonPath, Map, Pass, Result, StateMachine } from 'aws-cdk-lib/aws-stepfunctions'; 4 | import { CallAwsService } from 'aws-cdk-lib/aws-stepfunctions-tasks'; 5 | import { Construct } from 'constructs'; 6 | 7 | export const getRunner = ( 8 | scope: Construct, 9 | retentionInDays: RetentionDays = RetentionDays.ONE_WEEK, 10 | ): StateMachine => { 11 | /** 12 | * Input includes `LogGroups`, `Stats`, and `Token`. 13 | * Fan out on `LogGroups` (should be a max of 50). 14 | */ 15 | const map = new Map(scope, 'Map', { 16 | inputPath: '$.LogGroups', 17 | maxConcurrency: 10, 18 | resultPath: '$.MapResult', 19 | }); 20 | 21 | /** 22 | * Split LogGroupName into parts. We're looking for `lambda` in the second place. 23 | */ 24 | const getLGParts = new Pass(scope, 'GetLGParts', { 25 | parameters: { 26 | 'LGParts.$': 'States.StringSplit($.LogGroupName, \'/\')', 27 | }, 28 | resultPath: JsonPath.stringAt('$.Function'), 29 | }); 30 | 31 | /** 32 | * If we don't have at least two segments, `getLogType` will fail. Get the length here. 33 | * Can't seem to nest another intrinsic in `States.ArrayLength`. 34 | */ 35 | const getArrayLen = new Pass(scope, 'GetArrayLen', { 36 | parameters: { 37 | Len: JsonPath.numberAt('States.ArrayLength($.Function.LGParts)'), 38 | }, 39 | resultPath: '$.Array', 40 | }); 41 | 42 | const twoOrMoreParts = new Choice(scope, 'TwoOrMore?'); 43 | 44 | /** 45 | * Get the second part. We're looking for `lambda` to see if this is a LogGroup for a Lambda Function. 46 | */ 47 | const getLogType = new Pass(scope, 'GetLogType', { 48 | parameters: { 49 | LogType: JsonPath.numberAt('States.ArrayGetItem($.Function.LGParts, 1)'), 50 | }, 51 | resultPath: '$.Log', 52 | }); 53 | 54 | const isLambdaLog = new Choice(scope, 'IsLambdaLog?'); 55 | 56 | /** 57 | * The third part of the LogGroup name should be the name of the Lambda Function. 58 | */ 59 | const getFnName = new Pass(scope, 'GetFnName', { 60 | parameters: { 61 | FunctionName: JsonPath.stringAt( 62 | 'States.ArrayGetItem($.Function.LGParts, 2)', 63 | ), 64 | }, 65 | resultPath: '$.Function', 66 | }); 67 | 68 | const isFnPresent = new Choice(scope, 'FunctionPresent?'); 69 | 70 | /** 71 | * Make API call to get the Lambda Function. 72 | * If this call is successful, then we have a LogGroup for an active Lambda Function. 73 | * If we get a 404 back, then the Lambda Function no longer exists and the LogGroup isn't needed. 74 | */ 75 | const getFn = new CallAwsService(scope, 'GetFunction', { 76 | action: 'getFunction', 77 | iamResources: ['*'], 78 | parameters: { 79 | 'FunctionName.$': '$.Function.FunctionName', 80 | }, 81 | resultPath: JsonPath.DISCARD, 82 | service: 'lambda', 83 | }); 84 | 85 | /** 86 | * Make an API call to delete a LogGroup. 87 | * This will end a branch of the Map State. 88 | * The resultSelector indicates the LogGroup was deleted. 89 | */ 90 | const deleteLG = new CallAwsService(scope, 'DeleteLG', { 91 | action: 'deleteLogGroup', 92 | iamAction: 'logs:DeleteLogGroup', 93 | iamResources: ['*'], 94 | parameters: { 95 | LogGroupName: JsonPath.stringAt('$.LogGroupName'), 96 | }, 97 | resultSelector: { 98 | IsDeleted: 1, IsRetained: 0, 99 | }, 100 | service: 'cloudwatchlogs', 101 | }); 102 | 103 | /** 104 | * Make an API call to set retention on the LogGroup. 105 | * This will end a branch of the Map State. 106 | * The resultSelector indicates retention was added. 107 | */ 108 | const addRetention = new CallAwsService(scope, 'AddRetention', { 109 | action: 'putRetentionPolicy', 110 | iamAction: 'logs:PutRetentionPolicy', 111 | iamResources: ['*'], 112 | parameters: { 113 | LogGroupName: JsonPath.stringAt('$.LogGroupName'), 114 | RetentionInDays: retentionInDays, 115 | }, 116 | resultSelector: { 117 | IsDeleted: 0, IsRetained: 1, 118 | }, 119 | service: 'cloudwatchlogs', 120 | }); 121 | 122 | /** 123 | * For any log group that survived the pruning above, check to see if it already has retention. 124 | * If it doesn't have retention, then set it to the `retentionInDays` prop. 125 | */ 126 | const hasRetention = new Choice(scope, 'HasRetention?') 127 | .when(Condition.isNotPresent('$.RetentionInDays'), addRetention) 128 | .otherwise( 129 | new Pass(scope, 'lgtm', { 130 | result: Result.fromObject({ 131 | IsDeleted: 0, IsRetained: 0, 132 | }), 133 | }), 134 | ); 135 | 136 | /** 137 | * Initialize the loop for adding up the stats. 138 | */ 139 | const initStatsLoop = new Pass(scope, 'InitStatsLoop', { 140 | parameters: { 141 | Index: 0, 142 | ResultLen: JsonPath.numberAt('States.ArrayLength($.MapResult)'), 143 | }, 144 | resultPath: '$.Iterator', 145 | }); 146 | 147 | /** 148 | * Get the next item of the array by index. 149 | */ 150 | const getNextResult = new Pass(scope, 'GetNextResult', { 151 | parameters: { 152 | 'Result.$': 'States.ArrayGetItem($.MapResult, $.Iterator.Index)', 153 | }, 154 | resultPath: '$.R', 155 | }); 156 | 157 | /** 158 | * Increment stats based on the `IsDeleted` and `IsRetained` values of the map result. 159 | */ 160 | const incrementStats = new Pass(scope, 'IncrementStats', { 161 | parameters: { 162 | 'LGsDeleted.$': 163 | 'States.MathAdd($.Stats.LGsDeleted, $.R.Result.IsDeleted)', 164 | 'LGsRetained.$': 165 | 'States.MathAdd($.Stats.LGsRetained, $.R.Result.IsRetained)', 166 | LGsSeen: JsonPath.numberAt('$.Stats.LGsSeen'), 167 | }, 168 | resultPath: '$.Stats', 169 | }); 170 | 171 | /** 172 | * Increment the counter for the next loop. 173 | */ 174 | const incrementCounter = new Pass(scope, 'IncrementCounter', { 175 | parameters: { 176 | 'Index.$': 'States.MathAdd($.Iterator.Index, 1)', 177 | ResultLen: JsonPath.numberAt('$.Iterator.ResultLen'), 178 | }, 179 | resultPath: '$.Iterator', 180 | }); 181 | 182 | /** 183 | * Return the Task Token to the parent state machine and pass stats back. 184 | */ 185 | const sendSuccess = new CallAwsService(scope, 'SendSuccess', { 186 | action: 'sendTaskSuccess', 187 | iamAction: 'states:SendTaskSuccess', 188 | iamResources: ['*'], 189 | parameters: { 190 | 'Output.$': '$.Stats', 191 | 'TaskToken.$': '$.Token', 192 | }, 193 | service: 'sfn', 194 | }); 195 | 196 | const hasNextMapResult = new Choice(scope, 'HasNextMapResult?'); 197 | 198 | /** 199 | * Iterate over LogGroups, parsing the name to determine if it's a Lambda Log or not. 200 | */ 201 | map.iterator(getLGParts); 202 | getLGParts.next(getArrayLen); 203 | getArrayLen.next(twoOrMoreParts); 204 | twoOrMoreParts.when( 205 | Condition.numberGreaterThanEquals('$.Array.Len', 2), 206 | getLogType, 207 | ); 208 | twoOrMoreParts.otherwise(hasRetention); 209 | getLogType.next(isLambdaLog); 210 | 211 | /** 212 | * For each Lambda log, get the function name as the 3rd part of the LogGroup name. 213 | * Call the Lambda service to see if that function still exists. 214 | * If the function doesn't exist, then delete the log. 215 | */ 216 | isLambdaLog.when( 217 | Condition.stringEquals('$.Log.LogType', 'lambda'), 218 | getFnName, 219 | ); 220 | isLambdaLog.otherwise(hasRetention); 221 | getFnName.next(isFnPresent); 222 | isFnPresent.when(Condition.isNotNull('$.Function.FunctionName'), getFn); 223 | isFnPresent.otherwise(hasRetention); 224 | getFn.next(hasRetention); 225 | getFn.addCatch(deleteLG, { 226 | errors: [Errors.TASKS_FAILED], 227 | resultPath: JsonPath.DISCARD, 228 | }); 229 | 230 | /** 231 | * After the map completes, loop over the results in order to track how many LogGroups were deleted 232 | * and how many had retention set. 233 | */ 234 | map.next(initStatsLoop); 235 | initStatsLoop.next(hasNextMapResult); 236 | hasNextMapResult.when( 237 | Condition.numberLessThanJsonPath( 238 | '$.Iterator.Index', 239 | '$.Iterator.ResultLen', 240 | ), 241 | getNextResult, 242 | ); 243 | getNextResult.next(incrementStats); 244 | 245 | incrementStats.next(incrementCounter); 246 | incrementCounter.next(hasNextMapResult); 247 | 248 | /** 249 | * Finally send the stats back to the parent state machine. 250 | */ 251 | hasNextMapResult.otherwise(sendSuccess); 252 | 253 | /** 254 | * These calls may be throttled, so retry many times. 255 | * We probably don't need 10 retries, but the default wasn't enough. 256 | */ 257 | addRetention.addRetry({ 258 | maxAttempts: 10, 259 | }); 260 | deleteLG.addRetry({ 261 | maxAttempts: 10, 262 | }); 263 | 264 | const sm = new StateMachine(scope, 'LogsComptrollerRunner', { 265 | definition: map, 266 | stateMachineName: 'logs-comptroller-runner', 267 | tracingEnabled: true, 268 | }); 269 | 270 | /** 271 | * Workaround for CDK throwing a circular dependency error when attempting `grantTaskResponse`. 272 | */ 273 | sm.addToRolePolicy( 274 | new PolicyStatement({ 275 | actions: ['states:SendTaskSuccess'], 276 | resources: ['*'], 277 | }), 278 | ); 279 | 280 | return sm; 281 | }; 282 | -------------------------------------------------------------------------------- /test/__snapshots__/aws-logs-comptroller.test.ts.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`Custom schedule 1`] = ` 4 | Object { 5 | "Mappings": Object { 6 | "ServiceprincipalMap": Object { 7 | "af-south-1": Object { 8 | "states": "states.af-south-1.amazonaws.com", 9 | }, 10 | "ap-east-1": Object { 11 | "states": "states.ap-east-1.amazonaws.com", 12 | }, 13 | "ap-northeast-1": Object { 14 | "states": "states.ap-northeast-1.amazonaws.com", 15 | }, 16 | "ap-northeast-2": Object { 17 | "states": "states.ap-northeast-2.amazonaws.com", 18 | }, 19 | "ap-northeast-3": Object { 20 | "states": "states.ap-northeast-3.amazonaws.com", 21 | }, 22 | "ap-south-1": Object { 23 | "states": "states.ap-south-1.amazonaws.com", 24 | }, 25 | "ap-southeast-1": Object { 26 | "states": "states.ap-southeast-1.amazonaws.com", 27 | }, 28 | "ap-southeast-2": Object { 29 | "states": "states.ap-southeast-2.amazonaws.com", 30 | }, 31 | "ap-southeast-3": Object { 32 | "states": "states.ap-southeast-3.amazonaws.com", 33 | }, 34 | "ca-central-1": Object { 35 | "states": "states.ca-central-1.amazonaws.com", 36 | }, 37 | "cn-north-1": Object { 38 | "states": "states.cn-north-1.amazonaws.com", 39 | }, 40 | "cn-northwest-1": Object { 41 | "states": "states.cn-northwest-1.amazonaws.com", 42 | }, 43 | "eu-central-1": Object { 44 | "states": "states.eu-central-1.amazonaws.com", 45 | }, 46 | "eu-north-1": Object { 47 | "states": "states.eu-north-1.amazonaws.com", 48 | }, 49 | "eu-south-1": Object { 50 | "states": "states.eu-south-1.amazonaws.com", 51 | }, 52 | "eu-south-2": Object { 53 | "states": "states.eu-south-2.amazonaws.com", 54 | }, 55 | "eu-west-1": Object { 56 | "states": "states.eu-west-1.amazonaws.com", 57 | }, 58 | "eu-west-2": Object { 59 | "states": "states.eu-west-2.amazonaws.com", 60 | }, 61 | "eu-west-3": Object { 62 | "states": "states.eu-west-3.amazonaws.com", 63 | }, 64 | "me-south-1": Object { 65 | "states": "states.me-south-1.amazonaws.com", 66 | }, 67 | "sa-east-1": Object { 68 | "states": "states.sa-east-1.amazonaws.com", 69 | }, 70 | "us-east-1": Object { 71 | "states": "states.us-east-1.amazonaws.com", 72 | }, 73 | "us-east-2": Object { 74 | "states": "states.us-east-2.amazonaws.com", 75 | }, 76 | "us-gov-east-1": Object { 77 | "states": "states.us-gov-east-1.amazonaws.com", 78 | }, 79 | "us-gov-west-1": Object { 80 | "states": "states.us-gov-west-1.amazonaws.com", 81 | }, 82 | "us-iso-east-1": Object { 83 | "states": "states.amazonaws.com", 84 | }, 85 | "us-iso-west-1": Object { 86 | "states": "states.amazonaws.com", 87 | }, 88 | "us-isob-east-1": Object { 89 | "states": "states.amazonaws.com", 90 | }, 91 | "us-west-1": Object { 92 | "states": "states.us-west-1.amazonaws.com", 93 | }, 94 | "us-west-2": Object { 95 | "states": "states.us-west-2.amazonaws.com", 96 | }, 97 | }, 98 | }, 99 | "Parameters": Object { 100 | "BootstrapVersion": Object { 101 | "Default": "/cdk-bootstrap/hnb659fds/version", 102 | "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]", 103 | "Type": "AWS::SSM::Parameter::Value", 104 | }, 105 | }, 106 | "Resources": Object { 107 | "LogsComptrollerIteratorEA634D49": Object { 108 | "DependsOn": Array [ 109 | "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 110 | "LogsComptrollerIteratorRole3F6E7D1B", 111 | ], 112 | "Properties": Object { 113 | "DefinitionString": Object { 114 | "Fn::Join": Array [ 115 | "", 116 | Array [ 117 | "{\\"StartAt\\":\\"GetLogGroups\\",\\"States\\":{\\"GetLogGroups\\":{\\"Next\\":\\"SetLGsSeen\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.LG\\",\\"Resource\\":\\"arn:", 118 | Object { 119 | "Ref": "AWS::Partition", 120 | }, 121 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\\",\\"Parameters\\":{}},\\"SetLGsSeen\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted\\":0,\\"LGsRetained\\":0,\\"LGsSeen.$\\":\\"States.ArrayLength($.LG.LogGroups)\\"},\\"Next\\":\\"ExecuteRunner\\"},\\"ExecuteRunner\\":{\\"Next\\":\\"HasNextToken?\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.Stats\\",\\"Resource\\":\\"arn:", 122 | Object { 123 | "Ref": "AWS::Partition", 124 | }, 125 | ":states:::states:startExecution.waitForTaskToken\\",\\"Parameters\\":{\\"Input\\":{\\"LogGroups.$\\":\\"$.LG.LogGroups\\",\\"Stats.$\\":\\"$.Stats\\",\\"Token.$\\":\\"$$.Task.Token\\"},\\"StateMachineArn\\":\\"", 126 | Object { 127 | "Ref": "LogsComptrollerRunner2AB55B01", 128 | }, 129 | "\\"}},\\"AppendTotal\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted.$\\":\\"$.Stats.LGsDeleted\\",\\"LGsRetained.$\\":\\"$.Stats.LGsRetained\\",\\"LGsSeen.$\\":\\"States.MathAdd($.Stats.LGsSeen, States.ArrayLength($.LG.LogGroups))\\"},\\"Next\\":\\"ExecuteRunner\\"},\\"GetNextLogGroups\\":{\\"Next\\":\\"AppendTotal\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.LG\\",\\"Resource\\":\\"arn:", 130 | Object { 131 | "Ref": "AWS::Partition", 132 | }, 133 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\\",\\"Parameters\\":{\\"NextToken.$\\":\\"$.LG.NextToken\\"}},\\"HasNextToken?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.LG.NextToken\\",\\"IsPresent\\":true,\\"Next\\":\\"GetNextLogGroups\\"}],\\"Default\\":\\"Work Complete!\\"},\\"Work Complete!\\":{\\"Type\\":\\"Succeed\\",\\"OutputPath\\":\\"$.Stats\\"}}}", 134 | ], 135 | ], 136 | }, 137 | "RoleArn": Object { 138 | "Fn::GetAtt": Array [ 139 | "LogsComptrollerIteratorRole3F6E7D1B", 140 | "Arn", 141 | ], 142 | }, 143 | "StateMachineName": "logs-comptroller-iterator", 144 | "TracingConfiguration": Object { 145 | "Enabled": true, 146 | }, 147 | }, 148 | "Type": "AWS::StepFunctions::StateMachine", 149 | }, 150 | "LogsComptrollerIteratorEventsRoleDC130B80": Object { 151 | "Properties": Object { 152 | "AssumeRolePolicyDocument": Object { 153 | "Statement": Array [ 154 | Object { 155 | "Action": "sts:AssumeRole", 156 | "Effect": "Allow", 157 | "Principal": Object { 158 | "Service": "events.amazonaws.com", 159 | }, 160 | }, 161 | ], 162 | "Version": "2012-10-17", 163 | }, 164 | }, 165 | "Type": "AWS::IAM::Role", 166 | }, 167 | "LogsComptrollerIteratorEventsRoleDefaultPolicyA56FB7FE": Object { 168 | "Properties": Object { 169 | "PolicyDocument": Object { 170 | "Statement": Array [ 171 | Object { 172 | "Action": "states:StartExecution", 173 | "Effect": "Allow", 174 | "Resource": Object { 175 | "Ref": "LogsComptrollerIteratorEA634D49", 176 | }, 177 | }, 178 | ], 179 | "Version": "2012-10-17", 180 | }, 181 | "PolicyName": "LogsComptrollerIteratorEventsRoleDefaultPolicyA56FB7FE", 182 | "Roles": Array [ 183 | Object { 184 | "Ref": "LogsComptrollerIteratorEventsRoleDC130B80", 185 | }, 186 | ], 187 | }, 188 | "Type": "AWS::IAM::Policy", 189 | }, 190 | "LogsComptrollerIteratorRole3F6E7D1B": Object { 191 | "Properties": Object { 192 | "AssumeRolePolicyDocument": Object { 193 | "Statement": Array [ 194 | Object { 195 | "Action": "sts:AssumeRole", 196 | "Effect": "Allow", 197 | "Principal": Object { 198 | "Service": Object { 199 | "Fn::FindInMap": Array [ 200 | "ServiceprincipalMap", 201 | Object { 202 | "Ref": "AWS::Region", 203 | }, 204 | "states", 205 | ], 206 | }, 207 | }, 208 | }, 209 | ], 210 | "Version": "2012-10-17", 211 | }, 212 | }, 213 | "Type": "AWS::IAM::Role", 214 | }, 215 | "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE": Object { 216 | "Properties": Object { 217 | "PolicyDocument": Object { 218 | "Statement": Array [ 219 | Object { 220 | "Action": Array [ 221 | "xray:PutTraceSegments", 222 | "xray:PutTelemetryRecords", 223 | "xray:GetSamplingRules", 224 | "xray:GetSamplingTargets", 225 | ], 226 | "Effect": "Allow", 227 | "Resource": "*", 228 | }, 229 | Object { 230 | "Action": "logs:DescribeLogGroups", 231 | "Effect": "Allow", 232 | "Resource": "*", 233 | }, 234 | Object { 235 | "Action": "states:StartExecution", 236 | "Effect": "Allow", 237 | "Resource": Object { 238 | "Ref": "LogsComptrollerRunner2AB55B01", 239 | }, 240 | }, 241 | ], 242 | "Version": "2012-10-17", 243 | }, 244 | "PolicyName": "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 245 | "Roles": Array [ 246 | Object { 247 | "Ref": "LogsComptrollerIteratorRole3F6E7D1B", 248 | }, 249 | ], 250 | }, 251 | "Type": "AWS::IAM::Policy", 252 | }, 253 | "LogsComptrollerRunner2AB55B01": Object { 254 | "DependsOn": Array [ 255 | "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 256 | "LogsComptrollerRunnerRoleEB6A3DA6", 257 | ], 258 | "Properties": Object { 259 | "DefinitionString": Object { 260 | "Fn::Join": Array [ 261 | "", 262 | Array [ 263 | "{\\"StartAt\\":\\"Map\\",\\"States\\":{\\"Map\\":{\\"Type\\":\\"Map\\",\\"ResultPath\\":\\"$.MapResult\\",\\"Next\\":\\"InitStatsLoop\\",\\"InputPath\\":\\"$.LogGroups\\",\\"Iterator\\":{\\"StartAt\\":\\"GetLGParts\\",\\"States\\":{\\"GetLGParts\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Function\\",\\"Parameters\\":{\\"LGParts.$\\":\\"States.StringSplit($.LogGroupName, '/')\\"},\\"Next\\":\\"GetArrayLen\\"},\\"GetArrayLen\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Array\\",\\"Parameters\\":{\\"Len.$\\":\\"States.ArrayLength($.Function.LGParts)\\"},\\"Next\\":\\"TwoOrMore?\\"},\\"TwoOrMore?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Array.Len\\",\\"NumericGreaterThanEquals\\":2,\\"Next\\":\\"GetLogType\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetLogType\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Log\\",\\"Parameters\\":{\\"LogType.$\\":\\"States.ArrayGetItem($.Function.LGParts, 1)\\"},\\"Next\\":\\"IsLambdaLog?\\"},\\"IsLambdaLog?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Log.LogType\\",\\"StringEquals\\":\\"lambda\\",\\"Next\\":\\"GetFnName\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetFnName\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Function\\",\\"Parameters\\":{\\"FunctionName.$\\":\\"States.ArrayGetItem($.Function.LGParts, 2)\\"},\\"Next\\":\\"FunctionPresent?\\"},\\"FunctionPresent?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Function.FunctionName\\",\\"IsNull\\":false,\\"Next\\":\\"GetFunction\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetFunction\\":{\\"Next\\":\\"HasRetention?\\",\\"Catch\\":[{\\"ErrorEquals\\":[\\"States.TaskFailed\\"],\\"ResultPath\\":null,\\"Next\\":\\"DeleteLG\\"}],\\"Type\\":\\"Task\\",\\"ResultPath\\":null,\\"Resource\\":\\"arn:", 264 | Object { 265 | "Ref": "AWS::Partition", 266 | }, 267 | ":states:::aws-sdk:lambda:getFunction\\",\\"Parameters\\":{\\"FunctionName.$\\":\\"$.Function.FunctionName\\"}},\\"HasRetention?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.RetentionInDays\\",\\"IsPresent\\":false,\\"Next\\":\\"AddRetention\\"}],\\"Default\\":\\"lgtm\\"},\\"lgtm\\":{\\"Type\\":\\"Pass\\",\\"Result\\":{\\"IsDeleted\\":0,\\"IsRetained\\":0},\\"End\\":true},\\"AddRetention\\":{\\"End\\":true,\\"Retry\\":[{\\"ErrorEquals\\":[\\"States.ALL\\"],\\"MaxAttempts\\":10}],\\"Type\\":\\"Task\\",\\"ResultSelector\\":{\\"IsDeleted\\":0,\\"IsRetained\\":1},\\"Resource\\":\\"arn:", 268 | Object { 269 | "Ref": "AWS::Partition", 270 | }, 271 | ":states:::aws-sdk:cloudwatchlogs:putRetentionPolicy\\",\\"Parameters\\":{\\"LogGroupName.$\\":\\"$.LogGroupName\\",\\"RetentionInDays\\":7}},\\"DeleteLG\\":{\\"End\\":true,\\"Retry\\":[{\\"ErrorEquals\\":[\\"States.ALL\\"],\\"MaxAttempts\\":10}],\\"Type\\":\\"Task\\",\\"ResultSelector\\":{\\"IsDeleted\\":1,\\"IsRetained\\":0},\\"Resource\\":\\"arn:", 272 | Object { 273 | "Ref": "AWS::Partition", 274 | }, 275 | ":states:::aws-sdk:cloudwatchlogs:deleteLogGroup\\",\\"Parameters\\":{\\"LogGroupName.$\\":\\"$.LogGroupName\\"}}}},\\"MaxConcurrency\\":10},\\"InitStatsLoop\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Iterator\\",\\"Parameters\\":{\\"Index\\":0,\\"ResultLen.$\\":\\"States.ArrayLength($.MapResult)\\"},\\"Next\\":\\"HasNextMapResult?\\"},\\"HasNextMapResult?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Iterator.Index\\",\\"NumericLessThanPath\\":\\"$.Iterator.ResultLen\\",\\"Next\\":\\"GetNextResult\\"}],\\"Default\\":\\"SendSuccess\\"},\\"IncrementCounter\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Iterator\\",\\"Parameters\\":{\\"Index.$\\":\\"States.MathAdd($.Iterator.Index, 1)\\",\\"ResultLen.$\\":\\"$.Iterator.ResultLen\\"},\\"Next\\":\\"HasNextMapResult?\\"},\\"IncrementStats\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted.$\\":\\"States.MathAdd($.Stats.LGsDeleted, $.R.Result.IsDeleted)\\",\\"LGsRetained.$\\":\\"States.MathAdd($.Stats.LGsRetained, $.R.Result.IsRetained)\\",\\"LGsSeen.$\\":\\"$.Stats.LGsSeen\\"},\\"Next\\":\\"IncrementCounter\\"},\\"GetNextResult\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.R\\",\\"Parameters\\":{\\"Result.$\\":\\"States.ArrayGetItem($.MapResult, $.Iterator.Index)\\"},\\"Next\\":\\"IncrementStats\\"},\\"SendSuccess\\":{\\"End\\":true,\\"Type\\":\\"Task\\",\\"Resource\\":\\"arn:", 276 | Object { 277 | "Ref": "AWS::Partition", 278 | }, 279 | ":states:::aws-sdk:sfn:sendTaskSuccess\\",\\"Parameters\\":{\\"Output.$\\":\\"$.Stats\\",\\"TaskToken.$\\":\\"$.Token\\"}}}}", 280 | ], 281 | ], 282 | }, 283 | "RoleArn": Object { 284 | "Fn::GetAtt": Array [ 285 | "LogsComptrollerRunnerRoleEB6A3DA6", 286 | "Arn", 287 | ], 288 | }, 289 | "StateMachineName": "logs-comptroller-runner", 290 | "TracingConfiguration": Object { 291 | "Enabled": true, 292 | }, 293 | }, 294 | "Type": "AWS::StepFunctions::StateMachine", 295 | }, 296 | "LogsComptrollerRunnerRoleDefaultPolicyE31C0944": Object { 297 | "Properties": Object { 298 | "PolicyDocument": Object { 299 | "Statement": Array [ 300 | Object { 301 | "Action": Array [ 302 | "xray:PutTraceSegments", 303 | "xray:PutTelemetryRecords", 304 | "xray:GetSamplingRules", 305 | "xray:GetSamplingTargets", 306 | ], 307 | "Effect": "Allow", 308 | "Resource": "*", 309 | }, 310 | Object { 311 | "Action": "states:SendTaskSuccess", 312 | "Effect": "Allow", 313 | "Resource": "*", 314 | }, 315 | Object { 316 | "Action": "lambda:getFunction", 317 | "Effect": "Allow", 318 | "Resource": "*", 319 | }, 320 | Object { 321 | "Action": "logs:PutRetentionPolicy", 322 | "Effect": "Allow", 323 | "Resource": "*", 324 | }, 325 | Object { 326 | "Action": "logs:DeleteLogGroup", 327 | "Effect": "Allow", 328 | "Resource": "*", 329 | }, 330 | ], 331 | "Version": "2012-10-17", 332 | }, 333 | "PolicyName": "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 334 | "Roles": Array [ 335 | Object { 336 | "Ref": "LogsComptrollerRunnerRoleEB6A3DA6", 337 | }, 338 | ], 339 | }, 340 | "Type": "AWS::IAM::Policy", 341 | }, 342 | "LogsComptrollerRunnerRoleEB6A3DA6": Object { 343 | "Properties": Object { 344 | "AssumeRolePolicyDocument": Object { 345 | "Statement": Array [ 346 | Object { 347 | "Action": "sts:AssumeRole", 348 | "Effect": "Allow", 349 | "Principal": Object { 350 | "Service": Object { 351 | "Fn::FindInMap": Array [ 352 | "ServiceprincipalMap", 353 | Object { 354 | "Ref": "AWS::Region", 355 | }, 356 | "states", 357 | ], 358 | }, 359 | }, 360 | }, 361 | ], 362 | "Version": "2012-10-17", 363 | }, 364 | }, 365 | "Type": "AWS::IAM::Role", 366 | }, 367 | "MyTestConstructLogsComptrollerSchedulerA3F37B64": Object { 368 | "Properties": Object { 369 | "ScheduleExpression": "cron(0 4 1 * ? *)", 370 | "State": "ENABLED", 371 | "Targets": Array [ 372 | Object { 373 | "Arn": Object { 374 | "Ref": "LogsComptrollerIteratorEA634D49", 375 | }, 376 | "Id": "Target0", 377 | "RoleArn": Object { 378 | "Fn::GetAtt": Array [ 379 | "LogsComptrollerIteratorEventsRoleDC130B80", 380 | "Arn", 381 | ], 382 | }, 383 | }, 384 | ], 385 | }, 386 | "Type": "AWS::Events::Rule", 387 | }, 388 | }, 389 | "Rules": Object { 390 | "CheckBootstrapVersion": Object { 391 | "Assertions": Array [ 392 | Object { 393 | "Assert": Object { 394 | "Fn::Not": Array [ 395 | Object { 396 | "Fn::Contains": Array [ 397 | Array [ 398 | "1", 399 | "2", 400 | "3", 401 | "4", 402 | "5", 403 | ], 404 | Object { 405 | "Ref": "BootstrapVersion", 406 | }, 407 | ], 408 | }, 409 | ], 410 | }, 411 | "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.", 412 | }, 413 | ], 414 | }, 415 | }, 416 | } 417 | `; 418 | 419 | exports[`Scheduler enabled 1`] = ` 420 | Object { 421 | "Mappings": Object { 422 | "ServiceprincipalMap": Object { 423 | "af-south-1": Object { 424 | "states": "states.af-south-1.amazonaws.com", 425 | }, 426 | "ap-east-1": Object { 427 | "states": "states.ap-east-1.amazonaws.com", 428 | }, 429 | "ap-northeast-1": Object { 430 | "states": "states.ap-northeast-1.amazonaws.com", 431 | }, 432 | "ap-northeast-2": Object { 433 | "states": "states.ap-northeast-2.amazonaws.com", 434 | }, 435 | "ap-northeast-3": Object { 436 | "states": "states.ap-northeast-3.amazonaws.com", 437 | }, 438 | "ap-south-1": Object { 439 | "states": "states.ap-south-1.amazonaws.com", 440 | }, 441 | "ap-southeast-1": Object { 442 | "states": "states.ap-southeast-1.amazonaws.com", 443 | }, 444 | "ap-southeast-2": Object { 445 | "states": "states.ap-southeast-2.amazonaws.com", 446 | }, 447 | "ap-southeast-3": Object { 448 | "states": "states.ap-southeast-3.amazonaws.com", 449 | }, 450 | "ca-central-1": Object { 451 | "states": "states.ca-central-1.amazonaws.com", 452 | }, 453 | "cn-north-1": Object { 454 | "states": "states.cn-north-1.amazonaws.com", 455 | }, 456 | "cn-northwest-1": Object { 457 | "states": "states.cn-northwest-1.amazonaws.com", 458 | }, 459 | "eu-central-1": Object { 460 | "states": "states.eu-central-1.amazonaws.com", 461 | }, 462 | "eu-north-1": Object { 463 | "states": "states.eu-north-1.amazonaws.com", 464 | }, 465 | "eu-south-1": Object { 466 | "states": "states.eu-south-1.amazonaws.com", 467 | }, 468 | "eu-south-2": Object { 469 | "states": "states.eu-south-2.amazonaws.com", 470 | }, 471 | "eu-west-1": Object { 472 | "states": "states.eu-west-1.amazonaws.com", 473 | }, 474 | "eu-west-2": Object { 475 | "states": "states.eu-west-2.amazonaws.com", 476 | }, 477 | "eu-west-3": Object { 478 | "states": "states.eu-west-3.amazonaws.com", 479 | }, 480 | "me-south-1": Object { 481 | "states": "states.me-south-1.amazonaws.com", 482 | }, 483 | "sa-east-1": Object { 484 | "states": "states.sa-east-1.amazonaws.com", 485 | }, 486 | "us-east-1": Object { 487 | "states": "states.us-east-1.amazonaws.com", 488 | }, 489 | "us-east-2": Object { 490 | "states": "states.us-east-2.amazonaws.com", 491 | }, 492 | "us-gov-east-1": Object { 493 | "states": "states.us-gov-east-1.amazonaws.com", 494 | }, 495 | "us-gov-west-1": Object { 496 | "states": "states.us-gov-west-1.amazonaws.com", 497 | }, 498 | "us-iso-east-1": Object { 499 | "states": "states.amazonaws.com", 500 | }, 501 | "us-iso-west-1": Object { 502 | "states": "states.amazonaws.com", 503 | }, 504 | "us-isob-east-1": Object { 505 | "states": "states.amazonaws.com", 506 | }, 507 | "us-west-1": Object { 508 | "states": "states.us-west-1.amazonaws.com", 509 | }, 510 | "us-west-2": Object { 511 | "states": "states.us-west-2.amazonaws.com", 512 | }, 513 | }, 514 | }, 515 | "Parameters": Object { 516 | "BootstrapVersion": Object { 517 | "Default": "/cdk-bootstrap/hnb659fds/version", 518 | "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]", 519 | "Type": "AWS::SSM::Parameter::Value", 520 | }, 521 | }, 522 | "Resources": Object { 523 | "LogsComptrollerIteratorEA634D49": Object { 524 | "DependsOn": Array [ 525 | "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 526 | "LogsComptrollerIteratorRole3F6E7D1B", 527 | ], 528 | "Properties": Object { 529 | "DefinitionString": Object { 530 | "Fn::Join": Array [ 531 | "", 532 | Array [ 533 | "{\\"StartAt\\":\\"GetLogGroups\\",\\"States\\":{\\"GetLogGroups\\":{\\"Next\\":\\"SetLGsSeen\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.LG\\",\\"Resource\\":\\"arn:", 534 | Object { 535 | "Ref": "AWS::Partition", 536 | }, 537 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\\",\\"Parameters\\":{}},\\"SetLGsSeen\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted\\":0,\\"LGsRetained\\":0,\\"LGsSeen.$\\":\\"States.ArrayLength($.LG.LogGroups)\\"},\\"Next\\":\\"ExecuteRunner\\"},\\"ExecuteRunner\\":{\\"Next\\":\\"HasNextToken?\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.Stats\\",\\"Resource\\":\\"arn:", 538 | Object { 539 | "Ref": "AWS::Partition", 540 | }, 541 | ":states:::states:startExecution.waitForTaskToken\\",\\"Parameters\\":{\\"Input\\":{\\"LogGroups.$\\":\\"$.LG.LogGroups\\",\\"Stats.$\\":\\"$.Stats\\",\\"Token.$\\":\\"$$.Task.Token\\"},\\"StateMachineArn\\":\\"", 542 | Object { 543 | "Ref": "LogsComptrollerRunner2AB55B01", 544 | }, 545 | "\\"}},\\"AppendTotal\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted.$\\":\\"$.Stats.LGsDeleted\\",\\"LGsRetained.$\\":\\"$.Stats.LGsRetained\\",\\"LGsSeen.$\\":\\"States.MathAdd($.Stats.LGsSeen, States.ArrayLength($.LG.LogGroups))\\"},\\"Next\\":\\"ExecuteRunner\\"},\\"GetNextLogGroups\\":{\\"Next\\":\\"AppendTotal\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.LG\\",\\"Resource\\":\\"arn:", 546 | Object { 547 | "Ref": "AWS::Partition", 548 | }, 549 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\\",\\"Parameters\\":{\\"NextToken.$\\":\\"$.LG.NextToken\\"}},\\"HasNextToken?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.LG.NextToken\\",\\"IsPresent\\":true,\\"Next\\":\\"GetNextLogGroups\\"}],\\"Default\\":\\"Work Complete!\\"},\\"Work Complete!\\":{\\"Type\\":\\"Succeed\\",\\"OutputPath\\":\\"$.Stats\\"}}}", 550 | ], 551 | ], 552 | }, 553 | "RoleArn": Object { 554 | "Fn::GetAtt": Array [ 555 | "LogsComptrollerIteratorRole3F6E7D1B", 556 | "Arn", 557 | ], 558 | }, 559 | "StateMachineName": "logs-comptroller-iterator", 560 | "TracingConfiguration": Object { 561 | "Enabled": true, 562 | }, 563 | }, 564 | "Type": "AWS::StepFunctions::StateMachine", 565 | }, 566 | "LogsComptrollerIteratorEventsRoleDC130B80": Object { 567 | "Properties": Object { 568 | "AssumeRolePolicyDocument": Object { 569 | "Statement": Array [ 570 | Object { 571 | "Action": "sts:AssumeRole", 572 | "Effect": "Allow", 573 | "Principal": Object { 574 | "Service": "events.amazonaws.com", 575 | }, 576 | }, 577 | ], 578 | "Version": "2012-10-17", 579 | }, 580 | }, 581 | "Type": "AWS::IAM::Role", 582 | }, 583 | "LogsComptrollerIteratorEventsRoleDefaultPolicyA56FB7FE": Object { 584 | "Properties": Object { 585 | "PolicyDocument": Object { 586 | "Statement": Array [ 587 | Object { 588 | "Action": "states:StartExecution", 589 | "Effect": "Allow", 590 | "Resource": Object { 591 | "Ref": "LogsComptrollerIteratorEA634D49", 592 | }, 593 | }, 594 | ], 595 | "Version": "2012-10-17", 596 | }, 597 | "PolicyName": "LogsComptrollerIteratorEventsRoleDefaultPolicyA56FB7FE", 598 | "Roles": Array [ 599 | Object { 600 | "Ref": "LogsComptrollerIteratorEventsRoleDC130B80", 601 | }, 602 | ], 603 | }, 604 | "Type": "AWS::IAM::Policy", 605 | }, 606 | "LogsComptrollerIteratorRole3F6E7D1B": Object { 607 | "Properties": Object { 608 | "AssumeRolePolicyDocument": Object { 609 | "Statement": Array [ 610 | Object { 611 | "Action": "sts:AssumeRole", 612 | "Effect": "Allow", 613 | "Principal": Object { 614 | "Service": Object { 615 | "Fn::FindInMap": Array [ 616 | "ServiceprincipalMap", 617 | Object { 618 | "Ref": "AWS::Region", 619 | }, 620 | "states", 621 | ], 622 | }, 623 | }, 624 | }, 625 | ], 626 | "Version": "2012-10-17", 627 | }, 628 | }, 629 | "Type": "AWS::IAM::Role", 630 | }, 631 | "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE": Object { 632 | "Properties": Object { 633 | "PolicyDocument": Object { 634 | "Statement": Array [ 635 | Object { 636 | "Action": Array [ 637 | "xray:PutTraceSegments", 638 | "xray:PutTelemetryRecords", 639 | "xray:GetSamplingRules", 640 | "xray:GetSamplingTargets", 641 | ], 642 | "Effect": "Allow", 643 | "Resource": "*", 644 | }, 645 | Object { 646 | "Action": "logs:DescribeLogGroups", 647 | "Effect": "Allow", 648 | "Resource": "*", 649 | }, 650 | Object { 651 | "Action": "states:StartExecution", 652 | "Effect": "Allow", 653 | "Resource": Object { 654 | "Ref": "LogsComptrollerRunner2AB55B01", 655 | }, 656 | }, 657 | ], 658 | "Version": "2012-10-17", 659 | }, 660 | "PolicyName": "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 661 | "Roles": Array [ 662 | Object { 663 | "Ref": "LogsComptrollerIteratorRole3F6E7D1B", 664 | }, 665 | ], 666 | }, 667 | "Type": "AWS::IAM::Policy", 668 | }, 669 | "LogsComptrollerRunner2AB55B01": Object { 670 | "DependsOn": Array [ 671 | "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 672 | "LogsComptrollerRunnerRoleEB6A3DA6", 673 | ], 674 | "Properties": Object { 675 | "DefinitionString": Object { 676 | "Fn::Join": Array [ 677 | "", 678 | Array [ 679 | "{\\"StartAt\\":\\"Map\\",\\"States\\":{\\"Map\\":{\\"Type\\":\\"Map\\",\\"ResultPath\\":\\"$.MapResult\\",\\"Next\\":\\"InitStatsLoop\\",\\"InputPath\\":\\"$.LogGroups\\",\\"Iterator\\":{\\"StartAt\\":\\"GetLGParts\\",\\"States\\":{\\"GetLGParts\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Function\\",\\"Parameters\\":{\\"LGParts.$\\":\\"States.StringSplit($.LogGroupName, '/')\\"},\\"Next\\":\\"GetArrayLen\\"},\\"GetArrayLen\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Array\\",\\"Parameters\\":{\\"Len.$\\":\\"States.ArrayLength($.Function.LGParts)\\"},\\"Next\\":\\"TwoOrMore?\\"},\\"TwoOrMore?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Array.Len\\",\\"NumericGreaterThanEquals\\":2,\\"Next\\":\\"GetLogType\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetLogType\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Log\\",\\"Parameters\\":{\\"LogType.$\\":\\"States.ArrayGetItem($.Function.LGParts, 1)\\"},\\"Next\\":\\"IsLambdaLog?\\"},\\"IsLambdaLog?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Log.LogType\\",\\"StringEquals\\":\\"lambda\\",\\"Next\\":\\"GetFnName\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetFnName\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Function\\",\\"Parameters\\":{\\"FunctionName.$\\":\\"States.ArrayGetItem($.Function.LGParts, 2)\\"},\\"Next\\":\\"FunctionPresent?\\"},\\"FunctionPresent?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Function.FunctionName\\",\\"IsNull\\":false,\\"Next\\":\\"GetFunction\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetFunction\\":{\\"Next\\":\\"HasRetention?\\",\\"Catch\\":[{\\"ErrorEquals\\":[\\"States.TaskFailed\\"],\\"ResultPath\\":null,\\"Next\\":\\"DeleteLG\\"}],\\"Type\\":\\"Task\\",\\"ResultPath\\":null,\\"Resource\\":\\"arn:", 680 | Object { 681 | "Ref": "AWS::Partition", 682 | }, 683 | ":states:::aws-sdk:lambda:getFunction\\",\\"Parameters\\":{\\"FunctionName.$\\":\\"$.Function.FunctionName\\"}},\\"HasRetention?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.RetentionInDays\\",\\"IsPresent\\":false,\\"Next\\":\\"AddRetention\\"}],\\"Default\\":\\"lgtm\\"},\\"lgtm\\":{\\"Type\\":\\"Pass\\",\\"Result\\":{\\"IsDeleted\\":0,\\"IsRetained\\":0},\\"End\\":true},\\"AddRetention\\":{\\"End\\":true,\\"Retry\\":[{\\"ErrorEquals\\":[\\"States.ALL\\"],\\"MaxAttempts\\":10}],\\"Type\\":\\"Task\\",\\"ResultSelector\\":{\\"IsDeleted\\":0,\\"IsRetained\\":1},\\"Resource\\":\\"arn:", 684 | Object { 685 | "Ref": "AWS::Partition", 686 | }, 687 | ":states:::aws-sdk:cloudwatchlogs:putRetentionPolicy\\",\\"Parameters\\":{\\"LogGroupName.$\\":\\"$.LogGroupName\\",\\"RetentionInDays\\":7}},\\"DeleteLG\\":{\\"End\\":true,\\"Retry\\":[{\\"ErrorEquals\\":[\\"States.ALL\\"],\\"MaxAttempts\\":10}],\\"Type\\":\\"Task\\",\\"ResultSelector\\":{\\"IsDeleted\\":1,\\"IsRetained\\":0},\\"Resource\\":\\"arn:", 688 | Object { 689 | "Ref": "AWS::Partition", 690 | }, 691 | ":states:::aws-sdk:cloudwatchlogs:deleteLogGroup\\",\\"Parameters\\":{\\"LogGroupName.$\\":\\"$.LogGroupName\\"}}}},\\"MaxConcurrency\\":10},\\"InitStatsLoop\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Iterator\\",\\"Parameters\\":{\\"Index\\":0,\\"ResultLen.$\\":\\"States.ArrayLength($.MapResult)\\"},\\"Next\\":\\"HasNextMapResult?\\"},\\"HasNextMapResult?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Iterator.Index\\",\\"NumericLessThanPath\\":\\"$.Iterator.ResultLen\\",\\"Next\\":\\"GetNextResult\\"}],\\"Default\\":\\"SendSuccess\\"},\\"IncrementCounter\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Iterator\\",\\"Parameters\\":{\\"Index.$\\":\\"States.MathAdd($.Iterator.Index, 1)\\",\\"ResultLen.$\\":\\"$.Iterator.ResultLen\\"},\\"Next\\":\\"HasNextMapResult?\\"},\\"IncrementStats\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted.$\\":\\"States.MathAdd($.Stats.LGsDeleted, $.R.Result.IsDeleted)\\",\\"LGsRetained.$\\":\\"States.MathAdd($.Stats.LGsRetained, $.R.Result.IsRetained)\\",\\"LGsSeen.$\\":\\"$.Stats.LGsSeen\\"},\\"Next\\":\\"IncrementCounter\\"},\\"GetNextResult\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.R\\",\\"Parameters\\":{\\"Result.$\\":\\"States.ArrayGetItem($.MapResult, $.Iterator.Index)\\"},\\"Next\\":\\"IncrementStats\\"},\\"SendSuccess\\":{\\"End\\":true,\\"Type\\":\\"Task\\",\\"Resource\\":\\"arn:", 692 | Object { 693 | "Ref": "AWS::Partition", 694 | }, 695 | ":states:::aws-sdk:sfn:sendTaskSuccess\\",\\"Parameters\\":{\\"Output.$\\":\\"$.Stats\\",\\"TaskToken.$\\":\\"$.Token\\"}}}}", 696 | ], 697 | ], 698 | }, 699 | "RoleArn": Object { 700 | "Fn::GetAtt": Array [ 701 | "LogsComptrollerRunnerRoleEB6A3DA6", 702 | "Arn", 703 | ], 704 | }, 705 | "StateMachineName": "logs-comptroller-runner", 706 | "TracingConfiguration": Object { 707 | "Enabled": true, 708 | }, 709 | }, 710 | "Type": "AWS::StepFunctions::StateMachine", 711 | }, 712 | "LogsComptrollerRunnerRoleDefaultPolicyE31C0944": Object { 713 | "Properties": Object { 714 | "PolicyDocument": Object { 715 | "Statement": Array [ 716 | Object { 717 | "Action": Array [ 718 | "xray:PutTraceSegments", 719 | "xray:PutTelemetryRecords", 720 | "xray:GetSamplingRules", 721 | "xray:GetSamplingTargets", 722 | ], 723 | "Effect": "Allow", 724 | "Resource": "*", 725 | }, 726 | Object { 727 | "Action": "states:SendTaskSuccess", 728 | "Effect": "Allow", 729 | "Resource": "*", 730 | }, 731 | Object { 732 | "Action": "lambda:getFunction", 733 | "Effect": "Allow", 734 | "Resource": "*", 735 | }, 736 | Object { 737 | "Action": "logs:PutRetentionPolicy", 738 | "Effect": "Allow", 739 | "Resource": "*", 740 | }, 741 | Object { 742 | "Action": "logs:DeleteLogGroup", 743 | "Effect": "Allow", 744 | "Resource": "*", 745 | }, 746 | ], 747 | "Version": "2012-10-17", 748 | }, 749 | "PolicyName": "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 750 | "Roles": Array [ 751 | Object { 752 | "Ref": "LogsComptrollerRunnerRoleEB6A3DA6", 753 | }, 754 | ], 755 | }, 756 | "Type": "AWS::IAM::Policy", 757 | }, 758 | "LogsComptrollerRunnerRoleEB6A3DA6": Object { 759 | "Properties": Object { 760 | "AssumeRolePolicyDocument": Object { 761 | "Statement": Array [ 762 | Object { 763 | "Action": "sts:AssumeRole", 764 | "Effect": "Allow", 765 | "Principal": Object { 766 | "Service": Object { 767 | "Fn::FindInMap": Array [ 768 | "ServiceprincipalMap", 769 | Object { 770 | "Ref": "AWS::Region", 771 | }, 772 | "states", 773 | ], 774 | }, 775 | }, 776 | }, 777 | ], 778 | "Version": "2012-10-17", 779 | }, 780 | }, 781 | "Type": "AWS::IAM::Role", 782 | }, 783 | "MyTestConstructLogsComptrollerSchedulerA3F37B64": Object { 784 | "Properties": Object { 785 | "ScheduleExpression": "cron(0 4 * * ? *)", 786 | "State": "ENABLED", 787 | "Targets": Array [ 788 | Object { 789 | "Arn": Object { 790 | "Ref": "LogsComptrollerIteratorEA634D49", 791 | }, 792 | "Id": "Target0", 793 | "RoleArn": Object { 794 | "Fn::GetAtt": Array [ 795 | "LogsComptrollerIteratorEventsRoleDC130B80", 796 | "Arn", 797 | ], 798 | }, 799 | }, 800 | ], 801 | }, 802 | "Type": "AWS::Events::Rule", 803 | }, 804 | }, 805 | "Rules": Object { 806 | "CheckBootstrapVersion": Object { 807 | "Assertions": Array [ 808 | Object { 809 | "Assert": Object { 810 | "Fn::Not": Array [ 811 | Object { 812 | "Fn::Contains": Array [ 813 | Array [ 814 | "1", 815 | "2", 816 | "3", 817 | "4", 818 | "5", 819 | ], 820 | Object { 821 | "Ref": "BootstrapVersion", 822 | }, 823 | ], 824 | }, 825 | ], 826 | }, 827 | "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.", 828 | }, 829 | ], 830 | }, 831 | }, 832 | } 833 | `; 834 | 835 | exports[`State Machines Created 1`] = ` 836 | Object { 837 | "Mappings": Object { 838 | "ServiceprincipalMap": Object { 839 | "af-south-1": Object { 840 | "states": "states.af-south-1.amazonaws.com", 841 | }, 842 | "ap-east-1": Object { 843 | "states": "states.ap-east-1.amazonaws.com", 844 | }, 845 | "ap-northeast-1": Object { 846 | "states": "states.ap-northeast-1.amazonaws.com", 847 | }, 848 | "ap-northeast-2": Object { 849 | "states": "states.ap-northeast-2.amazonaws.com", 850 | }, 851 | "ap-northeast-3": Object { 852 | "states": "states.ap-northeast-3.amazonaws.com", 853 | }, 854 | "ap-south-1": Object { 855 | "states": "states.ap-south-1.amazonaws.com", 856 | }, 857 | "ap-southeast-1": Object { 858 | "states": "states.ap-southeast-1.amazonaws.com", 859 | }, 860 | "ap-southeast-2": Object { 861 | "states": "states.ap-southeast-2.amazonaws.com", 862 | }, 863 | "ap-southeast-3": Object { 864 | "states": "states.ap-southeast-3.amazonaws.com", 865 | }, 866 | "ca-central-1": Object { 867 | "states": "states.ca-central-1.amazonaws.com", 868 | }, 869 | "cn-north-1": Object { 870 | "states": "states.cn-north-1.amazonaws.com", 871 | }, 872 | "cn-northwest-1": Object { 873 | "states": "states.cn-northwest-1.amazonaws.com", 874 | }, 875 | "eu-central-1": Object { 876 | "states": "states.eu-central-1.amazonaws.com", 877 | }, 878 | "eu-north-1": Object { 879 | "states": "states.eu-north-1.amazonaws.com", 880 | }, 881 | "eu-south-1": Object { 882 | "states": "states.eu-south-1.amazonaws.com", 883 | }, 884 | "eu-south-2": Object { 885 | "states": "states.eu-south-2.amazonaws.com", 886 | }, 887 | "eu-west-1": Object { 888 | "states": "states.eu-west-1.amazonaws.com", 889 | }, 890 | "eu-west-2": Object { 891 | "states": "states.eu-west-2.amazonaws.com", 892 | }, 893 | "eu-west-3": Object { 894 | "states": "states.eu-west-3.amazonaws.com", 895 | }, 896 | "me-south-1": Object { 897 | "states": "states.me-south-1.amazonaws.com", 898 | }, 899 | "sa-east-1": Object { 900 | "states": "states.sa-east-1.amazonaws.com", 901 | }, 902 | "us-east-1": Object { 903 | "states": "states.us-east-1.amazonaws.com", 904 | }, 905 | "us-east-2": Object { 906 | "states": "states.us-east-2.amazonaws.com", 907 | }, 908 | "us-gov-east-1": Object { 909 | "states": "states.us-gov-east-1.amazonaws.com", 910 | }, 911 | "us-gov-west-1": Object { 912 | "states": "states.us-gov-west-1.amazonaws.com", 913 | }, 914 | "us-iso-east-1": Object { 915 | "states": "states.amazonaws.com", 916 | }, 917 | "us-iso-west-1": Object { 918 | "states": "states.amazonaws.com", 919 | }, 920 | "us-isob-east-1": Object { 921 | "states": "states.amazonaws.com", 922 | }, 923 | "us-west-1": Object { 924 | "states": "states.us-west-1.amazonaws.com", 925 | }, 926 | "us-west-2": Object { 927 | "states": "states.us-west-2.amazonaws.com", 928 | }, 929 | }, 930 | }, 931 | "Parameters": Object { 932 | "BootstrapVersion": Object { 933 | "Default": "/cdk-bootstrap/hnb659fds/version", 934 | "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]", 935 | "Type": "AWS::SSM::Parameter::Value", 936 | }, 937 | }, 938 | "Resources": Object { 939 | "LogsComptrollerIteratorEA634D49": Object { 940 | "DependsOn": Array [ 941 | "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 942 | "LogsComptrollerIteratorRole3F6E7D1B", 943 | ], 944 | "Properties": Object { 945 | "DefinitionString": Object { 946 | "Fn::Join": Array [ 947 | "", 948 | Array [ 949 | "{\\"StartAt\\":\\"GetLogGroups\\",\\"States\\":{\\"GetLogGroups\\":{\\"Next\\":\\"SetLGsSeen\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.LG\\",\\"Resource\\":\\"arn:", 950 | Object { 951 | "Ref": "AWS::Partition", 952 | }, 953 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\\",\\"Parameters\\":{}},\\"SetLGsSeen\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted\\":0,\\"LGsRetained\\":0,\\"LGsSeen.$\\":\\"States.ArrayLength($.LG.LogGroups)\\"},\\"Next\\":\\"ExecuteRunner\\"},\\"ExecuteRunner\\":{\\"Next\\":\\"HasNextToken?\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.Stats\\",\\"Resource\\":\\"arn:", 954 | Object { 955 | "Ref": "AWS::Partition", 956 | }, 957 | ":states:::states:startExecution.waitForTaskToken\\",\\"Parameters\\":{\\"Input\\":{\\"LogGroups.$\\":\\"$.LG.LogGroups\\",\\"Stats.$\\":\\"$.Stats\\",\\"Token.$\\":\\"$$.Task.Token\\"},\\"StateMachineArn\\":\\"", 958 | Object { 959 | "Ref": "LogsComptrollerRunner2AB55B01", 960 | }, 961 | "\\"}},\\"AppendTotal\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted.$\\":\\"$.Stats.LGsDeleted\\",\\"LGsRetained.$\\":\\"$.Stats.LGsRetained\\",\\"LGsSeen.$\\":\\"States.MathAdd($.Stats.LGsSeen, States.ArrayLength($.LG.LogGroups))\\"},\\"Next\\":\\"ExecuteRunner\\"},\\"GetNextLogGroups\\":{\\"Next\\":\\"AppendTotal\\",\\"Type\\":\\"Task\\",\\"ResultPath\\":\\"$.LG\\",\\"Resource\\":\\"arn:", 962 | Object { 963 | "Ref": "AWS::Partition", 964 | }, 965 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\\",\\"Parameters\\":{\\"NextToken.$\\":\\"$.LG.NextToken\\"}},\\"HasNextToken?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.LG.NextToken\\",\\"IsPresent\\":true,\\"Next\\":\\"GetNextLogGroups\\"}],\\"Default\\":\\"Work Complete!\\"},\\"Work Complete!\\":{\\"Type\\":\\"Succeed\\",\\"OutputPath\\":\\"$.Stats\\"}}}", 966 | ], 967 | ], 968 | }, 969 | "RoleArn": Object { 970 | "Fn::GetAtt": Array [ 971 | "LogsComptrollerIteratorRole3F6E7D1B", 972 | "Arn", 973 | ], 974 | }, 975 | "StateMachineName": "logs-comptroller-iterator", 976 | "TracingConfiguration": Object { 977 | "Enabled": true, 978 | }, 979 | }, 980 | "Type": "AWS::StepFunctions::StateMachine", 981 | }, 982 | "LogsComptrollerIteratorRole3F6E7D1B": Object { 983 | "Properties": Object { 984 | "AssumeRolePolicyDocument": Object { 985 | "Statement": Array [ 986 | Object { 987 | "Action": "sts:AssumeRole", 988 | "Effect": "Allow", 989 | "Principal": Object { 990 | "Service": Object { 991 | "Fn::FindInMap": Array [ 992 | "ServiceprincipalMap", 993 | Object { 994 | "Ref": "AWS::Region", 995 | }, 996 | "states", 997 | ], 998 | }, 999 | }, 1000 | }, 1001 | ], 1002 | "Version": "2012-10-17", 1003 | }, 1004 | }, 1005 | "Type": "AWS::IAM::Role", 1006 | }, 1007 | "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE": Object { 1008 | "Properties": Object { 1009 | "PolicyDocument": Object { 1010 | "Statement": Array [ 1011 | Object { 1012 | "Action": Array [ 1013 | "xray:PutTraceSegments", 1014 | "xray:PutTelemetryRecords", 1015 | "xray:GetSamplingRules", 1016 | "xray:GetSamplingTargets", 1017 | ], 1018 | "Effect": "Allow", 1019 | "Resource": "*", 1020 | }, 1021 | Object { 1022 | "Action": "logs:DescribeLogGroups", 1023 | "Effect": "Allow", 1024 | "Resource": "*", 1025 | }, 1026 | Object { 1027 | "Action": "states:StartExecution", 1028 | "Effect": "Allow", 1029 | "Resource": Object { 1030 | "Ref": "LogsComptrollerRunner2AB55B01", 1031 | }, 1032 | }, 1033 | ], 1034 | "Version": "2012-10-17", 1035 | }, 1036 | "PolicyName": "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 1037 | "Roles": Array [ 1038 | Object { 1039 | "Ref": "LogsComptrollerIteratorRole3F6E7D1B", 1040 | }, 1041 | ], 1042 | }, 1043 | "Type": "AWS::IAM::Policy", 1044 | }, 1045 | "LogsComptrollerRunner2AB55B01": Object { 1046 | "DependsOn": Array [ 1047 | "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 1048 | "LogsComptrollerRunnerRoleEB6A3DA6", 1049 | ], 1050 | "Properties": Object { 1051 | "DefinitionString": Object { 1052 | "Fn::Join": Array [ 1053 | "", 1054 | Array [ 1055 | "{\\"StartAt\\":\\"Map\\",\\"States\\":{\\"Map\\":{\\"Type\\":\\"Map\\",\\"ResultPath\\":\\"$.MapResult\\",\\"Next\\":\\"InitStatsLoop\\",\\"InputPath\\":\\"$.LogGroups\\",\\"Iterator\\":{\\"StartAt\\":\\"GetLGParts\\",\\"States\\":{\\"GetLGParts\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Function\\",\\"Parameters\\":{\\"LGParts.$\\":\\"States.StringSplit($.LogGroupName, '/')\\"},\\"Next\\":\\"GetArrayLen\\"},\\"GetArrayLen\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Array\\",\\"Parameters\\":{\\"Len.$\\":\\"States.ArrayLength($.Function.LGParts)\\"},\\"Next\\":\\"TwoOrMore?\\"},\\"TwoOrMore?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Array.Len\\",\\"NumericGreaterThanEquals\\":2,\\"Next\\":\\"GetLogType\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetLogType\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Log\\",\\"Parameters\\":{\\"LogType.$\\":\\"States.ArrayGetItem($.Function.LGParts, 1)\\"},\\"Next\\":\\"IsLambdaLog?\\"},\\"IsLambdaLog?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Log.LogType\\",\\"StringEquals\\":\\"lambda\\",\\"Next\\":\\"GetFnName\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetFnName\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Function\\",\\"Parameters\\":{\\"FunctionName.$\\":\\"States.ArrayGetItem($.Function.LGParts, 2)\\"},\\"Next\\":\\"FunctionPresent?\\"},\\"FunctionPresent?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Function.FunctionName\\",\\"IsNull\\":false,\\"Next\\":\\"GetFunction\\"}],\\"Default\\":\\"HasRetention?\\"},\\"GetFunction\\":{\\"Next\\":\\"HasRetention?\\",\\"Catch\\":[{\\"ErrorEquals\\":[\\"States.TaskFailed\\"],\\"ResultPath\\":null,\\"Next\\":\\"DeleteLG\\"}],\\"Type\\":\\"Task\\",\\"ResultPath\\":null,\\"Resource\\":\\"arn:", 1056 | Object { 1057 | "Ref": "AWS::Partition", 1058 | }, 1059 | ":states:::aws-sdk:lambda:getFunction\\",\\"Parameters\\":{\\"FunctionName.$\\":\\"$.Function.FunctionName\\"}},\\"HasRetention?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.RetentionInDays\\",\\"IsPresent\\":false,\\"Next\\":\\"AddRetention\\"}],\\"Default\\":\\"lgtm\\"},\\"lgtm\\":{\\"Type\\":\\"Pass\\",\\"Result\\":{\\"IsDeleted\\":0,\\"IsRetained\\":0},\\"End\\":true},\\"AddRetention\\":{\\"End\\":true,\\"Retry\\":[{\\"ErrorEquals\\":[\\"States.ALL\\"],\\"MaxAttempts\\":10}],\\"Type\\":\\"Task\\",\\"ResultSelector\\":{\\"IsDeleted\\":0,\\"IsRetained\\":1},\\"Resource\\":\\"arn:", 1060 | Object { 1061 | "Ref": "AWS::Partition", 1062 | }, 1063 | ":states:::aws-sdk:cloudwatchlogs:putRetentionPolicy\\",\\"Parameters\\":{\\"LogGroupName.$\\":\\"$.LogGroupName\\",\\"RetentionInDays\\":7}},\\"DeleteLG\\":{\\"End\\":true,\\"Retry\\":[{\\"ErrorEquals\\":[\\"States.ALL\\"],\\"MaxAttempts\\":10}],\\"Type\\":\\"Task\\",\\"ResultSelector\\":{\\"IsDeleted\\":1,\\"IsRetained\\":0},\\"Resource\\":\\"arn:", 1064 | Object { 1065 | "Ref": "AWS::Partition", 1066 | }, 1067 | ":states:::aws-sdk:cloudwatchlogs:deleteLogGroup\\",\\"Parameters\\":{\\"LogGroupName.$\\":\\"$.LogGroupName\\"}}}},\\"MaxConcurrency\\":10},\\"InitStatsLoop\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Iterator\\",\\"Parameters\\":{\\"Index\\":0,\\"ResultLen.$\\":\\"States.ArrayLength($.MapResult)\\"},\\"Next\\":\\"HasNextMapResult?\\"},\\"HasNextMapResult?\\":{\\"Type\\":\\"Choice\\",\\"Choices\\":[{\\"Variable\\":\\"$.Iterator.Index\\",\\"NumericLessThanPath\\":\\"$.Iterator.ResultLen\\",\\"Next\\":\\"GetNextResult\\"}],\\"Default\\":\\"SendSuccess\\"},\\"IncrementCounter\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Iterator\\",\\"Parameters\\":{\\"Index.$\\":\\"States.MathAdd($.Iterator.Index, 1)\\",\\"ResultLen.$\\":\\"$.Iterator.ResultLen\\"},\\"Next\\":\\"HasNextMapResult?\\"},\\"IncrementStats\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.Stats\\",\\"Parameters\\":{\\"LGsDeleted.$\\":\\"States.MathAdd($.Stats.LGsDeleted, $.R.Result.IsDeleted)\\",\\"LGsRetained.$\\":\\"States.MathAdd($.Stats.LGsRetained, $.R.Result.IsRetained)\\",\\"LGsSeen.$\\":\\"$.Stats.LGsSeen\\"},\\"Next\\":\\"IncrementCounter\\"},\\"GetNextResult\\":{\\"Type\\":\\"Pass\\",\\"ResultPath\\":\\"$.R\\",\\"Parameters\\":{\\"Result.$\\":\\"States.ArrayGetItem($.MapResult, $.Iterator.Index)\\"},\\"Next\\":\\"IncrementStats\\"},\\"SendSuccess\\":{\\"End\\":true,\\"Type\\":\\"Task\\",\\"Resource\\":\\"arn:", 1068 | Object { 1069 | "Ref": "AWS::Partition", 1070 | }, 1071 | ":states:::aws-sdk:sfn:sendTaskSuccess\\",\\"Parameters\\":{\\"Output.$\\":\\"$.Stats\\",\\"TaskToken.$\\":\\"$.Token\\"}}}}", 1072 | ], 1073 | ], 1074 | }, 1075 | "RoleArn": Object { 1076 | "Fn::GetAtt": Array [ 1077 | "LogsComptrollerRunnerRoleEB6A3DA6", 1078 | "Arn", 1079 | ], 1080 | }, 1081 | "StateMachineName": "logs-comptroller-runner", 1082 | "TracingConfiguration": Object { 1083 | "Enabled": true, 1084 | }, 1085 | }, 1086 | "Type": "AWS::StepFunctions::StateMachine", 1087 | }, 1088 | "LogsComptrollerRunnerRoleDefaultPolicyE31C0944": Object { 1089 | "Properties": Object { 1090 | "PolicyDocument": Object { 1091 | "Statement": Array [ 1092 | Object { 1093 | "Action": Array [ 1094 | "xray:PutTraceSegments", 1095 | "xray:PutTelemetryRecords", 1096 | "xray:GetSamplingRules", 1097 | "xray:GetSamplingTargets", 1098 | ], 1099 | "Effect": "Allow", 1100 | "Resource": "*", 1101 | }, 1102 | Object { 1103 | "Action": "states:SendTaskSuccess", 1104 | "Effect": "Allow", 1105 | "Resource": "*", 1106 | }, 1107 | Object { 1108 | "Action": "lambda:getFunction", 1109 | "Effect": "Allow", 1110 | "Resource": "*", 1111 | }, 1112 | Object { 1113 | "Action": "logs:PutRetentionPolicy", 1114 | "Effect": "Allow", 1115 | "Resource": "*", 1116 | }, 1117 | Object { 1118 | "Action": "logs:DeleteLogGroup", 1119 | "Effect": "Allow", 1120 | "Resource": "*", 1121 | }, 1122 | ], 1123 | "Version": "2012-10-17", 1124 | }, 1125 | "PolicyName": "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 1126 | "Roles": Array [ 1127 | Object { 1128 | "Ref": "LogsComptrollerRunnerRoleEB6A3DA6", 1129 | }, 1130 | ], 1131 | }, 1132 | "Type": "AWS::IAM::Policy", 1133 | }, 1134 | "LogsComptrollerRunnerRoleEB6A3DA6": Object { 1135 | "Properties": Object { 1136 | "AssumeRolePolicyDocument": Object { 1137 | "Statement": Array [ 1138 | Object { 1139 | "Action": "sts:AssumeRole", 1140 | "Effect": "Allow", 1141 | "Principal": Object { 1142 | "Service": Object { 1143 | "Fn::FindInMap": Array [ 1144 | "ServiceprincipalMap", 1145 | Object { 1146 | "Ref": "AWS::Region", 1147 | }, 1148 | "states", 1149 | ], 1150 | }, 1151 | }, 1152 | }, 1153 | ], 1154 | "Version": "2012-10-17", 1155 | }, 1156 | }, 1157 | "Type": "AWS::IAM::Role", 1158 | }, 1159 | }, 1160 | "Rules": Object { 1161 | "CheckBootstrapVersion": Object { 1162 | "Assertions": Array [ 1163 | Object { 1164 | "Assert": Object { 1165 | "Fn::Not": Array [ 1166 | Object { 1167 | "Fn::Contains": Array [ 1168 | Array [ 1169 | "1", 1170 | "2", 1171 | "3", 1172 | "4", 1173 | "5", 1174 | ], 1175 | Object { 1176 | "Ref": "BootstrapVersion", 1177 | }, 1178 | ], 1179 | }, 1180 | ], 1181 | }, 1182 | "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.", 1183 | }, 1184 | ], 1185 | }, 1186 | }, 1187 | } 1188 | `; 1189 | -------------------------------------------------------------------------------- /test/aws-logs-comptroller.test.ts: -------------------------------------------------------------------------------- 1 | import { App, Stack } from 'aws-cdk-lib'; 2 | import { Template } from 'aws-cdk-lib/assertions'; 3 | import { Schedule } from 'aws-cdk-lib/aws-events'; 4 | 5 | import { AwsLogsComptroller } from '../src'; 6 | 7 | test('State Machines Created', () => { 8 | const app = new App(); 9 | const stack = new Stack(app, 'TestStack'); 10 | new AwsLogsComptroller(stack, 'MyTestConstruct'); 11 | const template = Template.fromStack(stack); 12 | 13 | template.resourceCountIs('AWS::StepFunctions::StateMachine', 2); 14 | 15 | expect(template).toMatchSnapshot(); 16 | }); 17 | 18 | test('Scheduler enabled', () => { 19 | const app = new App(); 20 | const stack = new Stack(app, 'TestStack'); 21 | new AwsLogsComptroller(stack, 'MyTestConstruct', { 22 | schedule: true, 23 | }); 24 | const template = Template.fromStack(stack); 25 | 26 | template.resourceCountIs('AWS::Events::Rule', 1); 27 | template.resourceCountIs('AWS::StepFunctions::StateMachine', 2); 28 | 29 | expect(template).toMatchSnapshot(); 30 | }); 31 | 32 | test('Custom schedule', () => { 33 | const app = new App(); 34 | const stack = new Stack(app, 'TestStack'); 35 | new AwsLogsComptroller(stack, 'MyTestConstruct', { 36 | schedule: Schedule.cron({ 37 | day: '1', hour: '4', minute: '0', 38 | }), 39 | }); 40 | const template = Template.fromStack(stack); 41 | 42 | template.resourceCountIs('AWS::Events::Rule', 1); 43 | template.resourceCountIs('AWS::StepFunctions::StateMachine', 2); 44 | 45 | expect(template).toMatchSnapshot(); 46 | }); 47 | -------------------------------------------------------------------------------- /test/default.integ.snapshot/IntegDefaultTestDeployAssert4E6713E1.assets.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "21.0.0", 3 | "files": { 4 | "7566f03a4e5c13b6df8450112ed7d71ab166d8d211e5b501e15b758165d735b5": { 5 | "source": { 6 | "path": "asset.7566f03a4e5c13b6df8450112ed7d71ab166d8d211e5b501e15b758165d735b5.bundle", 7 | "packaging": "zip" 8 | }, 9 | "destinations": { 10 | "current_account-current_region": { 11 | "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", 12 | "objectKey": "7566f03a4e5c13b6df8450112ed7d71ab166d8d211e5b501e15b758165d735b5.zip", 13 | "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" 14 | } 15 | } 16 | }, 17 | "03b035eb49023734b1ff203dece1592d4038d444bca9a84d7896e653c354347e": { 18 | "source": { 19 | "path": "IntegDefaultTestDeployAssert4E6713E1.template.json", 20 | "packaging": "file" 21 | }, 22 | "destinations": { 23 | "current_account-current_region": { 24 | "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", 25 | "objectKey": "03b035eb49023734b1ff203dece1592d4038d444bca9a84d7896e653c354347e.json", 26 | "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" 27 | } 28 | } 29 | } 30 | }, 31 | "dockerImages": {} 32 | } -------------------------------------------------------------------------------- /test/default.integ.snapshot/IntegDefaultTestDeployAssert4E6713E1.template.json: -------------------------------------------------------------------------------- 1 | { 2 | "Resources": { 3 | "AwsApiCallStepFunctionsstartExecution": { 4 | "Type": "Custom::DeployAssert@SdkCallStepFunctionsstartExecution", 5 | "Properties": { 6 | "ServiceToken": { 7 | "Fn::GetAtt": [ 8 | "SingletonFunction1488541a7b23466481b69b4408076b81HandlerCD40AE9F", 9 | "Arn" 10 | ] 11 | }, 12 | "service": "StepFunctions", 13 | "api": "startExecution", 14 | "parameters": { 15 | "stateMachineArn": { 16 | "Fn::Join": [ 17 | "", 18 | [ 19 | "arn:", 20 | { 21 | "Ref": "AWS::Partition" 22 | }, 23 | ":states:", 24 | { 25 | "Ref": "AWS::Region" 26 | }, 27 | ":", 28 | { 29 | "Ref": "AWS::AccountId" 30 | }, 31 | ":stateMachine:logs-comptroller-iterator" 32 | ] 33 | ] 34 | } 35 | }, 36 | "flattenResponse": "true", 37 | "salt": "1665956039458" 38 | }, 39 | "UpdateReplacePolicy": "Delete", 40 | "DeletionPolicy": "Delete" 41 | }, 42 | "SingletonFunction1488541a7b23466481b69b4408076b81Role37ABCE73": { 43 | "Type": "AWS::IAM::Role", 44 | "Properties": { 45 | "AssumeRolePolicyDocument": { 46 | "Version": "2012-10-17", 47 | "Statement": [ 48 | { 49 | "Action": "sts:AssumeRole", 50 | "Effect": "Allow", 51 | "Principal": { 52 | "Service": "lambda.amazonaws.com" 53 | } 54 | } 55 | ] 56 | }, 57 | "ManagedPolicyArns": [ 58 | { 59 | "Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" 60 | } 61 | ], 62 | "Policies": [ 63 | { 64 | "PolicyName": "Inline", 65 | "PolicyDocument": { 66 | "Version": "2012-10-17", 67 | "Statement": [ 68 | { 69 | "Action": [ 70 | "states:StartExecution" 71 | ], 72 | "Effect": "Allow", 73 | "Resource": [ 74 | "*" 75 | ] 76 | }, 77 | { 78 | "Action": [ 79 | "states:DescribeExecution" 80 | ], 81 | "Effect": "Allow", 82 | "Resource": [ 83 | "*" 84 | ] 85 | }, 86 | { 87 | "Action": [ 88 | "states:StartExecution" 89 | ], 90 | "Effect": "Allow", 91 | "Resource": [ 92 | "*" 93 | ] 94 | } 95 | ] 96 | } 97 | } 98 | ] 99 | } 100 | }, 101 | "SingletonFunction1488541a7b23466481b69b4408076b81HandlerCD40AE9F": { 102 | "Type": "AWS::Lambda::Function", 103 | "Properties": { 104 | "Runtime": "nodejs14.x", 105 | "Code": { 106 | "S3Bucket": { 107 | "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" 108 | }, 109 | "S3Key": "7566f03a4e5c13b6df8450112ed7d71ab166d8d211e5b501e15b758165d735b5.zip" 110 | }, 111 | "Timeout": 120, 112 | "Handler": "index.handler", 113 | "Role": { 114 | "Fn::GetAtt": [ 115 | "SingletonFunction1488541a7b23466481b69b4408076b81Role37ABCE73", 116 | "Arn" 117 | ] 118 | } 119 | } 120 | }, 121 | "AwsApiCallStepFunctionsdescribeExecution": { 122 | "Type": "Custom::DeployAssert@SdkCallStepFunctionsdescribeExecution", 123 | "Properties": { 124 | "ServiceToken": { 125 | "Fn::GetAtt": [ 126 | "SingletonFunction1488541a7b23466481b69b4408076b81HandlerCD40AE9F", 127 | "Arn" 128 | ] 129 | }, 130 | "service": "StepFunctions", 131 | "api": "describeExecution", 132 | "expected": "{\"$ObjectLike\":{\"status\":\"SUCCEEDED\"}}", 133 | "stateMachineArn": { 134 | "Ref": "AwsApiCallStepFunctionsdescribeExecutionWaitFor3BA9FD23" 135 | }, 136 | "parameters": { 137 | "executionArn": { 138 | "Fn::GetAtt": [ 139 | "AwsApiCallStepFunctionsstartExecution", 140 | "apiCallResponse.executionArn" 141 | ] 142 | } 143 | }, 144 | "flattenResponse": "false", 145 | "salt": "1665956039459" 146 | }, 147 | "UpdateReplacePolicy": "Delete", 148 | "DeletionPolicy": "Delete" 149 | }, 150 | "AwsApiCallStepFunctionsdescribeExecutionWaitForIsCompleteProviderInvoke90973546": { 151 | "Type": "AWS::Lambda::Permission", 152 | "Properties": { 153 | "Action": "lambda:InvokeFunction", 154 | "FunctionName": { 155 | "Fn::GetAtt": [ 156 | "SingletonFunction76b3e830a873425f8453eddd85c86925Handler81461ECE", 157 | "Arn" 158 | ] 159 | }, 160 | "Principal": { 161 | "Fn::GetAtt": [ 162 | "AwsApiCallStepFunctionsdescribeExecutionWaitForRole0A7F8888", 163 | "Arn" 164 | ] 165 | } 166 | } 167 | }, 168 | "AwsApiCallStepFunctionsdescribeExecutionWaitForTimeoutProviderInvoke59993CEE": { 169 | "Type": "AWS::Lambda::Permission", 170 | "Properties": { 171 | "Action": "lambda:InvokeFunction", 172 | "FunctionName": { 173 | "Fn::GetAtt": [ 174 | "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41aHandlerADF3E6EA", 175 | "Arn" 176 | ] 177 | }, 178 | "Principal": { 179 | "Fn::GetAtt": [ 180 | "AwsApiCallStepFunctionsdescribeExecutionWaitForRole0A7F8888", 181 | "Arn" 182 | ] 183 | } 184 | } 185 | }, 186 | "AwsApiCallStepFunctionsdescribeExecutionWaitForRole0A7F8888": { 187 | "Type": "AWS::IAM::Role", 188 | "Properties": { 189 | "AssumeRolePolicyDocument": { 190 | "Version": "2012-10-17", 191 | "Statement": [ 192 | { 193 | "Action": "sts:AssumeRole", 194 | "Effect": "Allow", 195 | "Principal": { 196 | "Service": "states.amazonaws.com" 197 | } 198 | } 199 | ] 200 | }, 201 | "Policies": [ 202 | { 203 | "PolicyName": "InlineInvokeFunctions", 204 | "PolicyDocument": { 205 | "Version": "2012-10-17", 206 | "Statement": [ 207 | { 208 | "Action": "lambda:InvokeFunction", 209 | "Effect": "Allow", 210 | "Resource": [ 211 | { 212 | "Fn::GetAtt": [ 213 | "SingletonFunction76b3e830a873425f8453eddd85c86925Handler81461ECE", 214 | "Arn" 215 | ] 216 | }, 217 | { 218 | "Fn::GetAtt": [ 219 | "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41aHandlerADF3E6EA", 220 | "Arn" 221 | ] 222 | } 223 | ] 224 | } 225 | ] 226 | } 227 | } 228 | ] 229 | } 230 | }, 231 | "AwsApiCallStepFunctionsdescribeExecutionWaitFor3BA9FD23": { 232 | "Type": "AWS::StepFunctions::StateMachine", 233 | "Properties": { 234 | "DefinitionString": { 235 | "Fn::Join": [ 236 | "", 237 | [ 238 | "{\"StartAt\":\"framework-isComplete-task\",\"States\":{\"framework-isComplete-task\":{\"End\":true,\"Retry\":[{\"ErrorEquals\":[\"States.ALL\"],\"IntervalSeconds\":5,\"MaxAttempts\":8,\"BackoffRate\":1}],\"Catch\":[{\"ErrorEquals\":[\"States.ALL\"],\"Next\":\"framework-onTimeout-task\"}],\"Type\":\"Task\",\"Resource\":\"", 239 | { 240 | "Fn::GetAtt": [ 241 | "SingletonFunction76b3e830a873425f8453eddd85c86925Handler81461ECE", 242 | "Arn" 243 | ] 244 | }, 245 | "\"},\"framework-onTimeout-task\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"", 246 | { 247 | "Fn::GetAtt": [ 248 | "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41aHandlerADF3E6EA", 249 | "Arn" 250 | ] 251 | }, 252 | "\"}}}" 253 | ] 254 | ] 255 | }, 256 | "RoleArn": { 257 | "Fn::GetAtt": [ 258 | "AwsApiCallStepFunctionsdescribeExecutionWaitForRole0A7F8888", 259 | "Arn" 260 | ] 261 | } 262 | }, 263 | "DependsOn": [ 264 | "AwsApiCallStepFunctionsdescribeExecutionWaitForRole0A7F8888" 265 | ] 266 | }, 267 | "SingletonFunction76b3e830a873425f8453eddd85c86925Role918961BB": { 268 | "Type": "AWS::IAM::Role", 269 | "Properties": { 270 | "AssumeRolePolicyDocument": { 271 | "Version": "2012-10-17", 272 | "Statement": [ 273 | { 274 | "Action": "sts:AssumeRole", 275 | "Effect": "Allow", 276 | "Principal": { 277 | "Service": "lambda.amazonaws.com" 278 | } 279 | } 280 | ] 281 | }, 282 | "ManagedPolicyArns": [ 283 | { 284 | "Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" 285 | } 286 | ], 287 | "Policies": [ 288 | { 289 | "PolicyName": "Inline", 290 | "PolicyDocument": { 291 | "Version": "2012-10-17", 292 | "Statement": [ 293 | { 294 | "Action": [ 295 | "states:DescribeExecution" 296 | ], 297 | "Effect": "Allow", 298 | "Resource": [ 299 | "*" 300 | ] 301 | } 302 | ] 303 | } 304 | } 305 | ] 306 | } 307 | }, 308 | "SingletonFunction76b3e830a873425f8453eddd85c86925Handler81461ECE": { 309 | "Type": "AWS::Lambda::Function", 310 | "Properties": { 311 | "Runtime": "nodejs14.x", 312 | "Code": { 313 | "S3Bucket": { 314 | "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" 315 | }, 316 | "S3Key": "7566f03a4e5c13b6df8450112ed7d71ab166d8d211e5b501e15b758165d735b5.zip" 317 | }, 318 | "Timeout": 120, 319 | "Handler": "index.isComplete", 320 | "Role": { 321 | "Fn::GetAtt": [ 322 | "SingletonFunction76b3e830a873425f8453eddd85c86925Role918961BB", 323 | "Arn" 324 | ] 325 | } 326 | } 327 | }, 328 | "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41aRoleB84BD8CE": { 329 | "Type": "AWS::IAM::Role", 330 | "Properties": { 331 | "AssumeRolePolicyDocument": { 332 | "Version": "2012-10-17", 333 | "Statement": [ 334 | { 335 | "Action": "sts:AssumeRole", 336 | "Effect": "Allow", 337 | "Principal": { 338 | "Service": "lambda.amazonaws.com" 339 | } 340 | } 341 | ] 342 | }, 343 | "ManagedPolicyArns": [ 344 | { 345 | "Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" 346 | } 347 | ] 348 | } 349 | }, 350 | "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41aHandlerADF3E6EA": { 351 | "Type": "AWS::Lambda::Function", 352 | "Properties": { 353 | "Runtime": "nodejs14.x", 354 | "Code": { 355 | "S3Bucket": { 356 | "Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}" 357 | }, 358 | "S3Key": "7566f03a4e5c13b6df8450112ed7d71ab166d8d211e5b501e15b758165d735b5.zip" 359 | }, 360 | "Timeout": 120, 361 | "Handler": "index.onTimeout", 362 | "Role": { 363 | "Fn::GetAtt": [ 364 | "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41aRoleB84BD8CE", 365 | "Arn" 366 | ] 367 | } 368 | } 369 | } 370 | }, 371 | "Outputs": { 372 | "AssertionResultsAwsApiCallStepFunctionsdescribeExecution": { 373 | "Value": { 374 | "Fn::GetAtt": [ 375 | "AwsApiCallStepFunctionsdescribeExecution", 376 | "assertion" 377 | ] 378 | } 379 | } 380 | }, 381 | "Parameters": { 382 | "BootstrapVersion": { 383 | "Type": "AWS::SSM::Parameter::Value", 384 | "Default": "/cdk-bootstrap/hnb659fds/version", 385 | "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" 386 | } 387 | }, 388 | "Rules": { 389 | "CheckBootstrapVersion": { 390 | "Assertions": [ 391 | { 392 | "Assert": { 393 | "Fn::Not": [ 394 | { 395 | "Fn::Contains": [ 396 | [ 397 | "1", 398 | "2", 399 | "3", 400 | "4", 401 | "5" 402 | ], 403 | { 404 | "Ref": "BootstrapVersion" 405 | } 406 | ] 407 | } 408 | ] 409 | }, 410 | "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." 411 | } 412 | ] 413 | } 414 | } 415 | } -------------------------------------------------------------------------------- /test/default.integ.snapshot/StackUnderTest.assets.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "21.0.0", 3 | "files": { 4 | "b2d1fe199747ada5f3430102be9d9ebd4dac8010532ed3ae6e4af66ef8a904fb": { 5 | "source": { 6 | "path": "StackUnderTest.template.json", 7 | "packaging": "file" 8 | }, 9 | "destinations": { 10 | "current_account-current_region": { 11 | "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", 12 | "objectKey": "b2d1fe199747ada5f3430102be9d9ebd4dac8010532ed3ae6e4af66ef8a904fb.json", 13 | "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" 14 | } 15 | } 16 | } 17 | }, 18 | "dockerImages": {} 19 | } -------------------------------------------------------------------------------- /test/default.integ.snapshot/StackUnderTest.template.json: -------------------------------------------------------------------------------- 1 | { 2 | "Resources": { 3 | "LogsComptrollerRunnerRoleEB6A3DA6": { 4 | "Type": "AWS::IAM::Role", 5 | "Properties": { 6 | "AssumeRolePolicyDocument": { 7 | "Statement": [ 8 | { 9 | "Action": "sts:AssumeRole", 10 | "Effect": "Allow", 11 | "Principal": { 12 | "Service": { 13 | "Fn::FindInMap": [ 14 | "ServiceprincipalMap", 15 | { 16 | "Ref": "AWS::Region" 17 | }, 18 | "states" 19 | ] 20 | } 21 | } 22 | } 23 | ], 24 | "Version": "2012-10-17" 25 | } 26 | } 27 | }, 28 | "LogsComptrollerRunnerRoleDefaultPolicyE31C0944": { 29 | "Type": "AWS::IAM::Policy", 30 | "Properties": { 31 | "PolicyDocument": { 32 | "Statement": [ 33 | { 34 | "Action": [ 35 | "lambda:getFunction", 36 | "logs:DeleteLogGroup", 37 | "logs:PutRetentionPolicy", 38 | "states:SendTaskSuccess", 39 | "xray:GetSamplingRules", 40 | "xray:GetSamplingTargets", 41 | "xray:PutTelemetryRecords", 42 | "xray:PutTraceSegments" 43 | ], 44 | "Effect": "Allow", 45 | "Resource": "*" 46 | } 47 | ], 48 | "Version": "2012-10-17" 49 | }, 50 | "PolicyName": "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 51 | "Roles": [ 52 | { 53 | "Ref": "LogsComptrollerRunnerRoleEB6A3DA6" 54 | } 55 | ] 56 | } 57 | }, 58 | "LogsComptrollerRunner2AB55B01": { 59 | "Type": "AWS::StepFunctions::StateMachine", 60 | "Properties": { 61 | "RoleArn": { 62 | "Fn::GetAtt": [ 63 | "LogsComptrollerRunnerRoleEB6A3DA6", 64 | "Arn" 65 | ] 66 | }, 67 | "DefinitionString": { 68 | "Fn::Join": [ 69 | "", 70 | [ 71 | "{\"StartAt\":\"Map\",\"States\":{\"Map\":{\"Type\":\"Map\",\"ResultPath\":\"$.MapResult\",\"Next\":\"InitStatsLoop\",\"InputPath\":\"$.LogGroups\",\"Iterator\":{\"StartAt\":\"GetLGParts\",\"States\":{\"GetLGParts\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Function\",\"Parameters\":{\"LGParts.$\":\"States.StringSplit($.LogGroupName, '/')\"},\"Next\":\"GetArrayLen\"},\"GetArrayLen\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Array\",\"Parameters\":{\"Len.$\":\"States.ArrayLength($.Function.LGParts)\"},\"Next\":\"TwoOrMore?\"},\"TwoOrMore?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.Array.Len\",\"NumericGreaterThanEquals\":2,\"Next\":\"GetLogType\"}],\"Default\":\"HasRetention?\"},\"GetLogType\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Log\",\"Parameters\":{\"LogType.$\":\"States.ArrayGetItem($.Function.LGParts, 1)\"},\"Next\":\"IsLambdaLog?\"},\"IsLambdaLog?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.Log.LogType\",\"StringEquals\":\"lambda\",\"Next\":\"GetFnName\"}],\"Default\":\"HasRetention?\"},\"GetFnName\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Function\",\"Parameters\":{\"FunctionName.$\":\"States.ArrayGetItem($.Function.LGParts, 2)\"},\"Next\":\"FunctionPresent?\"},\"FunctionPresent?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.Function.FunctionName\",\"IsNull\":false,\"Next\":\"GetFunction\"}],\"Default\":\"HasRetention?\"},\"GetFunction\":{\"Next\":\"HasRetention?\",\"Catch\":[{\"ErrorEquals\":[\"States.TaskFailed\"],\"ResultPath\":null,\"Next\":\"DeleteLG\"}],\"Type\":\"Task\",\"ResultPath\":null,\"Resource\":\"arn:", 72 | { 73 | "Ref": "AWS::Partition" 74 | }, 75 | ":states:::aws-sdk:lambda:getFunction\",\"Parameters\":{\"FunctionName.$\":\"$.Function.FunctionName\"}},\"HasRetention?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.RetentionInDays\",\"IsPresent\":false,\"Next\":\"AddRetention\"}],\"Default\":\"lgtm\"},\"lgtm\":{\"Type\":\"Pass\",\"Result\":{\"IsDeleted\":0,\"IsRetained\":0},\"End\":true},\"AddRetention\":{\"End\":true,\"Retry\":[{\"ErrorEquals\":[\"States.ALL\"],\"MaxAttempts\":10}],\"Type\":\"Task\",\"ResultSelector\":{\"IsDeleted\":0,\"IsRetained\":1},\"Resource\":\"arn:", 76 | { 77 | "Ref": "AWS::Partition" 78 | }, 79 | ":states:::aws-sdk:cloudwatchlogs:putRetentionPolicy\",\"Parameters\":{\"LogGroupName.$\":\"$.LogGroupName\",\"RetentionInDays\":7}},\"DeleteLG\":{\"End\":true,\"Retry\":[{\"ErrorEquals\":[\"States.ALL\"],\"MaxAttempts\":10}],\"Type\":\"Task\",\"ResultSelector\":{\"IsDeleted\":1,\"IsRetained\":0},\"Resource\":\"arn:", 80 | { 81 | "Ref": "AWS::Partition" 82 | }, 83 | ":states:::aws-sdk:cloudwatchlogs:deleteLogGroup\",\"Parameters\":{\"LogGroupName.$\":\"$.LogGroupName\"}}}},\"MaxConcurrency\":10},\"InitStatsLoop\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Iterator\",\"Parameters\":{\"Index\":0,\"ResultLen.$\":\"States.ArrayLength($.MapResult)\"},\"Next\":\"HasNextMapResult?\"},\"HasNextMapResult?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.Iterator.Index\",\"NumericLessThanPath\":\"$.Iterator.ResultLen\",\"Next\":\"GetNextResult\"}],\"Default\":\"SendSuccess\"},\"IncrementCounter\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Iterator\",\"Parameters\":{\"Index.$\":\"States.MathAdd($.Iterator.Index, 1)\",\"ResultLen.$\":\"$.Iterator.ResultLen\"},\"Next\":\"HasNextMapResult?\"},\"IncrementStats\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Stats\",\"Parameters\":{\"LGsDeleted.$\":\"States.MathAdd($.Stats.LGsDeleted, $.R.Result.IsDeleted)\",\"LGsRetained.$\":\"States.MathAdd($.Stats.LGsRetained, $.R.Result.IsRetained)\",\"LGsSeen.$\":\"$.Stats.LGsSeen\"},\"Next\":\"IncrementCounter\"},\"GetNextResult\":{\"Type\":\"Pass\",\"ResultPath\":\"$.R\",\"Parameters\":{\"Result.$\":\"States.ArrayGetItem($.MapResult, $.Iterator.Index)\"},\"Next\":\"IncrementStats\"},\"SendSuccess\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:", 84 | { 85 | "Ref": "AWS::Partition" 86 | }, 87 | ":states:::aws-sdk:sfn:sendTaskSuccess\",\"Parameters\":{\"Output.$\":\"$.Stats\",\"TaskToken.$\":\"$.Token\"}}}}" 88 | ] 89 | ] 90 | }, 91 | "StateMachineName": "logs-comptroller-runner", 92 | "TracingConfiguration": { 93 | "Enabled": true 94 | } 95 | }, 96 | "DependsOn": [ 97 | "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 98 | "LogsComptrollerRunnerRoleEB6A3DA6" 99 | ] 100 | }, 101 | "LogsComptrollerIteratorRole3F6E7D1B": { 102 | "Type": "AWS::IAM::Role", 103 | "Properties": { 104 | "AssumeRolePolicyDocument": { 105 | "Statement": [ 106 | { 107 | "Action": "sts:AssumeRole", 108 | "Effect": "Allow", 109 | "Principal": { 110 | "Service": { 111 | "Fn::FindInMap": [ 112 | "ServiceprincipalMap", 113 | { 114 | "Ref": "AWS::Region" 115 | }, 116 | "states" 117 | ] 118 | } 119 | } 120 | } 121 | ], 122 | "Version": "2012-10-17" 123 | } 124 | } 125 | }, 126 | "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE": { 127 | "Type": "AWS::IAM::Policy", 128 | "Properties": { 129 | "PolicyDocument": { 130 | "Statement": [ 131 | { 132 | "Action": [ 133 | "logs:DescribeLogGroups", 134 | "xray:GetSamplingRules", 135 | "xray:GetSamplingTargets", 136 | "xray:PutTelemetryRecords", 137 | "xray:PutTraceSegments" 138 | ], 139 | "Effect": "Allow", 140 | "Resource": "*" 141 | }, 142 | { 143 | "Action": "states:StartExecution", 144 | "Effect": "Allow", 145 | "Resource": { 146 | "Ref": "LogsComptrollerRunner2AB55B01" 147 | } 148 | } 149 | ], 150 | "Version": "2012-10-17" 151 | }, 152 | "PolicyName": "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 153 | "Roles": [ 154 | { 155 | "Ref": "LogsComptrollerIteratorRole3F6E7D1B" 156 | } 157 | ] 158 | } 159 | }, 160 | "LogsComptrollerIteratorEA634D49": { 161 | "Type": "AWS::StepFunctions::StateMachine", 162 | "Properties": { 163 | "RoleArn": { 164 | "Fn::GetAtt": [ 165 | "LogsComptrollerIteratorRole3F6E7D1B", 166 | "Arn" 167 | ] 168 | }, 169 | "DefinitionString": { 170 | "Fn::Join": [ 171 | "", 172 | [ 173 | "{\"StartAt\":\"GetLogGroups\",\"States\":{\"GetLogGroups\":{\"Next\":\"SetLGsSeen\",\"Type\":\"Task\",\"ResultPath\":\"$.LG\",\"Resource\":\"arn:", 174 | { 175 | "Ref": "AWS::Partition" 176 | }, 177 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\",\"Parameters\":{}},\"SetLGsSeen\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Stats\",\"Parameters\":{\"LGsDeleted\":0,\"LGsRetained\":0,\"LGsSeen.$\":\"States.ArrayLength($.LG.LogGroups)\"},\"Next\":\"ExecuteRunner\"},\"ExecuteRunner\":{\"Next\":\"HasNextToken?\",\"Type\":\"Task\",\"ResultPath\":\"$.Stats\",\"Resource\":\"arn:", 178 | { 179 | "Ref": "AWS::Partition" 180 | }, 181 | ":states:::states:startExecution.waitForTaskToken\",\"Parameters\":{\"Input\":{\"LogGroups.$\":\"$.LG.LogGroups\",\"Stats.$\":\"$.Stats\",\"Token.$\":\"$$.Task.Token\"},\"StateMachineArn\":\"", 182 | { 183 | "Ref": "LogsComptrollerRunner2AB55B01" 184 | }, 185 | "\"}},\"AppendTotal\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Stats\",\"Parameters\":{\"LGsDeleted.$\":\"$.Stats.LGsDeleted\",\"LGsRetained.$\":\"$.Stats.LGsRetained\",\"LGsSeen.$\":\"States.MathAdd($.Stats.LGsSeen, States.ArrayLength($.LG.LogGroups))\"},\"Next\":\"ExecuteRunner\"},\"GetNextLogGroups\":{\"Next\":\"AppendTotal\",\"Type\":\"Task\",\"ResultPath\":\"$.LG\",\"Resource\":\"arn:", 186 | { 187 | "Ref": "AWS::Partition" 188 | }, 189 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\",\"Parameters\":{\"NextToken.$\":\"$.LG.NextToken\"}},\"HasNextToken?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.LG.NextToken\",\"IsPresent\":true,\"Next\":\"GetNextLogGroups\"}],\"Default\":\"Work Complete!\"},\"Work Complete!\":{\"Type\":\"Succeed\",\"OutputPath\":\"$.Stats\"}}}" 190 | ] 191 | ] 192 | }, 193 | "StateMachineName": "logs-comptroller-iterator", 194 | "TracingConfiguration": { 195 | "Enabled": true 196 | } 197 | }, 198 | "DependsOn": [ 199 | "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 200 | "LogsComptrollerIteratorRole3F6E7D1B" 201 | ] 202 | } 203 | }, 204 | "Mappings": { 205 | "ServiceprincipalMap": { 206 | "af-south-1": { 207 | "states": "states.af-south-1.amazonaws.com" 208 | }, 209 | "ap-east-1": { 210 | "states": "states.ap-east-1.amazonaws.com" 211 | }, 212 | "ap-northeast-1": { 213 | "states": "states.ap-northeast-1.amazonaws.com" 214 | }, 215 | "ap-northeast-2": { 216 | "states": "states.ap-northeast-2.amazonaws.com" 217 | }, 218 | "ap-northeast-3": { 219 | "states": "states.ap-northeast-3.amazonaws.com" 220 | }, 221 | "ap-south-1": { 222 | "states": "states.ap-south-1.amazonaws.com" 223 | }, 224 | "ap-southeast-1": { 225 | "states": "states.ap-southeast-1.amazonaws.com" 226 | }, 227 | "ap-southeast-2": { 228 | "states": "states.ap-southeast-2.amazonaws.com" 229 | }, 230 | "ap-southeast-3": { 231 | "states": "states.ap-southeast-3.amazonaws.com" 232 | }, 233 | "ca-central-1": { 234 | "states": "states.ca-central-1.amazonaws.com" 235 | }, 236 | "cn-north-1": { 237 | "states": "states.cn-north-1.amazonaws.com" 238 | }, 239 | "cn-northwest-1": { 240 | "states": "states.cn-northwest-1.amazonaws.com" 241 | }, 242 | "eu-central-1": { 243 | "states": "states.eu-central-1.amazonaws.com" 244 | }, 245 | "eu-north-1": { 246 | "states": "states.eu-north-1.amazonaws.com" 247 | }, 248 | "eu-south-1": { 249 | "states": "states.eu-south-1.amazonaws.com" 250 | }, 251 | "eu-south-2": { 252 | "states": "states.eu-south-2.amazonaws.com" 253 | }, 254 | "eu-west-1": { 255 | "states": "states.eu-west-1.amazonaws.com" 256 | }, 257 | "eu-west-2": { 258 | "states": "states.eu-west-2.amazonaws.com" 259 | }, 260 | "eu-west-3": { 261 | "states": "states.eu-west-3.amazonaws.com" 262 | }, 263 | "me-south-1": { 264 | "states": "states.me-south-1.amazonaws.com" 265 | }, 266 | "sa-east-1": { 267 | "states": "states.sa-east-1.amazonaws.com" 268 | }, 269 | "us-east-1": { 270 | "states": "states.us-east-1.amazonaws.com" 271 | }, 272 | "us-east-2": { 273 | "states": "states.us-east-2.amazonaws.com" 274 | }, 275 | "us-gov-east-1": { 276 | "states": "states.us-gov-east-1.amazonaws.com" 277 | }, 278 | "us-gov-west-1": { 279 | "states": "states.us-gov-west-1.amazonaws.com" 280 | }, 281 | "us-iso-east-1": { 282 | "states": "states.amazonaws.com" 283 | }, 284 | "us-iso-west-1": { 285 | "states": "states.amazonaws.com" 286 | }, 287 | "us-isob-east-1": { 288 | "states": "states.amazonaws.com" 289 | }, 290 | "us-west-1": { 291 | "states": "states.us-west-1.amazonaws.com" 292 | }, 293 | "us-west-2": { 294 | "states": "states.us-west-2.amazonaws.com" 295 | } 296 | } 297 | }, 298 | "Parameters": { 299 | "BootstrapVersion": { 300 | "Type": "AWS::SSM::Parameter::Value", 301 | "Default": "/cdk-bootstrap/hnb659fds/version", 302 | "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" 303 | } 304 | }, 305 | "Rules": { 306 | "CheckBootstrapVersion": { 307 | "Assertions": [ 308 | { 309 | "Assert": { 310 | "Fn::Not": [ 311 | { 312 | "Fn::Contains": [ 313 | [ 314 | "1", 315 | "2", 316 | "3", 317 | "4", 318 | "5" 319 | ], 320 | { 321 | "Ref": "BootstrapVersion" 322 | } 323 | ] 324 | } 325 | ] 326 | }, 327 | "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." 328 | } 329 | ] 330 | } 331 | } 332 | } -------------------------------------------------------------------------------- /test/default.integ.snapshot/integ.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "21.0.0", 3 | "testCases": { 4 | "Integ/DefaultTest": { 5 | "stacks": [ 6 | "StackUnderTest" 7 | ], 8 | "diffAssets": true, 9 | "cdkCommandOptions": { 10 | "deploy": { 11 | "args": { 12 | "json": true, 13 | "requireApproval": "never" 14 | } 15 | }, 16 | "destroy": { 17 | "args": { 18 | "force": true 19 | } 20 | } 21 | }, 22 | "stackUpdateWorkflow": true, 23 | "assertionStack": "Integ/DefaultTest/DeployAssert", 24 | "assertionStackName": "IntegDefaultTestDeployAssert4E6713E1" 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /test/default.integ.snapshot/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "21.0.0", 3 | "artifacts": { 4 | "Tree": { 5 | "type": "cdk:tree", 6 | "properties": { 7 | "file": "tree.json" 8 | } 9 | }, 10 | "StackUnderTest.assets": { 11 | "type": "cdk:asset-manifest", 12 | "properties": { 13 | "file": "StackUnderTest.assets.json", 14 | "requiresBootstrapStackVersion": 6, 15 | "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" 16 | } 17 | }, 18 | "StackUnderTest": { 19 | "type": "aws:cloudformation:stack", 20 | "environment": "aws://unknown-account/unknown-region", 21 | "properties": { 22 | "templateFile": "StackUnderTest.template.json", 23 | "validateOnSynth": false, 24 | "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", 25 | "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", 26 | "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/b2d1fe199747ada5f3430102be9d9ebd4dac8010532ed3ae6e4af66ef8a904fb.json", 27 | "requiresBootstrapStackVersion": 6, 28 | "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", 29 | "additionalDependencies": [ 30 | "StackUnderTest.assets" 31 | ], 32 | "lookupRole": { 33 | "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", 34 | "requiresBootstrapStackVersion": 8, 35 | "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" 36 | } 37 | }, 38 | "dependencies": [ 39 | "StackUnderTest.assets" 40 | ], 41 | "metadata": { 42 | "/StackUnderTest/LogsComptrollerRunner/Role/Resource": [ 43 | { 44 | "type": "aws:cdk:logicalId", 45 | "data": "LogsComptrollerRunnerRoleEB6A3DA6" 46 | } 47 | ], 48 | "/StackUnderTest/LogsComptrollerRunner/Role/DefaultPolicy/Resource": [ 49 | { 50 | "type": "aws:cdk:logicalId", 51 | "data": "LogsComptrollerRunnerRoleDefaultPolicyE31C0944" 52 | } 53 | ], 54 | "/StackUnderTest/LogsComptrollerRunner/Resource": [ 55 | { 56 | "type": "aws:cdk:logicalId", 57 | "data": "LogsComptrollerRunner2AB55B01" 58 | } 59 | ], 60 | "/StackUnderTest/LogsComptrollerIterator/Role/Resource": [ 61 | { 62 | "type": "aws:cdk:logicalId", 63 | "data": "LogsComptrollerIteratorRole3F6E7D1B" 64 | } 65 | ], 66 | "/StackUnderTest/LogsComptrollerIterator/Role/DefaultPolicy/Resource": [ 67 | { 68 | "type": "aws:cdk:logicalId", 69 | "data": "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE" 70 | } 71 | ], 72 | "/StackUnderTest/LogsComptrollerIterator/Resource": [ 73 | { 74 | "type": "aws:cdk:logicalId", 75 | "data": "LogsComptrollerIteratorEA634D49" 76 | } 77 | ], 78 | "/StackUnderTest/Service-principalMap": [ 79 | { 80 | "type": "aws:cdk:logicalId", 81 | "data": "ServiceprincipalMap" 82 | } 83 | ], 84 | "/StackUnderTest/BootstrapVersion": [ 85 | { 86 | "type": "aws:cdk:logicalId", 87 | "data": "BootstrapVersion" 88 | } 89 | ], 90 | "/StackUnderTest/CheckBootstrapVersion": [ 91 | { 92 | "type": "aws:cdk:logicalId", 93 | "data": "CheckBootstrapVersion" 94 | } 95 | ] 96 | }, 97 | "displayName": "StackUnderTest" 98 | }, 99 | "IntegDefaultTestDeployAssert4E6713E1.assets": { 100 | "type": "cdk:asset-manifest", 101 | "properties": { 102 | "file": "IntegDefaultTestDeployAssert4E6713E1.assets.json", 103 | "requiresBootstrapStackVersion": 6, 104 | "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" 105 | } 106 | }, 107 | "IntegDefaultTestDeployAssert4E6713E1": { 108 | "type": "aws:cloudformation:stack", 109 | "environment": "aws://unknown-account/unknown-region", 110 | "properties": { 111 | "templateFile": "IntegDefaultTestDeployAssert4E6713E1.template.json", 112 | "validateOnSynth": false, 113 | "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", 114 | "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", 115 | "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/03b035eb49023734b1ff203dece1592d4038d444bca9a84d7896e653c354347e.json", 116 | "requiresBootstrapStackVersion": 6, 117 | "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", 118 | "additionalDependencies": [ 119 | "IntegDefaultTestDeployAssert4E6713E1.assets" 120 | ], 121 | "lookupRole": { 122 | "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", 123 | "requiresBootstrapStackVersion": 8, 124 | "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" 125 | } 126 | }, 127 | "dependencies": [ 128 | "IntegDefaultTestDeployAssert4E6713E1.assets" 129 | ], 130 | "metadata": { 131 | "/Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsstartExecution/Default/Default": [ 132 | { 133 | "type": "aws:cdk:logicalId", 134 | "data": "AwsApiCallStepFunctionsstartExecution" 135 | } 136 | ], 137 | "/Integ/DefaultTest/DeployAssert/SingletonFunction1488541a7b23466481b69b4408076b81/Role": [ 138 | { 139 | "type": "aws:cdk:logicalId", 140 | "data": "SingletonFunction1488541a7b23466481b69b4408076b81Role37ABCE73" 141 | } 142 | ], 143 | "/Integ/DefaultTest/DeployAssert/SingletonFunction1488541a7b23466481b69b4408076b81/Handler": [ 144 | { 145 | "type": "aws:cdk:logicalId", 146 | "data": "SingletonFunction1488541a7b23466481b69b4408076b81HandlerCD40AE9F" 147 | } 148 | ], 149 | "/Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/Default/Default": [ 150 | { 151 | "type": "aws:cdk:logicalId", 152 | "data": "AwsApiCallStepFunctionsdescribeExecution" 153 | } 154 | ], 155 | "/Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/IsCompleteProvider/Invoke": [ 156 | { 157 | "type": "aws:cdk:logicalId", 158 | "data": "AwsApiCallStepFunctionsdescribeExecutionWaitForIsCompleteProviderInvoke90973546" 159 | } 160 | ], 161 | "/Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/TimeoutProvider/Invoke": [ 162 | { 163 | "type": "aws:cdk:logicalId", 164 | "data": "AwsApiCallStepFunctionsdescribeExecutionWaitForTimeoutProviderInvoke59993CEE" 165 | } 166 | ], 167 | "/Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/Role": [ 168 | { 169 | "type": "aws:cdk:logicalId", 170 | "data": "AwsApiCallStepFunctionsdescribeExecutionWaitForRole0A7F8888" 171 | } 172 | ], 173 | "/Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/Resource": [ 174 | { 175 | "type": "aws:cdk:logicalId", 176 | "data": "AwsApiCallStepFunctionsdescribeExecutionWaitFor3BA9FD23" 177 | } 178 | ], 179 | "/Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/AssertionResults": [ 180 | { 181 | "type": "aws:cdk:logicalId", 182 | "data": "AssertionResultsAwsApiCallStepFunctionsdescribeExecution" 183 | } 184 | ], 185 | "/Integ/DefaultTest/DeployAssert/SingletonFunction76b3e830a873425f8453eddd85c86925/Role": [ 186 | { 187 | "type": "aws:cdk:logicalId", 188 | "data": "SingletonFunction76b3e830a873425f8453eddd85c86925Role918961BB" 189 | } 190 | ], 191 | "/Integ/DefaultTest/DeployAssert/SingletonFunction76b3e830a873425f8453eddd85c86925/Handler": [ 192 | { 193 | "type": "aws:cdk:logicalId", 194 | "data": "SingletonFunction76b3e830a873425f8453eddd85c86925Handler81461ECE" 195 | } 196 | ], 197 | "/Integ/DefaultTest/DeployAssert/SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41a/Role": [ 198 | { 199 | "type": "aws:cdk:logicalId", 200 | "data": "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41aRoleB84BD8CE" 201 | } 202 | ], 203 | "/Integ/DefaultTest/DeployAssert/SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41a/Handler": [ 204 | { 205 | "type": "aws:cdk:logicalId", 206 | "data": "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41aHandlerADF3E6EA" 207 | } 208 | ], 209 | "/Integ/DefaultTest/DeployAssert/BootstrapVersion": [ 210 | { 211 | "type": "aws:cdk:logicalId", 212 | "data": "BootstrapVersion" 213 | } 214 | ], 215 | "/Integ/DefaultTest/DeployAssert/CheckBootstrapVersion": [ 216 | { 217 | "type": "aws:cdk:logicalId", 218 | "data": "CheckBootstrapVersion" 219 | } 220 | ] 221 | }, 222 | "displayName": "Integ/DefaultTest/DeployAssert" 223 | } 224 | } 225 | } -------------------------------------------------------------------------------- /test/default.integ.snapshot/tree.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "tree-0.1", 3 | "tree": { 4 | "id": "App", 5 | "path": "", 6 | "children": { 7 | "Tree": { 8 | "id": "Tree", 9 | "path": "Tree", 10 | "constructInfo": { 11 | "fqn": "constructs.Construct", 12 | "version": "10.1.131" 13 | } 14 | }, 15 | "StackUnderTest": { 16 | "id": "StackUnderTest", 17 | "path": "StackUnderTest", 18 | "children": { 19 | "LogsComptroller": { 20 | "id": "LogsComptroller", 21 | "path": "StackUnderTest/LogsComptroller", 22 | "constructInfo": { 23 | "fqn": "constructs.Construct", 24 | "version": "10.1.131" 25 | } 26 | }, 27 | "Map": { 28 | "id": "Map", 29 | "path": "StackUnderTest/Map", 30 | "constructInfo": { 31 | "fqn": "aws-cdk-lib.aws_stepfunctions.Map", 32 | "version": "2.46.0" 33 | } 34 | }, 35 | "GetLGParts": { 36 | "id": "GetLGParts", 37 | "path": "StackUnderTest/GetLGParts", 38 | "constructInfo": { 39 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 40 | "version": "2.46.0" 41 | } 42 | }, 43 | "GetArrayLen": { 44 | "id": "GetArrayLen", 45 | "path": "StackUnderTest/GetArrayLen", 46 | "constructInfo": { 47 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 48 | "version": "2.46.0" 49 | } 50 | }, 51 | "TwoOrMore?": { 52 | "id": "TwoOrMore?", 53 | "path": "StackUnderTest/TwoOrMore?", 54 | "constructInfo": { 55 | "fqn": "aws-cdk-lib.aws_stepfunctions.Choice", 56 | "version": "2.46.0" 57 | } 58 | }, 59 | "GetLogType": { 60 | "id": "GetLogType", 61 | "path": "StackUnderTest/GetLogType", 62 | "constructInfo": { 63 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 64 | "version": "2.46.0" 65 | } 66 | }, 67 | "IsLambdaLog?": { 68 | "id": "IsLambdaLog?", 69 | "path": "StackUnderTest/IsLambdaLog?", 70 | "constructInfo": { 71 | "fqn": "aws-cdk-lib.aws_stepfunctions.Choice", 72 | "version": "2.46.0" 73 | } 74 | }, 75 | "GetFnName": { 76 | "id": "GetFnName", 77 | "path": "StackUnderTest/GetFnName", 78 | "constructInfo": { 79 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 80 | "version": "2.46.0" 81 | } 82 | }, 83 | "FunctionPresent?": { 84 | "id": "FunctionPresent?", 85 | "path": "StackUnderTest/FunctionPresent?", 86 | "constructInfo": { 87 | "fqn": "aws-cdk-lib.aws_stepfunctions.Choice", 88 | "version": "2.46.0" 89 | } 90 | }, 91 | "GetFunction": { 92 | "id": "GetFunction", 93 | "path": "StackUnderTest/GetFunction", 94 | "constructInfo": { 95 | "fqn": "aws-cdk-lib.aws_stepfunctions_tasks.CallAwsService", 96 | "version": "2.46.0" 97 | } 98 | }, 99 | "DeleteLG": { 100 | "id": "DeleteLG", 101 | "path": "StackUnderTest/DeleteLG", 102 | "constructInfo": { 103 | "fqn": "aws-cdk-lib.aws_stepfunctions_tasks.CallAwsService", 104 | "version": "2.46.0" 105 | } 106 | }, 107 | "AddRetention": { 108 | "id": "AddRetention", 109 | "path": "StackUnderTest/AddRetention", 110 | "constructInfo": { 111 | "fqn": "aws-cdk-lib.aws_stepfunctions_tasks.CallAwsService", 112 | "version": "2.46.0" 113 | } 114 | }, 115 | "HasRetention?": { 116 | "id": "HasRetention?", 117 | "path": "StackUnderTest/HasRetention?", 118 | "constructInfo": { 119 | "fqn": "aws-cdk-lib.aws_stepfunctions.Choice", 120 | "version": "2.46.0" 121 | } 122 | }, 123 | "lgtm": { 124 | "id": "lgtm", 125 | "path": "StackUnderTest/lgtm", 126 | "constructInfo": { 127 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 128 | "version": "2.46.0" 129 | } 130 | }, 131 | "InitStatsLoop": { 132 | "id": "InitStatsLoop", 133 | "path": "StackUnderTest/InitStatsLoop", 134 | "constructInfo": { 135 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 136 | "version": "2.46.0" 137 | } 138 | }, 139 | "GetNextResult": { 140 | "id": "GetNextResult", 141 | "path": "StackUnderTest/GetNextResult", 142 | "constructInfo": { 143 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 144 | "version": "2.46.0" 145 | } 146 | }, 147 | "IncrementStats": { 148 | "id": "IncrementStats", 149 | "path": "StackUnderTest/IncrementStats", 150 | "constructInfo": { 151 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 152 | "version": "2.46.0" 153 | } 154 | }, 155 | "IncrementCounter": { 156 | "id": "IncrementCounter", 157 | "path": "StackUnderTest/IncrementCounter", 158 | "constructInfo": { 159 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 160 | "version": "2.46.0" 161 | } 162 | }, 163 | "SendSuccess": { 164 | "id": "SendSuccess", 165 | "path": "StackUnderTest/SendSuccess", 166 | "constructInfo": { 167 | "fqn": "aws-cdk-lib.aws_stepfunctions_tasks.CallAwsService", 168 | "version": "2.46.0" 169 | } 170 | }, 171 | "HasNextMapResult?": { 172 | "id": "HasNextMapResult?", 173 | "path": "StackUnderTest/HasNextMapResult?", 174 | "constructInfo": { 175 | "fqn": "aws-cdk-lib.aws_stepfunctions.Choice", 176 | "version": "2.46.0" 177 | } 178 | }, 179 | "LogsComptrollerRunner": { 180 | "id": "LogsComptrollerRunner", 181 | "path": "StackUnderTest/LogsComptrollerRunner", 182 | "children": { 183 | "Role": { 184 | "id": "Role", 185 | "path": "StackUnderTest/LogsComptrollerRunner/Role", 186 | "children": { 187 | "Resource": { 188 | "id": "Resource", 189 | "path": "StackUnderTest/LogsComptrollerRunner/Role/Resource", 190 | "attributes": { 191 | "aws:cdk:cloudformation:type": "AWS::IAM::Role", 192 | "aws:cdk:cloudformation:props": { 193 | "assumeRolePolicyDocument": { 194 | "Statement": [ 195 | { 196 | "Action": "sts:AssumeRole", 197 | "Effect": "Allow", 198 | "Principal": { 199 | "Service": { 200 | "Fn::FindInMap": [ 201 | "ServiceprincipalMap", 202 | { 203 | "Ref": "AWS::Region" 204 | }, 205 | "states" 206 | ] 207 | } 208 | } 209 | } 210 | ], 211 | "Version": "2012-10-17" 212 | } 213 | } 214 | }, 215 | "constructInfo": { 216 | "fqn": "aws-cdk-lib.aws_iam.CfnRole", 217 | "version": "2.46.0" 218 | } 219 | }, 220 | "DefaultPolicy": { 221 | "id": "DefaultPolicy", 222 | "path": "StackUnderTest/LogsComptrollerRunner/Role/DefaultPolicy", 223 | "children": { 224 | "Resource": { 225 | "id": "Resource", 226 | "path": "StackUnderTest/LogsComptrollerRunner/Role/DefaultPolicy/Resource", 227 | "attributes": { 228 | "aws:cdk:cloudformation:type": "AWS::IAM::Policy", 229 | "aws:cdk:cloudformation:props": { 230 | "policyDocument": { 231 | "Statement": [ 232 | { 233 | "Action": [ 234 | "lambda:getFunction", 235 | "logs:DeleteLogGroup", 236 | "logs:PutRetentionPolicy", 237 | "states:SendTaskSuccess", 238 | "xray:GetSamplingRules", 239 | "xray:GetSamplingTargets", 240 | "xray:PutTelemetryRecords", 241 | "xray:PutTraceSegments" 242 | ], 243 | "Effect": "Allow", 244 | "Resource": "*" 245 | } 246 | ], 247 | "Version": "2012-10-17" 248 | }, 249 | "policyName": "LogsComptrollerRunnerRoleDefaultPolicyE31C0944", 250 | "roles": [ 251 | { 252 | "Ref": "LogsComptrollerRunnerRoleEB6A3DA6" 253 | } 254 | ] 255 | } 256 | }, 257 | "constructInfo": { 258 | "fqn": "aws-cdk-lib.aws_iam.CfnPolicy", 259 | "version": "2.46.0" 260 | } 261 | } 262 | }, 263 | "constructInfo": { 264 | "fqn": "aws-cdk-lib.aws_iam.Policy", 265 | "version": "2.46.0" 266 | } 267 | } 268 | }, 269 | "constructInfo": { 270 | "fqn": "aws-cdk-lib.aws_iam.Role", 271 | "version": "2.46.0" 272 | } 273 | }, 274 | "Resource": { 275 | "id": "Resource", 276 | "path": "StackUnderTest/LogsComptrollerRunner/Resource", 277 | "attributes": { 278 | "aws:cdk:cloudformation:type": "AWS::StepFunctions::StateMachine", 279 | "aws:cdk:cloudformation:props": { 280 | "roleArn": { 281 | "Fn::GetAtt": [ 282 | "LogsComptrollerRunnerRoleEB6A3DA6", 283 | "Arn" 284 | ] 285 | }, 286 | "definitionString": { 287 | "Fn::Join": [ 288 | "", 289 | [ 290 | "{\"StartAt\":\"Map\",\"States\":{\"Map\":{\"Type\":\"Map\",\"ResultPath\":\"$.MapResult\",\"Next\":\"InitStatsLoop\",\"InputPath\":\"$.LogGroups\",\"Iterator\":{\"StartAt\":\"GetLGParts\",\"States\":{\"GetLGParts\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Function\",\"Parameters\":{\"LGParts.$\":\"States.StringSplit($.LogGroupName, '/')\"},\"Next\":\"GetArrayLen\"},\"GetArrayLen\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Array\",\"Parameters\":{\"Len.$\":\"States.ArrayLength($.Function.LGParts)\"},\"Next\":\"TwoOrMore?\"},\"TwoOrMore?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.Array.Len\",\"NumericGreaterThanEquals\":2,\"Next\":\"GetLogType\"}],\"Default\":\"HasRetention?\"},\"GetLogType\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Log\",\"Parameters\":{\"LogType.$\":\"States.ArrayGetItem($.Function.LGParts, 1)\"},\"Next\":\"IsLambdaLog?\"},\"IsLambdaLog?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.Log.LogType\",\"StringEquals\":\"lambda\",\"Next\":\"GetFnName\"}],\"Default\":\"HasRetention?\"},\"GetFnName\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Function\",\"Parameters\":{\"FunctionName.$\":\"States.ArrayGetItem($.Function.LGParts, 2)\"},\"Next\":\"FunctionPresent?\"},\"FunctionPresent?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.Function.FunctionName\",\"IsNull\":false,\"Next\":\"GetFunction\"}],\"Default\":\"HasRetention?\"},\"GetFunction\":{\"Next\":\"HasRetention?\",\"Catch\":[{\"ErrorEquals\":[\"States.TaskFailed\"],\"ResultPath\":null,\"Next\":\"DeleteLG\"}],\"Type\":\"Task\",\"ResultPath\":null,\"Resource\":\"arn:", 291 | { 292 | "Ref": "AWS::Partition" 293 | }, 294 | ":states:::aws-sdk:lambda:getFunction\",\"Parameters\":{\"FunctionName.$\":\"$.Function.FunctionName\"}},\"HasRetention?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.RetentionInDays\",\"IsPresent\":false,\"Next\":\"AddRetention\"}],\"Default\":\"lgtm\"},\"lgtm\":{\"Type\":\"Pass\",\"Result\":{\"IsDeleted\":0,\"IsRetained\":0},\"End\":true},\"AddRetention\":{\"End\":true,\"Retry\":[{\"ErrorEquals\":[\"States.ALL\"],\"MaxAttempts\":10}],\"Type\":\"Task\",\"ResultSelector\":{\"IsDeleted\":0,\"IsRetained\":1},\"Resource\":\"arn:", 295 | { 296 | "Ref": "AWS::Partition" 297 | }, 298 | ":states:::aws-sdk:cloudwatchlogs:putRetentionPolicy\",\"Parameters\":{\"LogGroupName.$\":\"$.LogGroupName\",\"RetentionInDays\":7}},\"DeleteLG\":{\"End\":true,\"Retry\":[{\"ErrorEquals\":[\"States.ALL\"],\"MaxAttempts\":10}],\"Type\":\"Task\",\"ResultSelector\":{\"IsDeleted\":1,\"IsRetained\":0},\"Resource\":\"arn:", 299 | { 300 | "Ref": "AWS::Partition" 301 | }, 302 | ":states:::aws-sdk:cloudwatchlogs:deleteLogGroup\",\"Parameters\":{\"LogGroupName.$\":\"$.LogGroupName\"}}}},\"MaxConcurrency\":10},\"InitStatsLoop\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Iterator\",\"Parameters\":{\"Index\":0,\"ResultLen.$\":\"States.ArrayLength($.MapResult)\"},\"Next\":\"HasNextMapResult?\"},\"HasNextMapResult?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.Iterator.Index\",\"NumericLessThanPath\":\"$.Iterator.ResultLen\",\"Next\":\"GetNextResult\"}],\"Default\":\"SendSuccess\"},\"IncrementCounter\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Iterator\",\"Parameters\":{\"Index.$\":\"States.MathAdd($.Iterator.Index, 1)\",\"ResultLen.$\":\"$.Iterator.ResultLen\"},\"Next\":\"HasNextMapResult?\"},\"IncrementStats\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Stats\",\"Parameters\":{\"LGsDeleted.$\":\"States.MathAdd($.Stats.LGsDeleted, $.R.Result.IsDeleted)\",\"LGsRetained.$\":\"States.MathAdd($.Stats.LGsRetained, $.R.Result.IsRetained)\",\"LGsSeen.$\":\"$.Stats.LGsSeen\"},\"Next\":\"IncrementCounter\"},\"GetNextResult\":{\"Type\":\"Pass\",\"ResultPath\":\"$.R\",\"Parameters\":{\"Result.$\":\"States.ArrayGetItem($.MapResult, $.Iterator.Index)\"},\"Next\":\"IncrementStats\"},\"SendSuccess\":{\"End\":true,\"Type\":\"Task\",\"Resource\":\"arn:", 303 | { 304 | "Ref": "AWS::Partition" 305 | }, 306 | ":states:::aws-sdk:sfn:sendTaskSuccess\",\"Parameters\":{\"Output.$\":\"$.Stats\",\"TaskToken.$\":\"$.Token\"}}}}" 307 | ] 308 | ] 309 | }, 310 | "stateMachineName": "logs-comptroller-runner", 311 | "tracingConfiguration": { 312 | "enabled": true 313 | } 314 | } 315 | }, 316 | "constructInfo": { 317 | "fqn": "aws-cdk-lib.aws_stepfunctions.CfnStateMachine", 318 | "version": "2.46.0" 319 | } 320 | } 321 | }, 322 | "constructInfo": { 323 | "fqn": "aws-cdk-lib.aws_stepfunctions.StateMachine", 324 | "version": "2.46.0" 325 | } 326 | }, 327 | "GetLogGroups": { 328 | "id": "GetLogGroups", 329 | "path": "StackUnderTest/GetLogGroups", 330 | "constructInfo": { 331 | "fqn": "aws-cdk-lib.aws_stepfunctions_tasks.CallAwsService", 332 | "version": "2.46.0" 333 | } 334 | }, 335 | "SetLGsSeen": { 336 | "id": "SetLGsSeen", 337 | "path": "StackUnderTest/SetLGsSeen", 338 | "constructInfo": { 339 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 340 | "version": "2.46.0" 341 | } 342 | }, 343 | "ExecuteRunner": { 344 | "id": "ExecuteRunner", 345 | "path": "StackUnderTest/ExecuteRunner", 346 | "constructInfo": { 347 | "fqn": "aws-cdk-lib.aws_stepfunctions_tasks.StepFunctionsStartExecution", 348 | "version": "2.46.0" 349 | } 350 | }, 351 | "HasNextToken?": { 352 | "id": "HasNextToken?", 353 | "path": "StackUnderTest/HasNextToken?", 354 | "constructInfo": { 355 | "fqn": "aws-cdk-lib.aws_stepfunctions.Choice", 356 | "version": "2.46.0" 357 | } 358 | }, 359 | "GetNextLogGroups": { 360 | "id": "GetNextLogGroups", 361 | "path": "StackUnderTest/GetNextLogGroups", 362 | "constructInfo": { 363 | "fqn": "aws-cdk-lib.aws_stepfunctions_tasks.CallAwsService", 364 | "version": "2.46.0" 365 | } 366 | }, 367 | "AppendTotal": { 368 | "id": "AppendTotal", 369 | "path": "StackUnderTest/AppendTotal", 370 | "constructInfo": { 371 | "fqn": "aws-cdk-lib.aws_stepfunctions.Pass", 372 | "version": "2.46.0" 373 | } 374 | }, 375 | "Work Complete!": { 376 | "id": "Work Complete!", 377 | "path": "StackUnderTest/Work Complete!", 378 | "constructInfo": { 379 | "fqn": "aws-cdk-lib.aws_stepfunctions.Succeed", 380 | "version": "2.46.0" 381 | } 382 | }, 383 | "LogsComptrollerIterator": { 384 | "id": "LogsComptrollerIterator", 385 | "path": "StackUnderTest/LogsComptrollerIterator", 386 | "children": { 387 | "Role": { 388 | "id": "Role", 389 | "path": "StackUnderTest/LogsComptrollerIterator/Role", 390 | "children": { 391 | "Resource": { 392 | "id": "Resource", 393 | "path": "StackUnderTest/LogsComptrollerIterator/Role/Resource", 394 | "attributes": { 395 | "aws:cdk:cloudformation:type": "AWS::IAM::Role", 396 | "aws:cdk:cloudformation:props": { 397 | "assumeRolePolicyDocument": { 398 | "Statement": [ 399 | { 400 | "Action": "sts:AssumeRole", 401 | "Effect": "Allow", 402 | "Principal": { 403 | "Service": { 404 | "Fn::FindInMap": [ 405 | "ServiceprincipalMap", 406 | { 407 | "Ref": "AWS::Region" 408 | }, 409 | "states" 410 | ] 411 | } 412 | } 413 | } 414 | ], 415 | "Version": "2012-10-17" 416 | } 417 | } 418 | }, 419 | "constructInfo": { 420 | "fqn": "aws-cdk-lib.aws_iam.CfnRole", 421 | "version": "2.46.0" 422 | } 423 | }, 424 | "DefaultPolicy": { 425 | "id": "DefaultPolicy", 426 | "path": "StackUnderTest/LogsComptrollerIterator/Role/DefaultPolicy", 427 | "children": { 428 | "Resource": { 429 | "id": "Resource", 430 | "path": "StackUnderTest/LogsComptrollerIterator/Role/DefaultPolicy/Resource", 431 | "attributes": { 432 | "aws:cdk:cloudformation:type": "AWS::IAM::Policy", 433 | "aws:cdk:cloudformation:props": { 434 | "policyDocument": { 435 | "Statement": [ 436 | { 437 | "Action": [ 438 | "logs:DescribeLogGroups", 439 | "xray:GetSamplingRules", 440 | "xray:GetSamplingTargets", 441 | "xray:PutTelemetryRecords", 442 | "xray:PutTraceSegments" 443 | ], 444 | "Effect": "Allow", 445 | "Resource": "*" 446 | }, 447 | { 448 | "Action": "states:StartExecution", 449 | "Effect": "Allow", 450 | "Resource": { 451 | "Ref": "LogsComptrollerRunner2AB55B01" 452 | } 453 | } 454 | ], 455 | "Version": "2012-10-17" 456 | }, 457 | "policyName": "LogsComptrollerIteratorRoleDefaultPolicyE5E8B0AE", 458 | "roles": [ 459 | { 460 | "Ref": "LogsComptrollerIteratorRole3F6E7D1B" 461 | } 462 | ] 463 | } 464 | }, 465 | "constructInfo": { 466 | "fqn": "aws-cdk-lib.aws_iam.CfnPolicy", 467 | "version": "2.46.0" 468 | } 469 | } 470 | }, 471 | "constructInfo": { 472 | "fqn": "aws-cdk-lib.aws_iam.Policy", 473 | "version": "2.46.0" 474 | } 475 | } 476 | }, 477 | "constructInfo": { 478 | "fqn": "aws-cdk-lib.aws_iam.Role", 479 | "version": "2.46.0" 480 | } 481 | }, 482 | "Resource": { 483 | "id": "Resource", 484 | "path": "StackUnderTest/LogsComptrollerIterator/Resource", 485 | "attributes": { 486 | "aws:cdk:cloudformation:type": "AWS::StepFunctions::StateMachine", 487 | "aws:cdk:cloudformation:props": { 488 | "roleArn": { 489 | "Fn::GetAtt": [ 490 | "LogsComptrollerIteratorRole3F6E7D1B", 491 | "Arn" 492 | ] 493 | }, 494 | "definitionString": { 495 | "Fn::Join": [ 496 | "", 497 | [ 498 | "{\"StartAt\":\"GetLogGroups\",\"States\":{\"GetLogGroups\":{\"Next\":\"SetLGsSeen\",\"Type\":\"Task\",\"ResultPath\":\"$.LG\",\"Resource\":\"arn:", 499 | { 500 | "Ref": "AWS::Partition" 501 | }, 502 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\",\"Parameters\":{}},\"SetLGsSeen\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Stats\",\"Parameters\":{\"LGsDeleted\":0,\"LGsRetained\":0,\"LGsSeen.$\":\"States.ArrayLength($.LG.LogGroups)\"},\"Next\":\"ExecuteRunner\"},\"ExecuteRunner\":{\"Next\":\"HasNextToken?\",\"Type\":\"Task\",\"ResultPath\":\"$.Stats\",\"Resource\":\"arn:", 503 | { 504 | "Ref": "AWS::Partition" 505 | }, 506 | ":states:::states:startExecution.waitForTaskToken\",\"Parameters\":{\"Input\":{\"LogGroups.$\":\"$.LG.LogGroups\",\"Stats.$\":\"$.Stats\",\"Token.$\":\"$$.Task.Token\"},\"StateMachineArn\":\"", 507 | { 508 | "Ref": "LogsComptrollerRunner2AB55B01" 509 | }, 510 | "\"}},\"AppendTotal\":{\"Type\":\"Pass\",\"ResultPath\":\"$.Stats\",\"Parameters\":{\"LGsDeleted.$\":\"$.Stats.LGsDeleted\",\"LGsRetained.$\":\"$.Stats.LGsRetained\",\"LGsSeen.$\":\"States.MathAdd($.Stats.LGsSeen, States.ArrayLength($.LG.LogGroups))\"},\"Next\":\"ExecuteRunner\"},\"GetNextLogGroups\":{\"Next\":\"AppendTotal\",\"Type\":\"Task\",\"ResultPath\":\"$.LG\",\"Resource\":\"arn:", 511 | { 512 | "Ref": "AWS::Partition" 513 | }, 514 | ":states:::aws-sdk:cloudwatchlogs:describeLogGroups\",\"Parameters\":{\"NextToken.$\":\"$.LG.NextToken\"}},\"HasNextToken?\":{\"Type\":\"Choice\",\"Choices\":[{\"Variable\":\"$.LG.NextToken\",\"IsPresent\":true,\"Next\":\"GetNextLogGroups\"}],\"Default\":\"Work Complete!\"},\"Work Complete!\":{\"Type\":\"Succeed\",\"OutputPath\":\"$.Stats\"}}}" 515 | ] 516 | ] 517 | }, 518 | "stateMachineName": "logs-comptroller-iterator", 519 | "tracingConfiguration": { 520 | "enabled": true 521 | } 522 | } 523 | }, 524 | "constructInfo": { 525 | "fqn": "aws-cdk-lib.aws_stepfunctions.CfnStateMachine", 526 | "version": "2.46.0" 527 | } 528 | } 529 | }, 530 | "constructInfo": { 531 | "fqn": "aws-cdk-lib.aws_stepfunctions.StateMachine", 532 | "version": "2.46.0" 533 | } 534 | }, 535 | "Service-principalMap": { 536 | "id": "Service-principalMap", 537 | "path": "StackUnderTest/Service-principalMap", 538 | "constructInfo": { 539 | "fqn": "aws-cdk-lib.CfnMapping", 540 | "version": "2.46.0" 541 | } 542 | } 543 | }, 544 | "constructInfo": { 545 | "fqn": "aws-cdk-lib.Stack", 546 | "version": "2.46.0" 547 | } 548 | }, 549 | "Integ": { 550 | "id": "Integ", 551 | "path": "Integ", 552 | "children": { 553 | "DefaultTest": { 554 | "id": "DefaultTest", 555 | "path": "Integ/DefaultTest", 556 | "children": { 557 | "Default": { 558 | "id": "Default", 559 | "path": "Integ/DefaultTest/Default", 560 | "constructInfo": { 561 | "fqn": "constructs.Construct", 562 | "version": "10.1.131" 563 | } 564 | }, 565 | "DeployAssert": { 566 | "id": "DeployAssert", 567 | "path": "Integ/DefaultTest/DeployAssert", 568 | "children": { 569 | "AwsApiCallStepFunctionsstartExecution": { 570 | "id": "AwsApiCallStepFunctionsstartExecution", 571 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsstartExecution", 572 | "children": { 573 | "SdkProvider": { 574 | "id": "SdkProvider", 575 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsstartExecution/SdkProvider", 576 | "children": { 577 | "AssertionsProvider": { 578 | "id": "AssertionsProvider", 579 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsstartExecution/SdkProvider/AssertionsProvider", 580 | "constructInfo": { 581 | "fqn": "constructs.Construct", 582 | "version": "10.1.131" 583 | } 584 | } 585 | }, 586 | "constructInfo": { 587 | "fqn": "@aws-cdk/integ-tests-alpha.AssertionsProvider", 588 | "version": "2.46.0-alpha.0" 589 | } 590 | }, 591 | "Default": { 592 | "id": "Default", 593 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsstartExecution/Default", 594 | "children": { 595 | "Default": { 596 | "id": "Default", 597 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsstartExecution/Default/Default", 598 | "constructInfo": { 599 | "fqn": "aws-cdk-lib.CfnResource", 600 | "version": "2.46.0" 601 | } 602 | } 603 | }, 604 | "constructInfo": { 605 | "fqn": "aws-cdk-lib.CustomResource", 606 | "version": "2.46.0" 607 | } 608 | } 609 | }, 610 | "constructInfo": { 611 | "fqn": "@aws-cdk/integ-tests-alpha.AwsApiCall", 612 | "version": "2.46.0-alpha.0" 613 | } 614 | }, 615 | "SingletonFunction1488541a7b23466481b69b4408076b81": { 616 | "id": "SingletonFunction1488541a7b23466481b69b4408076b81", 617 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction1488541a7b23466481b69b4408076b81", 618 | "children": { 619 | "Staging": { 620 | "id": "Staging", 621 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction1488541a7b23466481b69b4408076b81/Staging", 622 | "constructInfo": { 623 | "fqn": "aws-cdk-lib.AssetStaging", 624 | "version": "2.46.0" 625 | } 626 | }, 627 | "Role": { 628 | "id": "Role", 629 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction1488541a7b23466481b69b4408076b81/Role", 630 | "constructInfo": { 631 | "fqn": "aws-cdk-lib.CfnResource", 632 | "version": "2.46.0" 633 | } 634 | }, 635 | "Handler": { 636 | "id": "Handler", 637 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction1488541a7b23466481b69b4408076b81/Handler", 638 | "constructInfo": { 639 | "fqn": "aws-cdk-lib.CfnResource", 640 | "version": "2.46.0" 641 | } 642 | } 643 | }, 644 | "constructInfo": { 645 | "fqn": "constructs.Construct", 646 | "version": "10.1.131" 647 | } 648 | }, 649 | "AwsApiCallStepFunctionsdescribeExecution": { 650 | "id": "AwsApiCallStepFunctionsdescribeExecution", 651 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution", 652 | "children": { 653 | "SdkProvider": { 654 | "id": "SdkProvider", 655 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/SdkProvider", 656 | "children": { 657 | "AssertionsProvider": { 658 | "id": "AssertionsProvider", 659 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/SdkProvider/AssertionsProvider", 660 | "constructInfo": { 661 | "fqn": "constructs.Construct", 662 | "version": "10.1.131" 663 | } 664 | } 665 | }, 666 | "constructInfo": { 667 | "fqn": "@aws-cdk/integ-tests-alpha.AssertionsProvider", 668 | "version": "2.46.0-alpha.0" 669 | } 670 | }, 671 | "Default": { 672 | "id": "Default", 673 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/Default", 674 | "children": { 675 | "Default": { 676 | "id": "Default", 677 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/Default/Default", 678 | "constructInfo": { 679 | "fqn": "aws-cdk-lib.CfnResource", 680 | "version": "2.46.0" 681 | } 682 | } 683 | }, 684 | "constructInfo": { 685 | "fqn": "aws-cdk-lib.CustomResource", 686 | "version": "2.46.0" 687 | } 688 | }, 689 | "WaitFor": { 690 | "id": "WaitFor", 691 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor", 692 | "children": { 693 | "IsCompleteProvider": { 694 | "id": "IsCompleteProvider", 695 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/IsCompleteProvider", 696 | "children": { 697 | "AssertionsProvider": { 698 | "id": "AssertionsProvider", 699 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/IsCompleteProvider/AssertionsProvider", 700 | "constructInfo": { 701 | "fqn": "constructs.Construct", 702 | "version": "10.1.131" 703 | } 704 | }, 705 | "Invoke": { 706 | "id": "Invoke", 707 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/IsCompleteProvider/Invoke", 708 | "constructInfo": { 709 | "fqn": "aws-cdk-lib.CfnResource", 710 | "version": "2.46.0" 711 | } 712 | } 713 | }, 714 | "constructInfo": { 715 | "fqn": "@aws-cdk/integ-tests-alpha.AssertionsProvider", 716 | "version": "2.46.0-alpha.0" 717 | } 718 | }, 719 | "TimeoutProvider": { 720 | "id": "TimeoutProvider", 721 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/TimeoutProvider", 722 | "children": { 723 | "AssertionsProvider": { 724 | "id": "AssertionsProvider", 725 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/TimeoutProvider/AssertionsProvider", 726 | "constructInfo": { 727 | "fqn": "constructs.Construct", 728 | "version": "10.1.131" 729 | } 730 | }, 731 | "Invoke": { 732 | "id": "Invoke", 733 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/TimeoutProvider/Invoke", 734 | "constructInfo": { 735 | "fqn": "aws-cdk-lib.CfnResource", 736 | "version": "2.46.0" 737 | } 738 | } 739 | }, 740 | "constructInfo": { 741 | "fqn": "@aws-cdk/integ-tests-alpha.AssertionsProvider", 742 | "version": "2.46.0-alpha.0" 743 | } 744 | }, 745 | "Role": { 746 | "id": "Role", 747 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/Role", 748 | "constructInfo": { 749 | "fqn": "aws-cdk-lib.CfnResource", 750 | "version": "2.46.0" 751 | } 752 | }, 753 | "Resource": { 754 | "id": "Resource", 755 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/WaitFor/Resource", 756 | "constructInfo": { 757 | "fqn": "aws-cdk-lib.CfnResource", 758 | "version": "2.46.0" 759 | } 760 | } 761 | }, 762 | "constructInfo": { 763 | "fqn": "@aws-cdk/integ-tests-alpha.WaiterStateMachine", 764 | "version": "2.46.0-alpha.0" 765 | } 766 | }, 767 | "AssertionResults": { 768 | "id": "AssertionResults", 769 | "path": "Integ/DefaultTest/DeployAssert/AwsApiCallStepFunctionsdescribeExecution/AssertionResults", 770 | "constructInfo": { 771 | "fqn": "aws-cdk-lib.CfnOutput", 772 | "version": "2.46.0" 773 | } 774 | } 775 | }, 776 | "constructInfo": { 777 | "fqn": "@aws-cdk/integ-tests-alpha.AwsApiCall", 778 | "version": "2.46.0-alpha.0" 779 | } 780 | }, 781 | "SingletonFunction76b3e830a873425f8453eddd85c86925": { 782 | "id": "SingletonFunction76b3e830a873425f8453eddd85c86925", 783 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction76b3e830a873425f8453eddd85c86925", 784 | "children": { 785 | "Staging": { 786 | "id": "Staging", 787 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction76b3e830a873425f8453eddd85c86925/Staging", 788 | "constructInfo": { 789 | "fqn": "aws-cdk-lib.AssetStaging", 790 | "version": "2.46.0" 791 | } 792 | }, 793 | "Role": { 794 | "id": "Role", 795 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction76b3e830a873425f8453eddd85c86925/Role", 796 | "constructInfo": { 797 | "fqn": "aws-cdk-lib.CfnResource", 798 | "version": "2.46.0" 799 | } 800 | }, 801 | "Handler": { 802 | "id": "Handler", 803 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction76b3e830a873425f8453eddd85c86925/Handler", 804 | "constructInfo": { 805 | "fqn": "aws-cdk-lib.CfnResource", 806 | "version": "2.46.0" 807 | } 808 | } 809 | }, 810 | "constructInfo": { 811 | "fqn": "constructs.Construct", 812 | "version": "10.1.131" 813 | } 814 | }, 815 | "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41a": { 816 | "id": "SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41a", 817 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41a", 818 | "children": { 819 | "Staging": { 820 | "id": "Staging", 821 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41a/Staging", 822 | "constructInfo": { 823 | "fqn": "aws-cdk-lib.AssetStaging", 824 | "version": "2.46.0" 825 | } 826 | }, 827 | "Role": { 828 | "id": "Role", 829 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41a/Role", 830 | "constructInfo": { 831 | "fqn": "aws-cdk-lib.CfnResource", 832 | "version": "2.46.0" 833 | } 834 | }, 835 | "Handler": { 836 | "id": "Handler", 837 | "path": "Integ/DefaultTest/DeployAssert/SingletonFunction5c1898e096fb4e3e95d5f6c67f3ce41a/Handler", 838 | "constructInfo": { 839 | "fqn": "aws-cdk-lib.CfnResource", 840 | "version": "2.46.0" 841 | } 842 | } 843 | }, 844 | "constructInfo": { 845 | "fqn": "constructs.Construct", 846 | "version": "10.1.131" 847 | } 848 | } 849 | }, 850 | "constructInfo": { 851 | "fqn": "aws-cdk-lib.Stack", 852 | "version": "2.46.0" 853 | } 854 | } 855 | }, 856 | "constructInfo": { 857 | "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", 858 | "version": "2.46.0-alpha.0" 859 | } 860 | } 861 | }, 862 | "constructInfo": { 863 | "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", 864 | "version": "2.46.0-alpha.0" 865 | } 866 | } 867 | }, 868 | "constructInfo": { 869 | "fqn": "aws-cdk-lib.App", 870 | "version": "2.46.0" 871 | } 872 | } 873 | } -------------------------------------------------------------------------------- /test/integ.default.ts: -------------------------------------------------------------------------------- 1 | import { ExpectedResult, IntegTest } from '@aws-cdk/integ-tests-alpha'; 2 | import { App, ArnFormat, Stack, StackProps } from 'aws-cdk-lib'; 3 | import { RequireApproval } from 'aws-cdk-lib/cloud-assembly-schema'; 4 | import { Construct } from 'constructs'; 5 | 6 | import { AwsLogsComptroller } from '../src'; 7 | 8 | class StackUnderTest extends Stack { 9 | constructor (scope: Construct, id: string, props?: StackProps) { 10 | super(scope, id, props); 11 | 12 | new AwsLogsComptroller(this, 'LogsComptroller', { 13 | }); 14 | } 15 | } 16 | 17 | const app = new App(); 18 | const stack = new StackUnderTest(app, 'StackUnderTest'); 19 | 20 | const integ = new IntegTest(app, 'Integ', { 21 | cdkCommandOptions: { 22 | deploy: { 23 | args: { 24 | json: true, requireApproval: RequireApproval.NEVER, 25 | }, 26 | }, 27 | destroy: { 28 | args: { 29 | force: true, 30 | }, 31 | }, 32 | }, 33 | diffAssets: true, 34 | stackUpdateWorkflow: true, 35 | testCases: [stack], 36 | }); 37 | 38 | const start = integ.assertions.awsApiCall('StepFunctions', 'startExecution', { 39 | stateMachineArn: Stack.of(stack).formatArn({ 40 | arnFormat: ArnFormat.COLON_RESOURCE_NAME, 41 | resource: 'stateMachine', 42 | resourceName: 'logs-comptroller-iterator', 43 | service: 'states', 44 | }), 45 | }); 46 | 47 | integ.assertions.awsApiCall('StepFunctions', 'describeExecution', { 48 | executionArn: start.getAttString('executionArn'), 49 | }).expect(ExpectedResult.objectLike({ 50 | status: 'SUCCEEDED', 51 | })).waitForAssertions(); 52 | -------------------------------------------------------------------------------- /tsconfig.dev.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2018", 4 | "module": "commonjs", 5 | "lib": [ 6 | "es2018" 7 | ], 8 | "declaration": true, 9 | "strict": true, 10 | "noImplicitAny": true, 11 | "strictNullChecks": true, 12 | "noImplicitThis": true, 13 | "alwaysStrict": true, 14 | "noUnusedLocals": false, 15 | "noUnusedParameters": false, 16 | "noImplicitReturns": true, 17 | "noFallthroughCasesInSwitch": false, 18 | "inlineSourceMap": true, 19 | "inlineSources": true, 20 | "experimentalDecorators": true, 21 | "strictPropertyInitialization": false, 22 | "typeRoots": [ 23 | "./node_modules/@types" 24 | ] 25 | }, 26 | "include": [ 27 | "lib", 28 | "test" 29 | ] 30 | } --------------------------------------------------------------------------------