|
| 1 | +import {Construct} from "constructs" |
| 2 | +import {Duration, CustomResource} from "aws-cdk-lib" |
| 3 | +import {Function, Runtime, Code} from "aws-cdk-lib/aws-lambda" |
| 4 | +import {Role, ServicePrincipal, ManagedPolicy} from "aws-cdk-lib/aws-iam" |
| 5 | +import {Provider} from "aws-cdk-lib/custom-resources" |
| 6 | + |
| 7 | +export interface DelayResourceProps { |
| 8 | + /** |
| 9 | + * The delay time in seconds (default: 30 seconds) |
| 10 | + */ |
| 11 | + readonly delaySeconds?: number |
| 12 | + |
| 13 | + /** |
| 14 | + * Optional description for the delay resource |
| 15 | + */ |
| 16 | + readonly description?: string |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * a fix for an annoying time sync issue that adds a configurable delay |
| 21 | + * to ensure AWS resources are fully available before dependent resources are created |
| 22 | + */ |
| 23 | +export class DelayResource extends Construct { |
| 24 | + public readonly customResource: CustomResource |
| 25 | + public readonly delaySeconds: number |
| 26 | + |
| 27 | + constructor(scope: Construct, id: string, props: DelayResourceProps = {}) { |
| 28 | + super(scope, id) |
| 29 | + |
| 30 | + this.delaySeconds = props.delaySeconds || 30 |
| 31 | + |
| 32 | + // create IAM role for the delay Lambda function |
| 33 | + const lambdaExecutionRole = new Role(this, "LambdaExecutionRole", { |
| 34 | + assumedBy: new ServicePrincipal("lambda.amazonaws.com"), |
| 35 | + description: "Execution role for delay custom resource Lambda function", |
| 36 | + managedPolicies: [ |
| 37 | + ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSLambdaBasicExecutionRole") |
| 38 | + ] |
| 39 | + }) |
| 40 | + |
| 41 | + // create the delay Lambda function with inline Python code |
| 42 | + const delayFunction = new Function(this, "DelayFunction", { |
| 43 | + runtime: Runtime.PYTHON_3_12, |
| 44 | + handler: "index.handler", |
| 45 | + role: lambdaExecutionRole, |
| 46 | + timeout: Duration.minutes(15), // max Lambda timeout to handle long delays |
| 47 | + description: props.description || `Delay resource for ${this.delaySeconds} seconds`, |
| 48 | + code: Code.fromInline(` |
| 49 | +from time import sleep |
| 50 | +import json |
| 51 | +import cfnresponse |
| 52 | +import uuid |
| 53 | +
|
| 54 | +def handler(event, context): |
| 55 | + wait_seconds = 0 |
| 56 | + id = str(uuid.uuid1()) |
| 57 | +
|
| 58 | + print(f"Received event: {json.dumps(event, default=str)}") |
| 59 | +
|
| 60 | + try: |
| 61 | + if event["RequestType"] in ["Create", "Update"]: |
| 62 | + wait_seconds = int(event["ResourceProperties"].get("WaitSeconds", 0)) |
| 63 | + print(f"Waiting for {wait_seconds} seconds...") |
| 64 | + sleep(wait_seconds) |
| 65 | + print(f"Wait complete") |
| 66 | +
|
| 67 | + response = { |
| 68 | + "TimeWaited": wait_seconds, |
| 69 | + "Id": id, |
| 70 | + "Status": "SUCCESS" |
| 71 | + } |
| 72 | +
|
| 73 | + cfnresponse.send(event, context, cfnresponse.SUCCESS, response, f"Waiter-{id}") |
| 74 | +
|
| 75 | + except Exception as e: |
| 76 | + print(f"Error: {str(e)}") |
| 77 | + cfnresponse.send(event, context, cfnresponse.FAILED, {"Error": str(e)}, f"Waiter-{id}") |
| 78 | +`) |
| 79 | + }) |
| 80 | + |
| 81 | + // create the custom resource provider |
| 82 | + const provider = new Provider(this, "DelayProvider", { |
| 83 | + onEventHandler: delayFunction |
| 84 | + }) |
| 85 | + |
| 86 | + // create the custom resource that triggers the delay |
| 87 | + this.customResource = new CustomResource(this, "DelayCustomResource", { |
| 88 | + serviceToken: provider.serviceToken, |
| 89 | + properties: { |
| 90 | + WaitSeconds: this.delaySeconds, |
| 91 | + Description: props.description || `Delay for ${this.delaySeconds} seconds`, |
| 92 | + // timestamp to ensure updates trigger when properties change |
| 93 | + Timestamp: Date.now() |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments