Lakshman Diwaakar
Lakshman Diwaakar

Reputation: 7579

Creating a cloudFormation waitCondition with constant timeout

I am using Two Custom Resources in my cloudFormation template. Basically these Custom Resources are lambda functions which have custom code. I want to start the creation of second lambda after a constant 3 minutes.

I thought using the cloudFormation's WaitCondition with timeout property to solve this. But it need a WaitHandle which has to receive success signals before the timeout. Once the signals are received, the WaitCondition turns to Create-Complete. But in my case, I cant make the custom function to send signal to wait handle. I need to have a constant 3 minute wait time after completion of First Custom Resource. Then, start the creation of Second Custom Resource after the Create-Complete of WaitCondition. Here is my code:

"SecondCustomResource": {
  "Type": "Custom::SecondCustomResource",
  "DependsOn" : "WaitCondition",
  "Properties": {
    "ServiceToken": { "Fn::GetAtt" : ["SecondCustomResourceFunction", "Arn"] }
  }
},


"SecondCustomResourceFunction": {
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Code": {
        "S3Bucket": { "Ref": "S3Bucket" },
        "S3Key": { "Ref": "S3Key" }
    },
    "Handler": { "Fn::Join" : [ "", [{ "Ref": "ModuleName" },".handler"] ] },
    "Runtime": "nodejs4.3",
    "Timeout": "30"
  }
},


"WaitCondition": {
  "Type" : "AWS::CloudFormation::WaitCondition",
  "DependsOn" : "FirstCustomResource",
  "Properties": {
    "Timeout": "180"
  }
},


"FirstCustomResource": {
  "Type": "Custom::FirstCustomResource",
  "Properties": {
    "ServiceToken": { "Fn::GetAtt" : ["FirstCustomResourceFunction", "Arn"] }
  }
},


"FirstCustomResourceFunction": {
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Code": {
        "S3Bucket": { "Ref": "S3Bucket" },
        "S3Key": { "Ref": "S3Key" }
    },
    "Handler": { "Fn::Join" : [ "", [{ "Ref": "ModuleName" },".handler"] ] },
    "Runtime": "nodejs4.3",
    "Timeout": "30"
  }
}

This does not seem to work. Any hack or work-around to have a constant WaitCondition ?

Upvotes: 3

Views: 2298

Answers (1)

Tim Bassett
Tim Bassett

Reputation: 1168

I just want a constant 3 minute wait time between my first and second custom resource. Is this possible ? <<

Probably. I would think you want to try it by doing these two things. 1. Put a "sleep" function into the FirstCustomResourceFunction that sleeps for 3 minutes before signalling success. See this, super important, look for "SUCCESS" http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html

  1. Have SecondCustomResourceFunction depend on FirstCustomResource. That way it will not start until FirstCustomResource is complete (aka sleep and then success).

I think you can get rid of the WaitConditions then.

Be aware that "All calls made to AWS Lambda must complete execution within 300 seconds.". (https://aws.amazon.com/lambda/faqs/) So, if your deployment grows by 60%, you're probably cooked. (That's one of the reasons I was trying to steer you away from using Lambda as a wait state). I really would try to find some better way. In 15 years as DevOps, I've never arrived at "a constant value wait state" being a successful long term solution.

Upvotes: 1

Related Questions