# Automate AWS Athena Partitions for Organization CloudTrail Logs and VPC Flow Logs

DevFeed: [Automate AWS Athena Partitions for Organization CloudTrail Logs and VPC Flow Logs](<https://devfeed.tech/articles/automate-aws-athena-partitions-for-organization-cloudtrail-logs-and-vpc-flow-logs-23687.md>)

Original publisher: [Read original article](<https://medium.com/betclic-tech/automate-aws-athena-partitions-for-organization-cloudtrail-logs-and-vpc-flow-logs-2f322291c897?source=rss----7e406d68d94b---4>)

Author: Aurelien Cerveaux

Published: 2022-09-15T06:39:58Z

Content type: article

Language: en

Sources: [Betclic Tech](<https://devfeed.tech/sources/betclic-tech-medium.md>)

Topics: [VPC Flow Logs](<https://devfeed.tech/topics/vpc-flow-logs.md>), [AWS CloudTrail](<https://devfeed.tech/topics/aws-cloudtrail.md>), [table-partitioning](<https://devfeed.tech/topics/table-partitioning.md>), [Amazon DynamoDB](<https://devfeed.tech/topics/amazon-dynamodb.md>), [AWS CloudFormation](<https://devfeed.tech/topics/aws-cloudformation.md>), [AWS Glue](<https://devfeed.tech/topics/aws-glue.md>), [Amazon EventBridge](<https://devfeed.tech/topics/amazon-eventbridge.md>)

Tags: [amazon-s3](<https://devfeed.tech/tags/amazon-s3.md>), [athena](<https://devfeed.tech/tags/athena.md>), [automate](<https://devfeed.tech/tags/automate.md>), [aws](<https://devfeed.tech/tags/aws.md>), [aws-athena](<https://devfeed.tech/tags/aws-athena.md>), [aws-glue](<https://devfeed.tech/tags/aws-glue.md>), [cloud](<https://devfeed.tech/tags/cloud.md>), [cloudformation](<https://devfeed.tech/tags/cloudformation.md>), [cloudtrail](<https://devfeed.tech/tags/cloudtrail.md>), [code](<https://devfeed.tech/tags/code.md>), [cost](<https://devfeed.tech/tags/cost.md>), [daily](<https://devfeed.tech/tags/daily.md>), [dynamodb](<https://devfeed.tech/tags/dynamodb.md>), [eventbridge](<https://devfeed.tech/tags/eventbridge.md>), [lambda](<https://devfeed.tech/tags/lambda.md>), [organization](<https://devfeed.tech/tags/organization.md>), [partitions](<https://devfeed.tech/tags/partitions.md>), [query](<https://devfeed.tech/tags/query.md>), [s3](<https://devfeed.tech/tags/s3.md>), [sam](<https://devfeed.tech/tags/sam.md>), [vpc-flow-logs](<https://devfeed.tech/tags/vpc-flow-logs.md>)

## AI overview

The excerpt describes a daily process for creating Athena partitions to reduce scanned data and query costs for organization-wide CloudTrail and VPC Flow Logs stored in S3. The proposed deployment uses CloudFormation and SAM: a shared Lambda creates partitions for different log types, while DynamoDB stores partition names so the system can check whether a partition already exists without querying Athena. Each log type has its own Glue table with an appropriate structure and an EventBridge rule that triggers the Lambda daily with specific input. The excerpt ends while introducing the DynamoDB partition structure, so it does not show the implementation details or the rest of the workflow.

## Source excerpt

This article's goal is to explain how to query efficiently, and at low cost with AWS Athena, AWS CloudTrail logs, or VPC Flow logs by automating the creation of Athena partitions daily. AWS CloudTrail AWS CloudTrail is a service that records all API Calls and Events for AWS Accounts, including details on calls made to your AWS Services and the Console as well. Activated at the Organization level, CloudTrail logs help you in several cases: Simplified Compliance Summarizing Accesses Security Analysis and Troubleshooting Monitoring Users and Resources Activities Governance and Investigation CloudTrail Logs are delivered in an S3 Bucket in JSON format through many small files that make these difficult to operate and request. VPC Flow Logs VPC Flow Logs is a feature that allows you to capture traffic information going to and from your network interfaces in your VPC. It helps you to: Analyze and troubleshoot network issues Determine the direction of your traffic Monitor the traffic Like CloudTrail Logs, Flow Logs are published in an S3 Bucket for all accounts in the Organization. AWS Athena AWS Athena is a query service that permits you to analyze data directly in an Amazon S3. You can create tables in Athena which use AWS Glue Data Catalog and are based on an S3 location. To query efficiently and with less scanned data and optimize the cost, we need to create some partitions. To know more about Tables, Databases, and Catalog in Athena. Automate Partitions Creation In our case, we are using CloudTrail Logs and VPC Flow Logs to detail the solution and the source code. All is done in CloudFormation and using SAM to deploy the Lambda. Global Overview We have several parts to automate the creation of our partitions. A common part containing: The Lambda is used to create partitions concerning different log types A DynamoDB table used to store partition names A specific part for each log type contains: The Glue Table with the proper structure An EventBridge Rule to trigger the