1

I've started using lambda power-tools for lambda. Currently my code looks like:

@event_source(data_class=SNSEvent)
@LOGGER.inject_lambda_context(log_event=True)
def handler(event: SNSEvent, context: LambdaContext) -> None:  # pylint: disable=W0613
    """Lambda function invoked by Image builder SNS topic, putting Image
    builder ami-id in parameter store.
    :param event: SNS message containing Image Builder build results
    :return:
    """
    LOGGER.debug(f"Event: {event}") //logging event
    for record in event.records:
        message = record.sns.message
        LOGGER.info(f"Message: {message}")
        process_sns_event(message)
        return None

In line with comment I want to log what actually is lambda getting at beginning. As for now in cloud watch I'm getting entries like: Event: <aws_lambda_powertools.utilities.data_classes.sns_event.SNSEvent object at 0x7f9bbd36a0> or Event: <generator object SNSEvent.records at 0x7facfb6510> after updating powertools version to latest.<aws_lambda_powertools.utilities.data_classes.event_bridge_event.EventBridgeEvent object at 0x7f8af526d0> for event bridge one.

I'm confused what should I do to log just event json. Can any one point me out what should I do? (I'm rather beginner as can seen).

edit: After update powertools to latest version I'm getting: Event: <generator object SNSEvent.records at 0x7facfb6510>

with logging line line change to: LOGGER.debug(f"Event: {event.records}")

3
  • Try print(json.dumps(event, default=str)) Commented Dec 9, 2022 at 6:53
  • You can also try to set the annotation to str. def(event: ...) -> str: Commented Dec 9, 2022 at 7:34
  • Not working as for above two comments Commented Dec 9, 2022 at 12:27

1 Answer 1

-1

I ended up on this post as I was having issues getting powertools to log in JSON format. If someone else ends up here having the same issue it is probably because there is a known issue that shadowJar (shaded jars) dont play nice with the log4j2 from powertools. An additional transformation during the build is required

Using Gradle

import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer

shadowJar{
    transform(Log4j2PluginsCacheFileTransformer)
}

Using Maven

<dependencies>
  ...
  <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-log4j2</artifactId>
    <version>1.5.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.17.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.17.1</version>
  </dependency>
  ....
</dependencies>

and add the following transformation

<plugins>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer
                    implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>com.github.edwgiz</groupId>
        <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
        <version>2.8.1</version>
      </dependency>
    </dependencies>
  </plugin>
  ...
</plugins>

Other related posts

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

2 Comments

Please do not post links to other SO threads as answers; flag the question as a possible duplicate instead.
I disagree with both of those feedbacks. None of the linked items are actual duplicates, they are related but not true dups. Were not supposed to copy/paste answers even if the answer is the same and the above guides you to the answer. Also why would we not link to other posts? Just bc the answer is the same does not mean the question is? I reviewed the better part of 20+ posts to figure out an actual answer to this logging weirdness and only linked the relevant posts to attribute the correct credit and help others having the same confusion. I don't see how flagging as a pos dup is useful

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.