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

How to test Lambdas using crhelper #52

Open
SchnitzelKopf opened this issue Aug 19, 2021 · 4 comments
Open

How to test Lambdas using crhelper #52

SchnitzelKopf opened this issue Aug 19, 2021 · 4 comments

Comments

@SchnitzelKopf
Copy link

Sorry for this stupid question...

How do I run tests? When using cfnresponse I could simply create a standard Lambda Cloudformation TestEvent and somewhat test my lambda (besides the cfnresponse failing in the end).

When trying the same with a Lambda using crhelper I run into loads of errors as I do not want to specify a CFN Stack to send the response to. I simply want to run my code and not communicate with CFN-Stacks (as there are none so far).

Here is my sample test event which returns this error. Basically it's a createSnapshot for elastiCache, however I don't think this is relevant. The final product would be a CFN Template accessible via ServiceCatalog where customers enter their ReplicationGroupId to create a snapshot.
" socket.gaierror: [Errno -2] Name or service not known[ERROR] Unexpected failure sending response to CloudFormation [Errno -2] Name or service not known "

{ "RequestType": "Create", "ResponseURL": "http://pre-signed-S3-url-for-response", "StackId": "arn:aws:cloudformation:eu-central-1:123456789012:stack/MyStack/guid", "RequestId": "unique id for this create request", "ResourceType": "Custom::TestResource", "LogicalResourceId": "MyTestResource", "ResourceProperties": { "StackName": "MyStack", "ApplicationName": "abcd", "SnapshotMode": "Create", "CacheClusterId": "", "ReplicationGroupId": "abcd-ttwoshards", "CostReference": "abc", "KmsKeyId": "abc", "SnapshotName": "mySnapshotviaLambda" } }

Thanks you for helping a newb out. If this isn't the right place to ask this question please let me know the right place to ask, or link a me docs explaining what I am tryin to achieve.

@nikoizs
Copy link

nikoizs commented Nov 18, 2021

+1. I am encountering a similar situation where I am trying to test my custom resource lambda function locally with local-invoke, I want to be able to return a success in a local test without calling Cloudformation.

@Lewenhaupt
Copy link

You should be able to create a mock endpoint for the ResponseURL?

@iainelder
Copy link

iainelder commented Feb 3, 2022

You don't need to create a stack to test it. (And it's just as well because a broken custom resource lambda can all too easily cause CloudFormation to hang for an hour or more!)

You can do this by mocking the _send method of the CfnResource class. I described it in more detail in #54. Does it help?

Following @Lewenhaupt's suggestion, if you don't mind creating a temporary S3 bucket for the test, then you could just create a bucket and use its URL for your test requests. According to the _send_response function then it should work, but I'll admit I haven't tried this yet.

@mdavis-xyz
Copy link

mdavis-xyz commented Jun 4, 2023

Alternatively if you assume this library is bug-free and you want to test just your own code (e.g. you don't want the library to catch and ignore exceptions during deletion) just use a wrapper.


@helper.create
def create(event, context):
    return _create(event, context)
def _create(event, context=None):
    your code here

Then in your test:

def my_test():
    ret = _create(event)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants