How to view triggers associated with an AWS Lambda function using the Java SDK

Inside the AWS Lambda Console, if you click on the Triggers tab, you will see a list of triggers if any triggers are configured for this lambda function. How do I get a list of configured triggers using the AWS Java SDK?

Thank.

+3


source to share


3 answers


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

You searched for:



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

0


source


You can get a list of triggers, but this is not an easy task. I was able to reproduce the console behavior in code. I am code in Node.js but using these methods in Java SDK will give you the same results.

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

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

3) Use S3's getBucketNotificationConfiguration () method to get a list of triggers for each bucket found in 2).



4) parse the result from 3) and search the LambdaFunctionConfigurations nodes for the corresponding LambdaFunctionArn of your Lambda. The respective nodes have information about any triggers from the S3 buckets from 2) to your Lambda.

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

Note. The answer provided by johni on Apr 29 is wrong. When I tried, I found out that this method returns Kinesis events. Triggers from other AWS sources are only visible in Lambda Function Policy JSON.

0


source


It seems odd that they are not listed directly when you get the details of the lambda function, but from a UI perspective they seem to be part of the lambda. Someone already pointed to a policy document assigned to a lambda that you can use to define other AWS resources that are allowed to reference this lambda.

I've been working in Go working on this, but the approach for Java will be basically the same. In go, it looks 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:

  1. on the lambda service call the function getPolicy

    , the getPolicy

    name of your lambda.
  2. The result can contain a policy document which is itself a JSON string
  3. From this policy, you can identify the calling resources.

As an example, the result of analyzing this policy for a lambda that can be called from a cognito trigger is given:

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

      

0


source







All Articles