2
votes

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

2 Answers

2
votes

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.

1
votes

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.