Centralized Logging for Multi Accounts and Multi Regions Using CloudTrail
Tags: aws, awscloudtrail, awss3, cloudwatch
Published: 2022-02-23 17:13:52

“Challenges faced to find the solution of how to do centralized logging for multi accounts and multi regions using cloudtrail”. I have found the way by assigning permission on s3 and role for cloudwatch in trail. Also enable the option of multi-region trail and apply trail to my organization. Centralized logging makes monitoring very easy to access the logs of multi accounts in one place. In terms of cost and security perspective, it is a cheap service and also can be secured using KMS.

AWS CloudTrail is an AWS service that helps you enable governance, compliance, and operational and risk auditing of your AWS account. Actions taken by a user, role or an AWS service are recorded as events in CloudTrail. Events include actions taken in the AWS Management Console and AWS SDKs and APIs.

CloudTrail is enabled on your AWS account when you create it. When activity occurs in your AWS account, that activity is recorded in a CloudTrail event. You can easily view recent events in the CloudTrail console by going to Event history. For an ongoing record of activity and events in your AWS account, [create a trail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-create-a-trail-using-the-console-first-time.html). For more information about CloudTrail pricing, see [AWS CloudTrail Pricing](http://aws.amazon.com/cloudtrail/pricing/).

In this post, you will get to know how to do centralized logging for Multi Accounts and Multi Regions using cloudtrail. Here I have used two AWS accounts for having centralized logging, a s3 bucket in Master account and IAM role for cloudtrail logs to cloudwatch with required permissions.

#Prerequisites
You’ll need two [AWS Accounts](https://aws.amazon.com/free/?trk=ps_a134p000003yhmGAAQ&trkCampaign=acq_paid_search_brand&sc_channel=ps&sc_campaign=acquisition_IN&sc_publisher=google&sc_category=core&sc_country=IN&sc_geo=APAC&sc_outcome=Acquisition&sc_detail=create%20aws%20account&sc_content=Account_e&sc_matchtype=e&sc_segment=453325184860&sc_medium=ACQ-P|PS-GO|Brand|Desktop|SU|AWS|Core|IN|EN|Text&s_kwcid=AL!4422!3!453325184860!e!!g!!create%20aws%20account&ef_id=Cj0KCQiA09eQBhCxARIsAAYRiyl3Ip2W23Y6kzrEYPQ4h570rjP37hZ_JCLoBo6f5io_G-q7qFxTyGQaAmjOEALw_wcB:G:s&s_kwcid=AL!4422!3!453325184860!e!!g!!create%20aws%20account&all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) one as a master account and another as a sub-account. You’ll need the [AWS Organization](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_accounts_invites.html) to invite another sub-account to join the organization.
You’ll need an Amazon Simple Storage Service for this post. [Getting started with Amazon Simple Storage Service](https://aws.amazon.com/s3/getting-started/) provides instructions on how to create a bucket in simple storage service. For this blog, I assume that I have created a s3 bucket and have two accounts in organization.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9q5mjkvksiju0y57b69p.png)
The architecture diagram shows the overall deployment architecture with data flow, cloudtrail, cloudwatch and s3 bucket.

#Solution overview
The blog post consists of the following phases:
1. Create Trail in CloudTrail Console with required parameters. Also Set of permission on S3 bucket and Set IAM role with policy on Cloudwatch Logs for Trail
2. Output logs in S3 bucket for master account and sub-account and also log streams of CloudTrail in Cloudwatch Log Groups

I have two accounts, one master account and another sub-account. Also have a s3 bucket created as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n71swwq4jk9kc42toc22.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/quwl0uqts7au8c8x3k89.png)

##Phase 1: Create Trail in CloudTrail Console with required parameters. Also Set of permission on S3 bucket and Set IAM role with policy on Cloudwatch Logs for Trail   
1. Open the CloudTrail console, create a trail named as trail-logs. Change the trail log location, choosing the existing s3 bucket. Enable the log file validation and kms encryption as per requirement.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqvi91cgraoommwgduot.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fl5nk5hgrhtjbkmu041x.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imibfftioxsnj1hsxnb1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28p2zpq2rz5uvvp6ftip.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zay6whgzqqgm611n1sw9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y0y5kw9tc3t7dzgl5yv.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxap0ws9r78adrt9yln3.png)
2. Checkout the folder created in the bucket after choosing the log location named AWSLogs. And be able to see the folder inside it named with the master account number. In the permission tab of the bucket, add the permission policy of the sub-account.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gh2wveu2tlj477hzj6gj.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3w5t41wv2uim7452c41.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4u27airwtbsabap4lyt.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gtmrajscwpfaky67h75.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pggd15wbaja25h7s2l7a.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pd7s1l8jom7omr91vknv.png)
3. Create a role named CloudTrail-logs-Cloudwatch with permission required to have logs in cloudwatch log group for cloudtrail. And enable the cloudwatch logs in the cloudtrail console of the new trail created. Also enabled to use the same trail of organization for all sub-accounts.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxgctjx705m9mqkcufab.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oojcufb50luol04k697z.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftpn1wkxasihb4byzpzi.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8y2pbbc8ldmcdnmzmsm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsgah84ctq3x286ta06v.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qs1yaonffzfg1xplfs0n.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rs5u4t0okkrx9t5f4vi.png)

##Phase 2: Output logs in S3 bucket for master account and sub-account and also log streams of CloudTrail in Cloudwatch Log Groups 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kohjrpxfaaqm7p8a4pv7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67rksxs56vjbf8187bgf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40y3verssa0f7akpisr4.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufw4yxosc0tp8n6qf6gu.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuzjbuqfzpbx4228byqw.png)

#Clean-up
Delete AWS CloudTrail, IAM, S3 bucket, Cloudwatch Log Groups.

#Pricing
I review the pricing and estimated cost of this example. 
Cost of CloudTrail → 
$0.000001 per data event recorded for AWS CloudTrail APS3-DataEventsRecorded = 5.000 Events = $0.000005
$0.0 per free event recorded for AWS CloudTrail APS3-FreeEventsRecorded = 622.000 Events = $0.0 
Total = ($0.000005+$0.0) = $0.000005
Cost of Cloudwatch = $0.0
Cost of Data Transfer = $0.0
Cost of Simple Storage Service = $0.04
Total Cost = $(0.000005+0.0+0.0+0.04) = $0.040005

#Summary
In this post, I showed “how to do centralized logging for Multi Accounts and Multi Regions using cloudtrail”.
For more details on AWS CloudTrail, Checkout Get started AWS CloudTrail, open the [AWS CloudTrail console](https://ap-south-1.console.aws.amazon.com/cloudtrail/home?region=ap-south-1#/). To learn more, read the [AWS CloudTrail documentation](https://docs.aws.amazon.com/cloudtrail/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)