|
| 1 | +cfn\_resource.py |
| 2 | +---------------- |
| 3 | + |
| 4 | +This project is a decorator and validation system that takes the |
| 5 | +drudgery out of writing custom resources. You still have access to the |
| 6 | +context and event as normal, but the decorator handles serializing your |
| 7 | +response and communicating results to CloudFormation. |
| 8 | + |
| 9 | +See `cfn-lambda <https://github.com/andrew-templeton/cfn-lambda>`__ from |
| 10 | +Andrew Templeton if you're looking to write your custom resources in |
| 11 | +Node.js. |
| 12 | + |
| 13 | +Usage |
| 14 | +----- |
| 15 | + |
| 16 | +1. Copy ``cfn_resource.py`` into the directory of your lambda function |
| 17 | + handler.py |
| 18 | +2. Use the ``cfn_resource.Resource`` event decorators to decorate your |
| 19 | + handler like in ``example.py`` |
| 20 | +3. Zip up the contents and upload to Lambda |
| 21 | + |
| 22 | +Once the function is up, copy its ARN and use it as the ServiceToken for |
| 23 | +your `custom |
| 24 | +resource <https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cfn-customresource.html>`__. |
| 25 | +For more on the requests you may receive, see `this |
| 26 | +document <https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-requests.html>`__ |
| 27 | + |
| 28 | +.. code-block:: json |
| 29 | +
|
| 30 | + { |
| 31 | + "AWSTemplateFormatVersion": "2010-09-09", |
| 32 | + "Resources": { |
| 33 | + "FakeThing": { |
| 34 | + "Type": "Custom::MyResource", |
| 35 | + "Properties": { |
| 36 | + "ServiceToken": "arn:aws:lambda:SOME-REGION:ACCOUNT:function:FunctionName", |
| 37 | + "OtherThing": "foobar", |
| 38 | + "AnotherThing": 2 |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | +
|
| 44 | +For more on how custom resources work, see the `AWS |
| 45 | +docs <https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html>`__ |
| 46 | + |
| 47 | +Code Sample |
| 48 | +----------- |
| 49 | + |
| 50 | +For this example, you need to have your handler in Lambda set as |
| 51 | +``filename.handler`` where filename has the below contents. |
| 52 | + |
| 53 | +.. code-block:: python |
| 54 | +
|
| 55 | + import cfn_resource |
| 56 | +
|
| 57 | + # set `handler` as the entry point for Lambda |
| 58 | + handler = cfn_resource.Resource() |
| 59 | +
|
| 60 | + @handler.create |
| 61 | + def create_thing(event, context): |
| 62 | + # do some stuff |
| 63 | + return {"PhysicalResourceId": "arn:aws:fake:myID"} |
| 64 | +
|
| 65 | + @handler.update |
| 66 | + def update_thing(event, context): |
| 67 | + # do some stuff |
| 68 | + return {"PhysicalResourceId": "arn:aws:fake:myID"} |
| 69 | +
|
| 70 | +Running Tests |
| 71 | +------------- |
| 72 | + |
| 73 | +To run the tests locally, you need Python 2.7 and ``pip``. Ideally, you |
| 74 | +should use a virtualenv. |
| 75 | + |
| 76 | +.. code-block:: sh |
| 77 | +
|
| 78 | + $ pip install -r test-requirements.txt |
| 79 | + $ py.test |
| 80 | +
|
| 81 | +The tests use ``mock`` and ``py.test`` and will give you a terminal |
| 82 | +coverage report. Currently the tests cover ~90% of the (very small) |
| 83 | +codebase. |
| 84 | + |
| 85 | +License |
| 86 | +------- |
| 87 | + |
| 88 | +This code is released under the MIT software license, see LICENSE.txt |
| 89 | +for details. No warranty of any kind is included, and the copyright |
| 90 | +notice must be included in redistributions. |
0 commit comments