aws / serverless-java-container

A Java wrapper to run Spring, Spring Boot, Jersey, and other apps inside AWS Lambda.
https://aws.amazon.com/serverless/
Apache License 2.0
1.5k stars 560 forks source link

Need Assistance #371

Closed vardans6 closed 4 years ago

vardans6 commented 4 years ago

To help us debug your issue fill in the basic information below using the options provided

Serverless Java Container version: eg. 1.5

Implementations: Jersey / Spring / Spring Boot / Spring Boot 2 / Spark

Framework version: eg SpringBoot 2.2.6.RELEASE

Frontend service: REST API / HTTP API / ALB

Deployment method: eg SAM, Serverless Framework, Console

Scenario

Describe what you are trying to accomplish

Expected behavior

running serverless

Actual behavior

I have attached a link to the repository, i want to know if this microservice can run serverless or not? if it can then what's the procedure, will be helpful if uh help me in doing this

Steps to reproduce

https://github.com/vardans6/GTSUserProfileService.git

Full log output

Paste the full log output from the Lambda function's CloudWatch logs

logs
sapessi commented 4 years ago

You should be able to run this just by including this library as a dependency and creating the handler class. You can follow the guide in the wiki.

Few notes:

vardans6 commented 4 years ago

can u do this for me , for this one service i will go through it, gone through wiki but not understanding

vardans6 commented 4 years ago

my lambdahandler

package com.gts.training;

import com.amazonaws.serverless.exceptions.ContainerInitializationException; import com.amazonaws.serverless.proxy.internal.testutils.Timer; import com.amazonaws.serverless.proxy.model.AwsProxyRequest; import com.amazonaws.serverless.proxy.model.AwsProxyResponse; import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler; import com.amazonaws.serverless.sample.springboot2.filter.CognitoIdentityFilter; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

import javax.servlet.DispatcherType; import javax.servlet.FilterRegistration;

import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.EnumSet;

public class StreamLambdaHandler implements RequestStreamHandler { private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler; static { try { handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);

        // For applications that take longer than 10 seconds to start, use the async builder:
        // long startTime = Instant.now().toEpochMilli();
        // handler = new SpringBootProxyHandlerBuilder()
        //                    .defaultProxy()
        //                    .asyncInit(startTime)
        //                    .springBootApplication(Application.class)
        //                    .buildAndInitialize();

        // we use the onStartup method of the handler to register our custom filter
        handler.onStartup(servletContext -> {
            FilterRegistration.Dynamic registration = servletContext.addFilter("CognitoIdentityFilter", CognitoIdentityFilter.class);
            registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
        });
    } catch (ContainerInitializationException e) {
        // if we fail here. We re-throw the exception to force another cold start
        e.printStackTrace();
        throw new RuntimeException("Could not initialize Spring Boot application", e);
    }
}

public StreamLambdaHandler() {
    // we enable the timer for debugging. This SHOULD NOT be enabled in production.
    Timer.enable();
}

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
        throws IOException {
    handler.proxyStream(inputStream, outputStream, context);
}

}

vardans6 commented 4 years ago

added all dependencies in pom xml file which was required but its not importing successfully still

sapessi commented 4 years ago

We'll run some tests as soon as we can. I'll keep this open in case someone else in the community can assist

vardans6 commented 4 years ago

@sapessi

sapessi commented 4 years ago

Sent a PR to your repository. I have tested this in my account and it works. Adding comments from the PR here too:

Please note that I've increased initialization time to 30 seconds to allow for a large Spring application. You'll also have to allocate quite a bit of memory to the function to handle the initialization load - likely > 1,500Mb. See this re:Invent session about Java functions optimization. My recommendation would be to explore the new GraalVM integration with Spring (See Twitter thread).

Also make sure to configure your Lambda function is configured to connect to the right VPC and its security group has access to the MySQL database.

vardans6 commented 4 years ago
START RequestId: d5e235ee-e850-4468-b5ed-ce0c29fcb61f Version: $LATEST

2020-08-14T14:40:10.365+05:30
09:10:09.982 [main] INFO com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Starting Lambda Container Handler

2020-08-14T14:40:10.366+05:30
09:10:10.365 [Thread-0] INFO com.amazonaws.serverless.proxy.AsyncInitializationWrapper - Starting async initializer

2020-08-14T14:40:11.160+05:30
09:10:10.366 [main] INFO com.amazonaws.serverless.proxy.AsyncInitializationWrapper - Async initialization will wait for 8335ms

2020-08-14T14:40:11.160+05:30
. ____ _ __ _ _

2020-08-14T14:40:11.160+05:30
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

2020-08-14T14:40:11.160+05:30
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

2020-08-14T14:40:11.160+05:30
\\/ ___)| |_)| | | | | || (_| | ) ) ) )

2020-08-14T14:40:11.160+05:30
' |____| .__|_| |_|_| |_\__, | / / / /

