Skip to content

Commit 5bf2a0c

Browse files
committed
feat: added configurable 'wait' step to state machine
1 parent 856e5f4 commit 5bf2a0c

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

API.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ The tree node.
116116
| --- | --- | --- |
117117
| <code><a href="#cdk-iam-credentials-rotator.IIamCredentialsRotatorProps.property.credentialsHandler">credentialsHandler</a></code> | <code>aws-cdk-lib.aws_lambda.IFunction</code> | Lambda function which is invoked after new credentials are created for a user. |
118118
| <code><a href="#cdk-iam-credentials-rotator.IIamCredentialsRotatorProps.property.usernames">usernames</a></code> | <code>string[]</code> | List of IAM usernames in target account. |
119+
| <code><a href="#cdk-iam-credentials-rotator.IIamCredentialsRotatorProps.property.cleanupWaitDuration">cleanupWaitDuration</a></code> | <code>aws-cdk-lib.Duration</code> | The amount of time to wait before deleting old credentials. |
119120
| <code><a href="#cdk-iam-credentials-rotator.IIamCredentialsRotatorProps.property.scheduleDuration">scheduleDuration</a></code> | <code>aws-cdk-lib.Duration</code> | Frequency of key rotation. |
120121

121122
---
@@ -144,6 +145,21 @@ List of IAM usernames in target account.
144145

145146
---
146147

148+
##### `cleanupWaitDuration`<sup>Optional</sup> <a name="cleanupWaitDuration" id="cdk-iam-credentials-rotator.IIamCredentialsRotatorProps.property.cleanupWaitDuration"></a>
149+
150+
```typescript
151+
public readonly cleanupWaitDuration: Duration;
152+
```
153+
154+
- *Type:* aws-cdk-lib.Duration
155+
- *Default:* 5 minutes
156+
157+
The amount of time to wait before deleting old credentials.
158+
159+
This value MUST be significantly less-than `scheduleDuration`.
160+
161+
---
162+
147163
##### `scheduleDuration`<sup>Optional</sup> <a name="scheduleDuration" id="cdk-iam-credentials-rotator.IIamCredentialsRotatorProps.property.scheduleDuration"></a>
148164

149165
```typescript

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ AWS CDK construct for rotating IAM user credentials and sending to a third party
77
Simply provide a list of usernames of IAM users which exist in the target account and a Lambda function to handle the newly created credentials for a given user.
88

99
```typescript
10-
const credentialsHandler = new lambda.Function(this, 'MyCredentialsHandler', {
10+
const myCredentialsHandler = new lambda.Function(this, 'MyCredentialsHandler', {
1111
handler: 'index.handler',
1212
code: lambda.Code.fromAsset('path/to/your/code'),
1313
runtime: lambda.Runtime.NODEJS_14_X,
1414
});
1515

1616
new IamCredentialsRotator(this, 'MyCredentialsRotator', {
1717
usernames: ['homer', 'marge', 'bart', 'lisa', 'maggie'],
18-
credentialsHandler,
18+
credentialsHandler: myCredentialsHandler,
1919
});
2020
```
2121

22-
The Lambda function, `credentialsHandler` is called immediately after a new access key is created for a user. The newly created credentials must be retrieved from AWS Secrets Manager using the secret name passed in to the function.
22+
The Lambda function, `credentialsHandler`, is called immediately after a new access key is created for a user. The newly created credentials must be retrieved from AWS Secrets Manager using the secret name passed in to the function.
2323

2424
By default, credentials are rotated once an hour. This can be changed by providing `scheduleDuration` in the constructor.
2525

@@ -50,7 +50,9 @@ export async function handler(event: Event) {
5050
}
5151
```
5252

53-
Once your function exits the secret will be deleted from AWS Secrets Manager and old credentials for the user are also deleted at this step.
53+
Once your function exits the underlying AWS Step Functions workflow will wait a period of time before deleting the old credentials. During this period both the old and new credentials for the user exist. At the end of this period the old credentials are deleted.
54+
55+
The amount of time to wait before deleting old credentials defaults to 5 minutes and can be adjusted by setting `cleanupWaitDuration`. This value MUST be less-than `scheduleDuration`.
5456

5557
## Architecture
5658

images/diagram.png

-5.68 KB
Loading

src/iam-credentials-rotator.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export interface IIamCredentialsRotatorProps {
2626
* @default 1 hour
2727
*/
2828
readonly scheduleDuration?: Duration;
29+
/**
30+
* The amount of time to wait before deleting old credentials.
31+
*
32+
* This value MUST be significantly less-than `scheduleDuration`.
33+
* @default 5 minutes
34+
*/
35+
readonly cleanupWaitDuration?: Duration;
2936
}
3037

3138
export class IamCredentialsRotator extends Construct {
@@ -122,8 +129,16 @@ export class IamCredentialsRotator extends Construct {
122129
inputPath: '$.Payload',
123130
});
124131

132+
const waitBeforeCleanup = new sfn.Wait(this, 'Wait', {
133+
time: sfn.WaitTime.duration(
134+
props.cleanupWaitDuration || Duration.minutes(5),
135+
),
136+
});
137+
125138
const definition = credentialsRotatorLambdaTask.next(
126-
credentialsHandlerLambdaTask.next(cleanupLambdaTask),
139+
credentialsHandlerLambdaTask.next(
140+
waitBeforeCleanup.next(cleanupLambdaTask),
141+
),
127142
);
128143

129144
const stateMachine = new sfn.StateMachine(

0 commit comments

Comments
 (0)