Arpan Solanki
Arpan Solanki

Reputation: 855

How to list triggers associated with AWS Lambda function using Java SDK

Inside AWS Lambda console if you click on Triggers tab it will show you list of triggers if any triggers are configured for that lambda function. How to get the list of configured triggered using Java SDK for AWS?

Thanks.

Upvotes: 7

Views: 11793

Answers (4)

andrew lorien
andrew lorien

Reputation: 2688

A simple AWS CLI (I know it's not Java, but it'll get you the structure)

aws lambda get-policy --function-name your-function-name --query 'Policy' --output text| jq '.'

Upvotes: 7

Jos Dirksen
Jos Dirksen

Reputation: 1903

It looks kind of strange that these aren't listed directly when you get the details of a lambda function, while from a UI point of view they seem to be part of the lambda. Someone already pointed to the policy document assigned to the lambda, which you can use to determine the other AWS resources which are allowed to invoke this lambda.

I was working in Go when running into this, but the approach for Java will be mostly the same. In go it looks something like this:

func(p *AWSParser) getEventTriggers(functionName string) *[]Trigger {

    var triggers []Trigger
    res2, err := p.lambdaSvc.GetPolicy(&lambda.GetPolicyInput{FunctionName: &functionName})

    if err == nil {
        polAsJson := gjson.Parse(*res2.Policy)
        polAsJson.Get("Statement").ForEach(func (_, value gjson.Result) bool {
            sid := value.Get("Sid").String()
            effect := value.Get("Effect").String()
            action := value.Get("Action").String()
            sourceArn := value.Get("Condition.ArnLike.AWS:SourceArn").String()

            triggers = append(triggers, Trigger{Sid: sid, Effect: effect, Action: action, SourceArn: sourceArn})
            return true
        })
    }

    return &triggers
}

Basically what you need to do is:

  1. on the lambda service call the getPolicy function, providing the name of your Lambda.
  2. The result can contain a policy document, which in itself is a JSON string
  3. From this policy you can determine the invoking resources.

As an example the following is the result of parsing this policy for a lambda which can be invoked from a cognito trigger:

    "Sid": "CSI_customMessage_eu-west-1qTL8mCdN9_CSI_customMessage",
    "Effect": "Allow",
    "Action": "lambda:InvokeFunction",
    "SourceArn": "arn:aws:cognito-idp:eu-west-1:###:userpool/###"

Upvotes: 3

Corey A. Henderson
Corey A. Henderson

Reputation: 156

You can get the list of triggers, but it is not a straightforward task. I have managed to reproduce the behavior of the console in code. I code in Node.js, but using these methods in the Java SDK will give you the same results.

1) Use the Lambda getPolicy() method to retrieve the Policy JSON (the same one displayed in the console under Triggers / View Function Policy).

2) Parse the JSON and use the "Statement"/"Condition"/"ArnLike"/"AWS:SourceArn" elements to parse out the S3 bucket names for the triggers.

3) Use the S3 getBucketNotificationConfiguration() method to retrieve the list of triggers for each bucket found in 2).

4) Parse the result from 3) and search through the LambdaFunctionConfigurations nodes for the matching LambdaFunctionArn of your Lambda. The matching nodes have the details of any triggers from the S3 buckets from 2) to your Lambda.

I would assume that triggers from other AWS sources than S3 buckets can be found similarly, but my use case was only for S3 buckets.

Note: The answer provided by johni on 29-Apr is incorrect. When I attempted it I learned that this method only returns Kinesis events. Triggers from other AWS sources are only visible in the Lambda Function's Policy JSON.

Upvotes: 4

johni
johni

Reputation: 5568

Of course this is possible. The triggers that you mean are called EventSources in their documentation.

You were looking for this:

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/lambda/model/ListEventSourceMappingsResult.html

Upvotes: -4

Related Questions