2020-08-14T14:40:11.178+05:30
=========|_|==============|___/=/_/_/_/

2020-08-14T14:40:11.661+05:30
:: Spring Boot :: (v2.1.15.RELEASE)

2020-08-14T14:40:11.662+05:30
2020-08-14 09:10:11.660 INFO 1 --- [ Thread-0] o.s.boot.SpringApplication : Starting application on 169.254.2.229 with PID 1 (started by sbx_user1051 in /)

2020-08-14T14:40:15.817+05:30
2020-08-14 09:10:11.662 INFO 1 --- [ Thread-0] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default

2020-08-14T14:40:16.218+05:30
2020-08-14 09:10:15.801 INFO 1 --- [ Thread-0] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2020-08-14T14:40:18.137+05:30
2020-08-14 09:10:16.218 INFO 1 --- [ Thread-0] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 374ms. Found 3 JPA repository interfaces.

2020-08-14T14:40:18.184+05:30
2020-08-14 09:10:18.137 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@d2cfc0' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:40:18.253+05:30
2020-08-14 09:10:18.183 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:40:18.712+05:30
2020-08-14 09:10:18.253 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$c023c58b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:40:18.712+05:30
2020-08-14 09:10:18.712 INFO 1 --- [ main] c.a.s.proxy.AsyncInitializationWrapper : Initialization took longer than 10000, setting new CountDownLatch and continuing in event handler

2020-08-14T14:40:18.736+05:30
2020-08-14 09:10:18.736 INFO 1 --- [ Thread-0] c.a.s.p.i.servlet.AwsServletContext : Initializing Spring embedded WebApplicationContext

2020-08-14T14:40:18.812+05:30
2020-08-14 09:10:18.812 INFO 1 --- [ Thread-0] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6959 ms

2020-08-14T14:40:23.633+05:30
2020-08-14 09:10:23.633 INFO 1 --- [ Thread-0] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...

2020-08-14T14:40:48.740+05:30
END RequestId: d5e235ee-e850-4468-b5ed-ce0c29fcb61f

2020-08-14T14:40:48.740+05:30
REPORT RequestId: d5e235ee-e850-4468-b5ed-ce0c29fcb61f Duration: 30023.42 ms Billed Duration: 30000 ms Memory Size: 512 MB Max Memory Used: 226 MB Init Duration: 9547.94 ms

2020-08-14T14:40:48.740+05:30
2020-08-14T09:10:48.740Z d5e235ee-e850-4468-b5ed-ce0c29fcb61f Task timed out after 30.02 seconds

2020-08-14T14:40:49.759+05:30
09:10:49.754 [main] INFO com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Starting Lambda Container Handler

2020-08-14T14:40:50.136+05:30
09:10:50.135 [Thread-0] INFO com.amazonaws.serverless.proxy.AsyncInitializationWrapper - Starting async initializer

2020-08-14T14:40:50.136+05:30
09:10:50.136 [main] INFO com.amazonaws.serverless.proxy.AsyncInitializationWrapper - Async initialization will wait for 8598ms

2020-08-14T14:40:50.939+05:30
. ____ _ __ _ _

2020-08-14T14:40:50.939+05:30
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

2020-08-14T14:40:50.939+05:30
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

2020-08-14T14:40:50.939+05:30
\\/ ___)| |_)| | | | | || (_| | ) ) ) )

2020-08-14T14:40:50.939+05:30
' |____| .__|_| |_|_| |_\__, | / / / /

2020-08-14T14:40:50.939+05:30
=========|_|==============|___/=/_/_/_/

2020-08-14T14:40:50.940+05:30
:: Spring Boot :: (v2.1.15.RELEASE)

2020-08-14T14:40:51.380+05:30
2020-08-14 09:10:51.379 INFO 1 --- [ Thread-0] o.s.boot.SpringApplication : Starting application on 169.254.2.229 with PID 1 (started by sbx_user1051 in /)

2020-08-14T14:40:51.393+05:30
2020-08-14 09:10:51.393 INFO 1 --- [ Thread-0] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default

2020-08-14T14:40:55.094+05:30
2020-08-14 09:10:55.093 INFO 1 --- [ Thread-0] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2020-08-14T14:40:55.474+05:30
2020-08-14 09:10:55.473 INFO 1 --- [ Thread-0] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 371ms. Found 3 JPA repository interfaces.

2020-08-14T14:40:57.334+05:30
2020-08-14 09:10:57.334 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@69dfad25' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:40:57.357+05:30
2020-08-14 09:10:57.357 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:40:57.420+05:30
2020-08-14 09:10:57.420 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$ac268ee] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:40:57.915+05:30
2020-08-14 09:10:57.914 INFO 1 --- [ Thread-0] c.a.s.p.i.servlet.AwsServletContext : Initializing Spring embedded WebApplicationContext

2020-08-14T14:40:57.915+05:30
2020-08-14 09:10:57.915 INFO 1 --- [ Thread-0] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6437 ms

