2

I implemented AWS lambda method to learn what I can do with it. What I have now:

  • AWS lambda itself
  • API Gateway
  • Amazon CloudWatch Logs

In API Gateway configs I see next options:

Behind this lambda I have Java code, implementing com.amazonaws.services.lambda.runtime.RequestStreamHandler and a REST controller behind it, something like that:

@Path("/tester")
public class TestResource {

    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    private TestRepository testRepository;

    public void setTestRepository(TestRepository testRepository) {
        this.testRepository = testRepository;
    }

    @POST
    @Path("/{identifier}")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.WILDCARD)
    public Response store(@PathParam("identifier") String identifier, @QueryParam("hashcode") String hashcode) {
        try {
            this.testRepository.store(identifier, hashcode);
        } catch (RuntimeException ex) {
            LOG.error("Failed to store pair {}, {}", identifier, hashcode, ex);
            throw new InternalServerErrorException(ex);
        }
        return Response.noContent().build();
    }
}

Now I tried invoking this service through Postman:

Option 1 - Invoke what I saw in API Gateway:

Result: 404 Not Found, see the appropriate logs in AWS CloudWatch.

Option 2 - Invoke my method:

Result: 403 Forbidden, no logs in AWS CloudWatch.

{
    "message": "Missing Authentication Token"
}

I fully understand why Option 1 did not work, but what went wrong with Option 2? Could you please help me understanding what I missed?

2 Answers 2

2

This message occurs (most of the times) when you try to call URL that doesn't exist.

Please make sure you are calling http://api-gateway-name/stage-name/resource-name

Also, make sure you have deployed your API.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your hint, you was right that URL was wrong, but I did not understand why it was wrong before I dived into the AWS client's code. I'm going to paste my finding as a separate answer here.
1

The answer to my problem was simple and obvious (silly me). Endpoint configuration in API gateway must match the @Path configurations in Java code.

So if I define dummmy/test-store in gateway, should use @Path("/dummmy/test-store") instead of @Path("/tester").

Addressed this limitation and made my Lambda-based API lightweight by implementing the handler manually, without overhead of regular REST API frameworks. Even published my WIP "framework" on GitHub.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.