How to view triggers associated with an AWS Lambda function using the Java SDK
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.
source to share
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:
- on the lambda service call the function
getPolicy
, thegetPolicy
name of your lambda. - The result can contain a policy document which is itself a JSON string
- 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/###"
source to share