Enable VPC Flow Logs
Central AWS Account: AWS account which you want to designate for storing VPC Flow Logs centrally. This account will also contain Athena DB, table and QuickSight Dashboard.
Additional Accounts: These are other accounts that you own and has VPCs that you wish to enable Flow Logs and have an ability to push it to Central AWS Account’s S3 bucket.
QuickSight: To manage VPC Flow Logs and QuickSight dashboard in central account please make sure you create resources for the central account in the region supported by QuickSight. Refer to this link to see supported regions.
VPC
If you already have VPC and other resources running your AWS account continue with next section “Enable VPC Flow Logs” otherwise click on below link to deploy VPC and a toy webapp into your account.
Click here for instructions how to deploy a VPC to your AWS account:
This step will create the VPC and all components using the example CloudFormation template.
- Download the latest version of the CloudFormation template here: vpc-alb-app-db.yaml
- Sign in to the AWS Management Console, select your preferred region, and open the CloudFormation console at https://console.aws.amazon.com/cloudformation/.
- Click Create Stack, then With new resources (standard).

- Click Upload a template file and then click Choose file.

- Choose the CloudFormation template you downloaded in step 1, return to the CloudFormation console page and click Next.
- Enter the following details:
- Stack name: The name of this stack. For this lab, use WebApp1-VPC and match the case.
- Parameters: Parameters may be left as defaults, you can find out more in the description for each.

- At the bottom of the page click Next.
- In this lab, we use tags, which are key-value pairs, that can help you identify your stacks. Enter Owner in the left column which is the key, and your email address in the right column which is the value. We will not use additional permissions or advanced options so click Next. For more information, see Setting AWS CloudFormation Stack Options.
- Review the information for the stack. When you’re satisfied with the configuration, at the bottom of the page check I acknowledge that AWS CloudFormation might create IAM resources with custom names then click Create stack.

- After a few minutes the final stack status should change from CREATE_IN_PROGRESS to CREATE_COMPLETE. You can click the refresh button to check on the current status.
You have now created the VPC stack (well actually CloudFormation did it for you).
Wait until the VPC CloudFormation stack status is CREATE_COMPLETE, then continue. This will take about four minutes.
- Download the CloudFormation template: staticwebapp.yaml
- You can right-click then choose Save link as; or you can right click and copy the link to use with
wget
Go to the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation and click Create Stack > With new resources

Leave Prepare template setting as-is
- For Template source select Upload a template file
- Click Choose file and supply the CloudFormation template you downloaded: staticwebapp.yaml

Click Next
For Stack name use CloudFormationLab
Parameters
For Configure stack options we recommend configuring tags, which are key-value pairs, that can help you identify your stacks and the resources they create. For example, enter Owner in the left column which is the key, and your email address in the right column which is the value. We will not use additional permissions or advanced options so click Next. For more information, see Setting AWS CloudFormation Stack Options.
For Review
- Review the contents of the page
- At the bottom of the page, select I acknowledge that AWS CloudFormation might create IAM resources with custom names
- Click Create stack

This will take you to the CloudFormation stack status page, showing the stack creation in progress.
- Click on the Events tab
- Scroll through the listing. It shows the activities performed by CloudFormation (newest events at top), such as starting to create a resource and then completing the resource creation.
- Any errors encountered during the creation of the stack will be listed in this tab.

