Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I conditionally invoke a lambda function in an AWS Appsync resolver?

I have attached an Appsync pipeline resolver to a field called paymentStatus in my Organisation object. The idea is that if an organisations last pay day has passed, I want to fetch the payment status from an external API using a Lambda function. If the pay day has not passed, I do not want to invoke the function but simply return a "OK".

Is there any way I can conditionally invoke a Lambda function? Something like this:

#if ($ctx.source.payday < $util.time.nowEpochSeconds()) 
    {
        "version": "2017-02-28",
        "operation": "Invoke",
        "payload": {
            "arguments": {
                "orgID": "$ctx.source.id"
            }
        }
    }
#end

If I run this, Appsync complains about operation attribute missing when the condition is not met. I have also noted that the condition attribute which exists for queries is not available for Lambda datasources.

Thank you in advance <3

like image 255
mufasa Avatar asked Aug 31 '25 10:08

mufasa


1 Answers

You can use the #return directive inside the request mapping template to return early from the template effectively returning early from the unit resolver.

Your request mapping template can look like this:

#if ($ctx.source.payday >= $util.time.nowEpochSeconds()) 
  #set($result = "OK")
  #return($result)
#end

{
    "version": "2017-02-28",
    "operation": "Invoke",
    "payload": {
        "arguments": {
            "orgID": "$ctx.source.id"
        }
    }
}

For more details you can read https://docs.aws.amazon.com/appsync/latest/devguide/resolver-util-reference.html#aws-appsync-directives

like image 56
Tinou Avatar answered Sep 03 '25 03:09

Tinou