Reputation: 63
Need your help! From the API Gateway, I'm trying to trigger a Lambda function. This Lambda function is going to create a CloudFormation stack and the stack is in-turn going to deploy an EC2-instance. Below is part of the code that initiates the stack creation and waits for the stack creation status to be set to complete before it can return a response. Thing is API Gateway has an hard-coded timeout value of 30 seconds and the stack creation does not complete before 30 seconds. In this scenario, the API request just times-out returning an internal server error. How do I handle this?
# Create the CloudFormation Stack
StackID = cf_client.create_stack(
StackName=stackname,
TemplateURL='https://s3-bucket/template1.template',
Capabilities=['CAPABILITY_NAMED_IAM']
)
waiter = cf_client.get_waiter('stack_create_complete')
waiter.wait(
StackName=stackname,
WaiterConfig={
'Delay' : 5,
'MaxAttempts' : 50
}
)
Upvotes: 3
Views: 1355
Reputation: 35176
Perhaps instead of the Lambda directly creating the CloudFormation stack it could trigger a step function with a Lambda function solely for creating the CloudFormation stacks. The original function could return the execution id when it returns after running the start_execution function.
This serves 2 benefits, the first is that it will be much quicker (once the step function execution has begun a response is returned back by the SDK so this will be quicker), as well as being resilient to failure (you have the option to retry or notify on errors).
Alternatively if you want the request itself to do this within the HTTP request from the client, you would need to look at an ALB serving the traffic to your Lambda rather than API Gateway (which has a maximum cutoff). An ALB can support a longer timeout of upto 4000 seconds.
Upvotes: 2