2020-08-14T14:40:58.735+05:30
2020-08-14 09:10:58.734 INFO 1 --- [ main] c.a.s.proxy.AsyncInitializationWrapper : Initialization took longer than 10000, setting new CountDownLatch and continuing in event handler

2020-08-14T14:42:54.607+05:30
START RequestId: a562173c-ff1d-4672-8458-804abc4df009 Version: $LATEST

2020-08-14T14:42:57.277+05:30
2020-08-14 09:12:57.277 INFO 1 --- [ Thread-0] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...

2020-08-14T14:43:24.643+05:30
END RequestId: a562173c-ff1d-4672-8458-804abc4df009

2020-08-14T14:43:24.643+05:30
REPORT RequestId: a562173c-ff1d-4672-8458-804abc4df009 Duration: 30030.15 ms Billed Duration: 30000 ms Memory Size: 512 MB Max Memory Used: 176 MB

2020-08-14T14:43:24.643+05:30
2020-08-14T09:13:24.643Z a562173c-ff1d-4672-8458-804abc4df009 Task timed out after 30.03 seconds

2020-08-14T14:43:25.916+05:30
09:13:25.898 [main] INFO com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Starting Lambda Container Handler

2020-08-14T14:43:26.292+05:30
09:13:26.292 [Thread-0] INFO com.amazonaws.serverless.proxy.AsyncInitializationWrapper - Starting async initializer

2020-08-14T14:43:26.292+05:30
09:13:26.292 [main] INFO com.amazonaws.serverless.proxy.AsyncInitializationWrapper - Async initialization will wait for 8360ms

2020-08-14T14:43:27.124+05:30
. ____ _ __ _ _

2020-08-14T14:43:27.124+05:30
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

2020-08-14T14:43:27.141+05:30
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

2020-08-14T14:43:27.141+05:30
\\/ ___)| |_)| | | | | || (_| | ) ) ) )

2020-08-14T14:43:27.141+05:30
' |____| .__|_| |_|_| |_\__, | / / / /

2020-08-14T14:43:27.141+05:30
=========|_|==============|___/=/_/_/_/

2020-08-14T14:43:27.142+05:30
:: Spring Boot ::       (v2.1.15.RELEASE)

2020-08-14T14:43:27.574+05:30
2020-08-14 09:13:27.564 INFO 1 --- [ Thread-0] o.s.boot.SpringApplication : Starting application on 169.254.2.229 with PID 1 (started by sbx_user1051 in /)

2020-08-14T14:43:27.575+05:30
2020-08-14 09:13:27.575 INFO 1 --- [ Thread-0] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default

2020-08-14T14:43:31.382+05:30
2020-08-14 09:13:31.365 INFO 1 --- [ Thread-0] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2020-08-14T14:43:31.799+05:30
2020-08-14 09:13:31.799 INFO 1 --- [ Thread-0] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 379ms. Found 3 JPA repository interfaces.

2020-08-14T14:43:33.777+05:30
2020-08-14 09:13:33.777 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@4897976c' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:43:33.817+05:30
2020-08-14 09:13:33.817 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:43:33.854+05:30
2020-08-14 09:13:33.853 INFO 1 --- [ Thread-0] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$c2b2afca] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2020-08-14T14:43:34.137+05:30
2020-08-14 09:13:34.137 INFO 1 --- [ Thread-0] c.a.s.p.i.servlet.AwsServletContext : Initializing Spring embedded WebApplicationContext

2020-08-14T14:43:34.137+05:30
2020-08-14 09:13:34.137 INFO 1 --- [ Thread-0] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6492 ms

2020-08-14T14:43:34.654+05:30
2020-08-14 09:13:34.653 INFO 1 --- [ main] c.a.s.proxy.AsyncInitializationWrapper : Initializati
vardans6 commented 4 years ago

getting this output in cloudwatch logs

sapessi commented 4 years ago

What does that output tell you?

vardans6 commented 4 years ago

Service is not able to start, its crashing before that

sapessi commented 4 years ago

Why is it crashing? What does the log say?

vardans6 commented 4 years ago

Crashing bcoz of timeout

sapessi commented 4 years ago

Ok. Then I would do two things:

  1. Increase the timeout in the Lambda configuration
  2. I also see that you allocated 512Mb of memory to the function. If you look at my recommendation in the PR you'll see that I asked you to allocate more memory: "You'll also have to allocate quite a bit of memory to the function to handle the initialization load - likely > 1,500Mb." If you really want to use 512 you'll have to dig deeper into the GraalVM link I sent you
vardans6 commented 4 years ago

Ok, i am allocating 1500 mb and will update you if its working

sapessi commented 4 years ago

I had tested at 3Gb just to play it safe

vardans6 commented 4 years ago

@sapessi Issue has been resolved, Increased memory its working fine now.

Can it work with RDS Mysql database in another aws account open publicly?