Skip to main content

Wrapper decorators for building CloudFormation custom resources

Project description

## cfn_resource.py

This project is a decorator and validation system that takes the drudgery out
of writing custom resources. You still have access to the context and event as
normal, but the decorator handles serializing your response and communicating
results to CloudFormation.

See [cfn-lambda](https://github.com/andrew-templeton/cfn-lambda) from Andrew
Templeton if you're looking to write your custom resources in Node.js.

## Usage

1. Copy `cfn_resource.py` into the directory of your lambda function handler.py
1. Use the `cfn_resource.Resource` event decorators to decorate your handler
like in `example.py`
1. Zip up the contents and upload to Lambda

Once the function is up, copy its ARN and use it as the ServiceToken for your
[custom resource][rsrc]. For more on the requests you may receive, see
[this document][reqdocs]

```json
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"FakeThing": {
"Type": "Custom::MyResource",
"Properties": {
"ServiceToken": "arn:aws:lambda:SOME-REGION:ACCOUNT:function:FunctionName",
"OtherThing": "foobar",
"AnotherThing": 2
}
}
}
}
```

For more on how custom resources work, see the [AWS docs][docs]

## Code Sample

For this example, you need to have your handler in Lambda set as
`filename.handler` where filename has the below contents.

```
import cfn_resource

# set `handler` as the entry point for Lambda
handler = cfn_resource.Resource()

@handler.create
def create_thing(event, context):
# do some stuff
return {"PhysicalResourceId": "arn:aws:fake:myID"}

@handler.update
def update_thing(event, context):
# do some stuff
return {"PhysicalResourceId": "arn:aws:fake:myID"}
```

## Running Tests

To run the tests locally, you need Python 2.7 and `pip`. Ideally, you should
use a virtualenv.

```
$ pip install -r test-requirements.txt
$ py.test
```

The tests use `mock` and `py.test` and will give you a terminal coverage
report. Currently the tests cover ~90% of the (very small) codebase.

## License

This code is released under the MIT software license, see LICENSE.txt for
details. No warranty of any kind is included, and the copyright notice must be
included in redistributions.

[rsrc]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cfn-customresource.html
[docs]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html
[reqdocs]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-requests.html

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cfn_resource-0.2.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

cfn_resource-0.2.1.linux-x86_64.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file cfn_resource-0.2.1.tar.gz.

File metadata

File hashes

Hashes for cfn_resource-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d581e51d0a3d4c969a55485ce02de7bc55b2bbe2e7c1e6873628f96ba5d8e9ac
MD5 2307abb50dc0be3840200abaa5a21ce9
BLAKE2b-256 eb0e23b4c160ceba62254b0f8adb658902a2fb19176710d126115fed344ef433

See more details on using hashes here.

File details

Details for the file cfn_resource-0.2.1.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for cfn_resource-0.2.1.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 da6e727a19dc9f600981027ef469643c46dc8ebcb26695a47f5cd66f6478f372
MD5 1b6f6bdbd4f6c01e11cc2f8eafc85b34
BLAKE2b-256 ebe826ec9572455cf59207f8b0f552748d84d01305480bedcc56ccf8296b2d79

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page