How to Invoke Lambda from Local using Selenium Java Framework

Author: neptune | 24th-Apr-2023
#AWS

AWS Lambda is a serverless computing service that allows you to run your code without provisioning or managing servers. It is a cost-effective and flexible way to run your code in the cloud. You can invoke your Lambda functions using various methods such as the AWS Management Console, AWS CLI, SDKs, and APIs. In this article, we will discuss how to invoke a Lambda function from local using the Selenium Java framework.


Prerequisites:

- AWS Account

- AWS CLI installed on your local machine(Optional)

- Java Development Kit (JDK) installed on your local machine

- Selenium Java framework setup on your local machine


Steps to create a Lambda function in AWS:

1. Log in to the AWS Management Console and navigate to the Lambda service.

2. Click on the "Create function" button.

3. Select "Author from scratch" as the function blueprint.

4. Give your function a name and select "Java 8" as the runtime.

5. Under "Permissions", select "Create a new role with basic Lambda permissions".

6. Click on "Create function".


Add dependencies in existing Selenium Java Framework:

1. Open your Selenium Java project in your preferred IDE.

2. Add the following dependencies to your pom.xml file:



<dependency>

    <groupId>com.amazonaws</groupId>

    <artifactId>aws-java-sdk-lambda</artifactId>

    <version>1.11.1003</version>

</dependency>

<dependency>

    <groupId>com.amazonaws</groupId>

    <artifactId>aws-java-sdk-core</artifactId>

    <version>1.11.1003</version>

</dependency>


Create a class "InvokeLambdaFromLocal":

1. Create a new Java class called "InvokeLambdaFromLocal".

2. Add the following code to the class:


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;

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

import com.fasterxml.jackson.databind.ObjectMapper;


public class InvokeLambdaFromLocal {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    private static final String FUNCTION_NAME = "your-function-name";

    private static final String REGION = "your-region";

    private static final AWSLambda LAMBDA_CLIENT = AWSLambdaClientBuilder.standard()

            .withRegion(REGION)

            .build();


    public static void main(String[] args) throws Exception {

        APIGatewayProxyRequestEvent requestEvent = new APIGatewayProxyRequestEvent();

        // set request event properties

        requestEvent.setHttpMethod("GET");

        requestEvent.setBody("request body goes here");

        // invoke Lambda function

        InvokeRequest invokeRequest = new InvokeRequest()

                .withFunctionName(FUNCTION_NAME)

                .withPayload(objectMapper.writeValueAsString(requestEvent));

        InvokeResult invokeResult = LAMBDA_CLIENT.invoke(invokeRequest);

        // parse response from Lambda function

        String responseJson = new String(invokeResult.getPayload().array(), "UTF-8");

        APIGatewayProxyResponseEvent responseEvent = objectMapper.readValue(responseJson, APIGatewayProxyResponseEvent.class);

        // handle response

        System.out.println("Response status code: " + responseEvent.getStatusCode());

        System.out.println("Response body: " + responseEvent.getBody());

    }

}



Steps to grab Access key, Access Token from your AWS Account:

1. Log in to your AWS Management Console.

2. Click on your username in the top right corner of the page.

3. Select "My Security Credentials" from the dropdown menu.

4. Under the "Access keys" section, click on "Create access key" if you don't have any access keys yet. Otherwise, you'll see your existing access keys here.

5. Click on "Download .csv" to download a CSV file with your access key and secret key.


Update your access key, token and region:

1. Open the "InvokeLambdaFromLocal" class in your IDE.

2. Replace "your-lambda-function-name" with the name of your Lambda function.

3. Replace "your-access-key" and "your-secret-key" with the access key and secret key that you obtained from your AWS account.

4. Replace "your-region" with the region in which your Lambda function is located.


Boom! Now invoke the Lambda function:

1. Run the "InvokeLambdaFromLocal" class.

2. You should see the response from your Lambda function in the console.


Conclusion

In this article, we have discussed how to invoke a Lambda function from local using the Selenium Java framework. We have gone through the prerequisites, steps to create a Lambda function in AWS, adding dependencies in the Selenium Java framework, creating a class to invoke the Lambda function, and updating the access key, token, and region. Invoking a Lambda function from local can be a useful way to test your Lambda function without deploying it to the cloud. With the steps outlined in this article, you should be able to easily invoke your Lambda functions from your local machine.






Related Blogs
Generative AI Made Easy: Explore Top 7 AWS Courses
Author: neptune | 05th-Aug-2023
#AI #AWS #Certifications
These top 7 Generative AI courses by AWS offer a pathway to explore and master the fascinating world of Generative AI...

Mastering AWS Step Functions: A Visual Workflow Solution
Author: neptune | 07th-Aug-2023
#AWS
AWS Step Functions: Simplify, visualize, and automate serverless workflows with seamless AWS service integration and built-in error handling for reliable applications...

Roadmap to AWS Certified Solutions Architect – Associate (SAA-C03)
Author: neptune | 04th-Jul-2023
#AWS #Certifications
As technology continues to evolve, the demand for skilled professionals in cloud computing and architecture has seen significant growth...

A Step-by-Step Guide to Triggering Lambda with S3 Bucket
Author: neptune | 25th-Apr-2023
#AWS
This article provides a step-by-step guide to trigger AWS Lambda with S3 bucket uploads using Python as the runtime...

Roadmap to AWS Cloud Architect Certification
Author: neptune | 28th-Mar-2023
#AWS
The demand for cloud computing has been rapidly growing in recent years, and as a result, there is a high demand for professionals with AWS cloud computing skills...

AWS Step Functions Express vs. Standard Workflows: Which One Fits Your Use Case?
Author: neptune | 20th-Sep-2023
#AWS
AWS Step Functions is a fully managed service that allows you to build serverless workflows to coordinate AWS services and applications...

AWS Certified Developer – Associate | Roadmap
Author: neptune | 26th-Jul-2023
#AWS #Certifications
The AWS Certified Developer – Associate (DVA-C01) exam is intended for individuals who perform a developer role...

View More