When it shows status CREATE_COMPLETE, then you are finished with this step.
Enable VPC Flow Logs
QuickSight dashboard provided in this lab requires all the fields mentioned in the Introduction section are required. If you already have enabled VPC Flow logs with those fields (with CSV format, Hive partition enabled and delivered to S3) then you can skip this section and proceed to "Create Athena resources, Lambda function and CloudWatch rule" section to continue. If you do not have VPC flow logs enabled or existing VPC Flow logs does not have all the required fields then this section will help you in enabling vpc flow logs for existing VPC(s) in your account. Repeat all the steps from this section for each VPC in case you want to enable VPC Flow logs in respective account to visualize them in QuickSight dashboard under central account.
Click here for the instructions for enabling VPC Flow Logs in Parquet format
Use aws cli or AWS CloudShell to run below command. This command will create Flow Log in parquet file format with hive-compatible s3 prefixes
Navigate to CloudShell from AWS Console from the account where your VPC is located.
Note: Please make sure you have correct region selected.
Replace <VPC ID>
with VPC id from your account. You can find the VPC ID in console
Replace <S3 ARN>
with S3 buckets arn from central account. Please specify subfolder in case you are storing logs under it.
e.g. arn:aws:s3:::my-flow-log-bucket/my-custom-flow-logs/
aws ec2 create-flow-logs \
--resource-type VPC \
--resource-ids <VPC ID> \
--traffic-type ALL \
--log-destination-type s3 \
--log-destination <S3 ARN> \
--destination-options FileFormat=parquet,HiveCompatiblePartitions=True,PerHourPartition=false \
--log-format '${account-id} ${action} ${az-id} ${bytes} ${dstaddr} ${dstport} ${end} ${flow-direction} ${instance-id} ${interface-id} ${log-status} ${packets} ${pkt-dst-aws-service} ${pkt-dstaddr} ${pkt-src-aws-service} ${pkt-srcaddr} ${protocol} ${region} ${srcaddr} ${srcport} ${start} ${sublocation-id} ${sublocation-type} ${subnet-id} ${tcp-flags} ${traffic-path} ${type} ${version} ${vpc-id}'
Once you finish replacing ID, ARN paste the command in CloudShell and run it. You will see below result with FlowLogIds, if it is successful.

Click here for the instructions for enabling VPC Flow Logs in CSV format
Login to your central AWS account.
Run CloudFormation stack to enable VPC Flow Logs.
- Download CloudFormation Template:
Provide name for the stack e.g., “vpc-flow-logs-stack” and values for the stack parameters and then click Next
TrafficType (ACCEPT, REJECT, ALL): Type of traffic you wish to record
- ACCEPT — The recorded traffic was permitted by the security groups and network ACLs.
- REJECT — The recorded traffic was not permitted by the security groups or network ACLs.
- ALL - The recorded traffic that was permitted (ACCEPT) and was not permitted (REJECT) by the security groups or network ACLs.
VpcFlowLogsBucketName (Optional): S3 bucket name where VPC flow logs will be stored.
If you specify the bucket name then it is assumed that the bucket already exists. If you want to centralize the storage of the logs, then create the bucket before and specify the bucket name here. If you are enabling VPC Flow Logs in additional account then please make sure to modify S3 bucket’s policy from the central account to grant access to additional account and provide the name of the central bucket to this parameter.
If you leave it blank CloudFormation template will create a bucket for you.
Note:
VpcFlowLogsBucketName - This bucket will be used to gather vpc flow logs for all of your vpcs from one or more accounts. So please make sure this is the central account where you want your VPC flow logs to be collected and QuickSight dashboard to be hosted.
VpcFlowLogsFilePrefix (Optional): VPC Flow logfile prefix in S3 bucket. See bold text in below example
e.g., bucket_name/vpc-flow-logs/AWSLogs/aws_account_id/vpcflowlogs/region/year/month/day/
VpcId: You can find the VPC ID in console

In Configure stack options page, add below tags and click on Next
- Name=VPCFlowLogs-CFN
- Purpose=WALabVPCFlowLogs

- On Review screen verify the inputs you have provided

Last click on Create stack

As shown below you will see progress of the stack creation under Events tab. Please wait for the stack to complete the execution. Once complete it will show the status CREATE_COMPLETE in green then proceed to the next step.

- To verify, navigate to VPC service, click on vpc link and then click on Flow Logs tab at the bottom part of the screen. You will see a line with flow logs you just created now.

Delete older VPC Flow Logs from S3 bucket (Optional)
We recommend you to create a life cycle policy to delete logs older than 90 days or lesser as per your requirement to save cost. All the steps from this section are required to execute one time in central account.
Click here to see the steps to Delete older VPC Flow Logs from S3 bucket
Login to central AWS account if you are not already in that account.
Navigate to S3 service from console
Click on S3 bucket where you stored VPC Flow Logs and click on Management link.

