7

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.

2
  • It seems you can't add/retrieve triggers associated with any Lambda function at this moment. Commented Apr 27, 2017 at 19:16
  • Did you try getSourceArn() method? If your trigger is associated with S3, it seems this should list S3 ARN. docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/… Commented Apr 27, 2017 at 19:44

4 Answers 4

7

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 '.'
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure why there wasn't an upvote on this, but thank you!
4

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.

Comments

3

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/###"

1 Comment

This is similar to what I had to do here: stackoverflow.com/a/55189358/1592879
-4

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

2 Comments

As Corey mentions, this method only returns Kinesis events (and I think DynamoDB events as well) so this doesn't quite do it.
as mentioned above, this only works for SQS, DynamoDB streams, and Kinesis streams.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.