Menu
AWS Toolkit for Eclipse
User Guide

Tutorial: How to Create, Upload and Invoke an AWS Lambda Function

This tutorial will guide you through the process of a typical AWS Lambda workflow and provide you with first-hand experience in using Lambda with the AWS Toolkit for Eclipse.

Important

The tutorial assumes that you have an AWS account, have already installed the AWS Toolkit for Eclipse and that you understand the basic concepts and features of Lambda. If you are unfamiliar with Lambda, you can find out more at the Lambda home page and in the AWS Lambda Developer Guide.

Create a Lambda handler class

First, you will implement the code as a method in a handler class. The AWS Toolkit for Eclipse provides a new project wizard to help you create a new handler class.

To create an Lambda handler class

  1. On the Eclipse toolbar, open the drop-down Amazon Web Services menu (identified by the AWS icon) and select New AWS Lambda Java project...

  2. Add your Java project name, package name, and class name in the associated input boxes. You can choose any valid names that you want. This tutorial will use the following sample values:

    • Project name: HelloLambda

    • Package name: example

    • Class name: Hello

    While you type, the code in the Source preview will change to reflect the changes you make in the dialog.

  3. For Input Type, choose Custom. For information about each of the available input types, see New AWS Lambda Java Project Dialog.

  4. The second Input Type field is name of the actual Java type that will be returned, which must be a Java class (not a primitive type such as float, int or boolean). It defaults to Object. Change this value to String.

  5. Change the value of Output Type to String, as well.

  6. Verify that your entries look like the following screenshot (modify them if they are not), and then click Finish.

  7. Once you click Finish, your project's directory and source files will be generated in your eclipse workspace, and a new web browser window will appear, displaying README.html (which was created for you in your project's root directory). README.html provides instructions to help guide you through the next steps of implementing, testing, uploading and invoking your new Lambda function. Read through it to gain some familiarity with the next steps that will be covered here.

Next, we'll implement the function in the HelloLambda Java project that was just created for you in Eclipse.

Implement the handler method

The Create New Project dialog creates a skeleton project for you, but it's up to you to fill in the code that will be run when your Lambda function is invoked (in this case, by a custom event that sends a String to your function, as you specified when setting your method's input parameter.

To implement your Lambda handler method

  1. Using Eclipse's Project Explorer, open Hello.java in the HelloLambda project. It will contain code similar to the following:

    Copy
    package example; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; public class Hello implements RequestHandler<String, String> { @Override public String handleRequest(String input, Context context) { context.getLogger().log("Input: " + input); // TODO: implement your handler return null; } }
  2. Replace the contents of the handleRequest function with the following code:

    Copy
    @Override public String handleRequest(String input, Context context) { String output = "Hello, " + input + "!"; return output; }

Your Hello.java file should now contain:

Copy
package example; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; public class Hello implements RequestHandler<String, String> { @Override public String handleRequest(String input, Context context) { String output = "Hello, " + input + "!"; return output; } }

Allow Lambda to assume an IAM role

In order for Lambda to access your Lambda function, you will need to create an IAM role that gives it access to your AWS resources. The easiest way to do this is with the AWS Management Console.

To create an IAM role for Lambda

  1. Sign in to the AWS Management Console.

  2. Open the IAM console.

  3. Select Roles on the sidebar, then Create New Role.

  4. Add a name for your role, such as hello-lambda-role, and click Next Step.

  5. On the Select Role Type page, select AWS Lambda within the AWS Service Roles list.

  6. For Attach Policy, check AmazonS3FullAccess, which allows Lambda to access your Amazon S3 resources, and then click Next Step to continue.

    Note

    Amazon S3 is required because Lambda will upload your code to an Amazon S3 bucket when you deploy and run your Lambda function. You can use a bucket that you create (this is covered in the next section) or use an existing bucket.

  7. Review your role parameters, then click Create Role to finish creating the IAM role.

Create an Amazon S3 bucket for your Lambda code

AWS Lambda requires an Amazon S3 bucket to store your Java project when you upload it. You can either use a bucket that already exists in the AWS region in which you'll run your code, or you can create a new one specifically for use by Lambda (recommended).

To create an Amazon S3 bucket for use with Lambda

  1. Log in to AWS and go to the S3 console.

  2. Click Create Bucket.

  3. Enter a bucket name and select an AWS region for your bucket. This region should be the same one in which you intend to run your Lambda function. For a list of regions supported by Lambda see the Regions and Endpoints topic in the Amazon Web Services General Reference.

  4. Click Create to finish creating your bucket.

Upload the code

Next, we'll upload your code to AWS Lambda in preparation for invoking it using the AWS Management Console.

To upload your function to Lambda

  1. Right-click in your code window and select AWS Lambda, then Upload function to AWS Lambda....

  2. In the Select Target Lambda Function dialog that appears, select the AWS region to use. This should be the same region that you chose for your Amazon S3 bucket.

  3. Select Create a new Lambda function and enter the name of your function (such as HelloFunction).

  4. Click Next to proceed to Function Configuration.

  5. Enter a description for your target Lambda function. You can leave the rest of the options as they are; the AWS Toolkit for Eclipse chooses default values for you.

    For more information about the available options, see Upload AWS Lambda Function Dialog.

  6. Click Finish to upload your Lambda function to AWS.

If the upload succeeds, you will see the Lambda function name that you chose appear next to your Eclipse project name in the Project Explorer view:

If you don't see this happen, you should open Eclipse's Error Log view. Lambda will write information about failures to upload or run your function to the error log for further debugging.

Invoke the Lambda function

You can now invoke the function on AWS Lambda.

To invoke your Lambda function

  1. Right-click in your code window and select AWS Lambda, then Run on AWS Lambda.

  2. In the input box, type a valid JSON string, such as "AWS Lambda".

    Note

    You can add new JSON input files in your project, and they will show up in this dialog as long as the file name ends with ".json". You can use this feature to provide standard input files for your Lambda functions.

  3. Click Invoke and it will send your input data to your Lambda function. If you have set up everything correctly, you should see the return value of your function printed out in the Eclipse Console view (which will automatically appear if it isn't already shown).

Congratulations, you've just run your first Lambda function directly from the Eclipse IDE!

Where to go from here

Now that you've uploaded and deployed your function, try changing the code and re-running the function. Lambda will automatically re-upload and invoke the function for you, and print output to the console.

For more information about each of the screens that were covered in this tutorial, as well as a full description of each option, see the AWS Lambda Interface Reference.

For more information about Lambda itself, and about writing Java code for Lambda, see Authoring Lambda Functions in Java in the AWS Lambda Developer Guide.