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}")

Chris Williams
  • 32,215
  • 4
  • 30
  • 68
Maciej
  • 1,209
  • 4
  • 16
  • 26

1 Answers1

-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

Rhineb
  • 305
  • 3
  • 12
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). You can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/low-quality-posts/34687850) – jared Jul 15 '23 at 20:55
  • Please do **not** post links to other SO threads as answers; flag the question as a possible duplicate instead. – desertnaut Jul 17 '23 at 00:00
  • 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 – Rhineb Aug 23 '23 at 15:18