Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CloudFormation Transform - How do I properly return an error message?

I have a custom AWS::CloudFormation::Transform which is attached to a Lambda function. On successful responses, as mentioned in the documentation, I'm returning the following:

{
  "requestId": requestId,  //pulled from the event
  "status": "success",
  "fragment": value  //string value
}

This works fine. However, on an error case, I'm not entirely sure what to do. I know that according to the documentation, I should be returning the same structure but with status set to anything other than "success", and I'm assuming (because I can't seem to find anything to confirm this), the error message in the fragment portion. This is what I return on an error case:

{
  "requestId": requestId,  //pulled from the event
  "status": "failure",
  "fragment": err.code  //string value of error code
}

However, in my CloudFormation I get the following error:

Transform ############::MyCustomMacro failed without an error message.

I know based on the logs that the err.code has a value, so that's not the issue.

Is there something I'm missing on how to properly return an error to CloudFormation?

like image 558
Deiv Avatar asked Mar 15 '19 20:03

Deiv


People also ask

How do you check CloudFormation errors?

You can view logs, such as /var/log/cloud-init. log or /var/log/cfn-init. log , to help you debug the instance launch. You can retrieve the logs by logging in to your instance, but you must disable rollback on failure or else AWS CloudFormation deletes the instance after your stack fails to create.

How do you ensure that the CloudFormation template is valid and error free?

To check your template file for syntax errors, you can use the aws cloudformation validate-template command. The aws cloudformation validate-template command is designed to check only the syntax of your template.

How do I retry CloudFormation stack?

In the console, select the stack set that contains the stack on which the operation failed. In the Actions menu, choose Edit StackSet details to retry creating or updating stacks. On the Specify template page, to use the same AWS CloudFormation template, keep the default option, Use current template.

Does CloudFormation rollback on failure?

CloudFormation will continue to provision the resources until completion or stop on a different failure. Remediate any issues to continue the deployment process. CloudFormation performs the necessary updates before retrying provisioning actions on resources that couldn't be successfully provisioned earlier.


1 Answers

I was facing the same problem, but the following JSON response finally worked for me:

{
  "requestId": requestId,
  "status": "failure",
  "fragment": value,
  "errorMessage": customErrorMessage   // String value
}
like image 184
raza-zaidi Avatar answered Sep 27 '22 22:09

raza-zaidi