Larry Ludden - Enterprise Java Developer - AWS Cloud Architect

Back to home

Some useful AWS Java Lambda development hints

As I do more development using AWS Lambda and Java, I come across various tasks that are difficult, time consuming or repetitive that I try and find solutions for. A lot of these are solved either in similar ways by others, but I'm going to note down a few of the ones that have helped me the most along with any relevant links that I found to help. 

Changing Log Levels ( AWS Lambda, Java, SLF4J, Logback )

There are probably other ways to do this, but the easiest way for me was to add a bit of code to look for an environment variable, and then set the lambda's main logger ( not the ROOT logger ) to that value. It still requires a "re-deploy" of the Lambda, but all you need to worry about is changing the environment variable and AWS will take care of the rest. 

In my case I'm using sl4f and logback as my logging libraries, so I added this Java method to the project:

  public static void updateLogLevel( Logger logger, String loggerName, String logLevel ) {
    if( logLevel != null ) {
      LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
      ch.qos.logback.classic.Logger definedLogger = context.getLogger( loggerName );
      if( definedLogger != null ) {
        definedLogger.setLevel( Level.toLevel( logLevel ) );
        logger.debug( "  Setting logLevel to {}", logLevel );
      }
    }

( Additional error checking wouldn't hurt for a more robust implementation )

And then in the beginning of my Lambda I make this call:

  LambdaUtil.updateLogLevel( logger, "com.jklfamily.monitor", System.getenv( "LOG_LEVEL" ) );

This will affect the logger that I have defined in my logback.xml file ( with a default level ):

  <logger name="com.jklfamily.monitor" level="DEBUG" />

And will set the logger to the passed in value from the environment variable. Which can be set in the Lambda configuration like this:

Screenshot 2025 06 04 104957

So far, fairly simple and works quite well. I've considered other options such as putting the value in an AWS System Parameter and reading it from there, which would allow truly on-the-fly changes, but so far I haven't needed that flexibility so I haven't added the additional complexity. Always a balance. 

Links:

https://mincong.io/en/change-log-level-at-runtime-in-logback/
https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-example

Articles