enter image description here Is it possible to invoke aws lambda function from the aws publish subscribe sample java code? Can we invoke lambda function from our java code? if yes then how?
2
-
1Try first and if some challenge, then mention that specificallyNitinSingh– NitinSingh2018-05-11 05:59:26 +00:00Commented May 11, 2018 at 5:59
-
I don't know from where to start my lambda is ready and the pub/sub sample code from aws java is ready I don't know how m I supposed to trigger my lambda function from my code.Ashu– Ashu2018-05-11 10:41:19 +00:00Commented May 11, 2018 at 10:41
Add a comment
|
1 Answer
Invoking an AWS Lambda Function from Java
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;
// ...
// (1) Define the AWS Region in which the function is to be invoked
Regions region = Regions.fromName("us-east-1");
// (2) Instantiate AWSLambdaClientBuilder to build the Lambda client
AWSLambdaClientBuilder builder = AWSLambdaClientBuilder.standard()
.withRegion(region);
// (3) Build the client, which will ultimately invoke the function
AWSLambda client = builder.build();
// (4) Create an InvokeRequest with required parameters
InvokeRequest req = new InvokeRequest()
.withFunctionName("myFunctionName")
.withPayload("{ ... }"); // optional
// (5) Invoke the function and capture response
InvokeResult result = client.invoke(req);
// (6) Handle result
...
The above will invoke Lambda synchronously; however, you can also invoke asynchronously using Java Futures or callbacks.
Invocation with Access Key and Secret Key The above sample will utilize the DefaultAWSCredentialsProviderChain to utilize environment variables, EC2 Instance Profile, etc. when invoking the function. While this is often best practice, there are also scenarios when you may need to provide access and secret keys. To do so, we only need to make a small change to the code above:
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
// (1a) Instantiate credentials
BasicAWSCredentials credentials = new
BasicAWSCredentials("myAccessKey", "mySecretKey");
// (2) Modify to leverage credentials
AWSLambdaClientBuilder builder = AWSLambdaClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials));
.withRegion(region);
6 Comments
Ashu
Thank you so much so this will invokelambda from my code itself I am sending a screenshot of my code actually I am new to this and it will be of great help if I can solve this thank you
Ashu
yes hope so and can you please guide where o I add this in my code it is a normal pub/sub aws sdk java sample code.
Yoko Zunna
It is just how you handle the outcome from InvokeResult result = client.invoke(req);
Ashu
Region, AWSLambda and all these are classes that we need to define right because I am not able to import it
Michel Feinstein
Give credit when credit is due: medium.com/@joshua.a.kahn/…
|