Click on Create lifecycle rule

Enter name for the rule e.g., 90_DAY_DELETE. You can edit the number of days based on your requirement.
- Check “This rule applies to all objects in the bucket”
- Check “I acknowledge that this rule will apply to all the objects in the bucket."
- Under “Lifecycle rule actions” check
- Expire current versions of objects
- Delete expired delete markers or incomplete multipart uploads

Enter 90 days for “Number of days after object creation” and 90 days for “Number of days after object becomes previous versions” and click on Create rule
NOTE: You can change the number of days based on your requirement.

Once you create the rule, it will appear on Lifecycle Configuration Page

Enable VPC Flow Logs in additional accounts and store it in central bucket (Optional)
Click here to see the steps to enable VPC Flow logs in additional accounts
Before you proceed to enable VPC Flow Logs in additional account, you need to grant permission to access S3 bucket(from central account) for target account’s VPC to push logs. Repeat all the steps from this section for each Account/VPC.
Please follow below steps to edit S3 bucket policy in central account:
Navigate to S3 service in the central account where you have S3 bucket and QuickSight Dashboard for VPC Flow Logs created in first step.
Click on the vpc flow logs bucket you created earlier and then navigate to permissions tab.
Scroll down to the bucket policy. You will see existing policy like below.
Click on Edit. In the policy json find resource attribute. Add another line under resource to grant permission to store flow logs from another account you wish to.
e.g., "arn:aws:s3:::wa-lab-vpc-flow-logs/vpc-flow-logs/AWSLogs/<New account number>/*"
Note: Above is an example only. Please change it according to your bucket name, prefix and <New account number>
with actual target account number. If Resource attribute of the policy is not an array, then you need to add any additional account in array format (as a comma separated list surrounded in square brackets)
Click on Save.
Log out from central account.
Repeat steps 1 thru 9 from section “Enable VPC Flow Logs” to enable logs in new account for desired VPC.
Log out from the additional account once you successfully enable flow logs for another VPC.
Log in to the central account.
Manually add first partition to the external table for the vpc in the new account:
Follow below instructions (1 thru 4) to make necessary changes in the code:
ALTER TABLE vpc_flow_logs_custom_integration ADD
PARTITION (`aws-account-id`='<your new account number>', `aws-service`='vpcflowlogs', `aws-region`='<your region>', year='yyyy', month='mm', day='dd')
LOCATION 's3://<VPC-Flow-Logs-Bucket-Name>/<VPC-Flow-Logs-Prefix>/AWSLogs/<your new account number>/vpcflowlogs/<your region>/yyyy/mm/dd';
Replace <your new account number>
with your new account number at 2 places
Replace the yyyy
,mm
,dd
with date for the log file at 2 places. Look into S3 bucket for files created under specific date
Note: Navigate to S3 service and click on S3 bucket you have created to store VPC Flow Logs, to see the date and region information as shown in example image below. If you do not see any content then you may need to wait until log records are written to the bucket based on 1- or 10-minutes granularity

Replace <your region>
with respective region in S3 bucket for vpc flow logs at 2 places
In LOCATION Replace complete S3 url with appropriate path where your VPC logs are stored
<VPC-Flow-Logs-Bucket-Name>
with bucket name where logs are stored<VPC-Flow-Logs-Prefix>
with Flow Logs prefix you have used while enabling logs. If you have not provided any prefix at the time of enabling it you can remove it from above path.
Example below:
ALTER TABLE vpc_flow_logs_custom_integration ADD
PARTITION (`aws-account-id`='0123456789', `aws-service`='vpcflowlogs', `aws-region`='us-east-1', year='2021', month='10', day='27')
LOCATION 's3://my-central-vpc-flow-logs/vpc-flow-logs/AWSLogs/0123456789/vpcflowlogs/us-east-1/2021/10/27';