{"id":7071,"library":"cfnresponse","title":"cfnresponse","description":"cfnresponse is a micro Python package designed to simplify sending responses from AWS Lambda functions acting as custom resources back to AWS CloudFormation. It abstracts the HTTP call to the pre-signed S3 URL provided by CloudFormation, ensuring that stack operations (create, update, delete) complete successfully or fail gracefully. The current version is 1.1.5, and releases are infrequent, primarily for updates related to AWS Lambda runtime environments or underlying HTTP client changes.","status":"active","version":"1.1.5","language":"en","source_language":"en","source_url":"https://github.com/gene1wood/cfnresponse","tags":["aws","cloudformation","lambda","custom-resource","serverless"],"install":[{"cmd":"pip install cfnresponse","lang":"bash","label":"Install via pip"}],"dependencies":[],"imports":[{"note":"While 'import cfnresponse' is valid, directly importing `send`, `SUCCESS`, and `FAILED` is the idiomatic and common pattern for brevity.","wrong":"import cfnresponse","symbol":"send, SUCCESS, FAILED","correct":"from cfnresponse import send, SUCCESS, FAILED"}],"quickstart":{"code":"import json\nimport cfnresponse\n\ndef handler(event, context):\n    print(\"Received event: \" + json.dumps(event))\n\n    # Default to SUCCESS\n    status = cfnresponse.SUCCESS\n    reason = None\n    physical_resource_id = event.get('PhysicalResourceId', context.log_stream_name)\n    response_data = {}\n\n    try:\n        if event['RequestType'] == 'Create':\n            # Implement creation logic here\n            response_data['Message'] = 'Resource created successfully!'\n            # Often a unique ID for the resource is set as PhysicalResourceId\n            physical_resource_id = 'my-unique-resource-id'\n        elif event['RequestType'] == 'Update':\n            # Implement update logic here\n            response_data['Message'] = 'Resource updated successfully!'\n        elif event['RequestType'] == 'Delete':\n            # Implement deletion logic here\n            response_data['Message'] = 'Resource deleted successfully!'\n\n    except Exception as e:\n        status = cfnresponse.FAILED\n        reason = f'Lambda function failed: {str(e)}'\n        response_data['Error'] = str(e)\n        print(f\"Error: {e}\")\n\n    finally:\n        cfnresponse.send(event, context, status, response_data, physical_resource_id, reason=reason)\n        # Note: Any code after cfnresponse.send is generally not executed as the Lambda terminates.","lang":"python","description":"This quickstart demonstrates a typical AWS Lambda handler function for a CloudFormation custom resource using cfnresponse. It handles 'Create', 'Update', and 'Delete' request types, sends a success or failure response back to CloudFormation, and includes basic error handling. The `physical_resource_id` is crucial for tracking the resource lifecycle."},"warnings":[{"fix":"Ensure your Lambda function uses `cfnresponse` version 1.1.4 or higher. If deploying inline via `ZipFile`, CloudFormation automatically provides the updated module for current runtimes. For S3-deployed code, ensure you've bundled a recent `cfnresponse`.","message":"The underlying HTTP client for cfnresponse changed from `botocore.vendored.requests` to `urllib3` around April 2020. AWS Lambda environments stopped supporting the vendored `requests` by December 2021. Older Lambda functions using `cfnresponse` might fail if not updated.","severity":"breaking","affected_versions":"< 1.1.4 (cfnresponse), older Lambda runtimes"},{"fix":"Always ensure `cfnresponse.send()` is called exactly once in your Lambda handler, preferably within a `finally` block to guarantee execution even after errors. Include explicit handling for `RequestType == 'Delete'`.","message":"Failure to call `cfnresponse.send()` for any request type (Create, Update, Delete) will cause your CloudFormation stack operation to hang and eventually time out after a long delay (typically 1 hour).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Place all necessary logic before the `cfnresponse.send()` call. If cleanup or logging is required post-response, consider asynchronous methods or ensure it's handled before the final `send`.","message":"Any Python code placed after the `cfnresponse.send()` call will not be executed. The Lambda function terminates immediately after sending the response to CloudFormation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"The `PhysicalResourceId` should be stable across updates for the same logical resource. For `Update` requests, reuse `event['PhysicalResourceId']`. Only generate a new `PhysicalResourceId` for `Create` requests.","message":"Changing the `PhysicalResourceId` between an `Update` request can cause CloudFormation to treat it as a resource replacement (deletion of old, creation of new) rather than an in-place modification, leading to unexpected behavior or data loss.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"If using a `ZipFile` property in CloudFormation, ensure your CloudFormation template explicitly sets the Lambda `Runtime` property to a supported Python version. If deploying from S3, you must package `cfnresponse` with your Lambda code or use a Lambda Layer. Verify the handler path (e.g., `index.handler`).","cause":"The cfnresponse module is not found in the Lambda execution environment. This often happens if the Lambda function code is deployed from an S3 bucket without bundling `cfnresponse`, or if the Lambda runtime doesn't automatically include it (e.g., in newer Python versions if not deployed via inline ZipFile).","error":"Runtime.ImportModuleError: Unable to import module 'index': No module named 'cfnresponse'"},{"fix":"Update your `cfnresponse` library to version 1.1.4 or newer. If deploying via `ZipFile` in CloudFormation, ensure your template's `Runtime` property is set to a recent Python version (e.g., `python3.9` or higher) to automatically pick up the latest `cfnresponse` version provided by AWS. If bundling manually, include the latest `cfnresponse`.","cause":"This warning indicates your Lambda function is using an older version of `cfnresponse` that relied on a deprecated vendored `requests` library within `botocore`. This dependency was removed, and newer AWS Lambda runtimes no longer support it.","error":"/var/task/botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the put() function from 'botocore.vendored.requests'."}]}