Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling multiple resource types in a single function #13

Open
vahdet opened this issue May 19, 2019 · 1 comment
Open

Handling multiple resource types in a single function #13

vahdet opened this issue May 19, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@vahdet
Copy link

vahdet commented May 19, 2019

With this library and its decorators, is it possible to map the received events to multiple functions depending on their ResourceTypes?

For instance. I imagine multiple function definitions annotated with @helper.create and make CfnResource can decide which one to run depending on the resource type.

is it possible this way or in some other manner?

@jaymccon
Copy link
Contributor

jaymccon commented May 20, 2019

supporting multiple resource types with a single lambda is not supported in the library currently. You could easily create a wrapper that fans out to multiple lambdas depending on type, something like:

import boto3
import json
from crhelper import CfnResource
import logging

logger = logging.getLogger(__name__)
# Initialise the helper, all inputs are optional, this example shows the defaults
helper = CfnResource(json_logging=False, log_level='DEBUG', boto_level='CRITICAL')

try:
    HANDLER_MAP = {
        "Custom::SomeType": "SomeType-function",
        "Custom::SomeOtherType": "SomeOtherType-function"
    }
    lambda_client = boto3.client('lambda')
except Exception as e:
    helper.init_failure(e)


@helper.create
@helper.update
@helper.delete
def handler(event, context):
    func_name = HANDLER_MAP[event['ResourceType']]
    lambda_client.invoke(FunctionName=func_name, InvocationType='Event', Payload=json.dumps(event))

crhelper can be implemented in the target functions as if they were being invoked directly by cloudformation

@jaymccon jaymccon added the enhancement New feature or request label Jul 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants