Can Logstash Base64 decode then parse json from a cloudfront log?

Hello, we are using AWS's image resizer Serverless Image Handler | Implementations | AWS Solutions

To get an image, you need to base64 encode a json object such as


which becomes eyJidWNrZXQiOiJteS1zMy1idWNrZXQiLCJrZXkiOiJwYXRoL3RvL3lvdXIvaW1hZ2UuanBnIiwiZWRpdHMiOnsicmVzaXplIjp7IndpZHRoIjoyMDAsImZpdCI6ImNvdmVyIn19fQ==

So you would end up with a url of This url is what ends up in the cloudfront logs that are delivered to the s3 bucket.

What I'd like to do is have Logstash grab these cloudfront logs, decode url, and grab the "real" url path/to/your/image.jpg for Elasticsearch

You can use

    grok { match => { "url" => "/(?<[@metadata][uri]>[^/]*)$" } }
    ruby {
        code => '
            uri = event.get("[@metadata][uri]")
            if uri
                event.set("[@metadata][json]", Base64.decode64(uri))
    json { source => "[@metadata][json]" target => "[@metadata][stuff]" }
    mutate { add_field => { "someField" => "%{[@metadata][stuff][key]}" } }

to process

       "url" => ""

and extract

 "someField" => "path/to/your/image.jpg",

thank you, worked great!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.