简体   繁体   中英

Unable to understand which Java SDK to use when developing AWS Lambda Function

When I was following the tutorial on deploying an AWS Lambda Function, I saw in it's sample that it uses an AWS Java SDK containing the interface Speechlet.

Here's a screenshot of the sample:

在此处输入图片说明

And the official documentation also states that I should use the Speechlet interface here: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/handling-requests-sent-by-alexa

But unfortunately, when I installed the AWS Toolkit for Eclipse, there is no such interface in its toolkit. I also downloaded the AWS Java SDK from its official site but no Speechlet interface could be found.

And so, I searched again for some tutorials on how to create a Lambda Function using java then I came up with this: http://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda-tutorial.html

This tutorial uses the Java SDK with no Speechlet interface! This really confuses me, because in its other documentation it states there clearly that it has a Speechlet interface and now it doesn't. I don't know how to continue this.

The minimum functionality your AWS Lambda function needs to support in Java is to have a class that implements a method with the following signature:

outputType handler-name(inputType input) {
   ...
}

If your signature looks something like this, no third-party libraries are necessary. You could have a Lambda handler that looks like this and it would work fine.

package LambdaExample;

public class Hello {
    public String lambdaHandler(String name) {
        return String.format("Hello %s!", name);
    }
}

In non-trivial usage, you'll likely see handler signatures that look something more like this:

outputType handler-name(inputType input, Context context) {
   ...
}

The Context object can be used to retrieve information about your Lambda handler, as well as a logger you can use to log information about your handler to Cloudwatch.

The Context type is defined in com.amazonaws.services.lambda.runtime.Context , which is provided by aws-lambda-java-core , part of a set of libraries that AWS publish for working with Lambda using Java/JVM languages. aws-lambda-java-core is separate from the AWS SDK for Java.

What you're running into seems to be a case of using very specific resources. Your image shows code for building a custom Alexa skill and deploying it to Lambda. If you are building an Alexa skill, then you will need to also include the alexa-skills-kit SDK , which is what provides the Speechlet interface you see. The Alexa Skills Kit SDK is also separate from the AWS SDK for Java.

Assuming your project uses Maven for project management/dependencies, adding the following to the <dependencies> section should enable you to work with Lambda and Alexa:

<dependency>
  <groupId>com.amazon.alexa</groupId>
  <artifactId>alexa-skills-kit</artifactId>
  <version>1.2</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-lambda-java-core</artifactId>
  <version>1.1.0</version>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-lambda-java-events</artifactId>
  <version>1.3.0</version>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-lambda-java-log4j</artifactId>
  <version>1.0.0</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM