In this blog we will write python script using boto3 which will set retention policy for all existing log groups which are already created in the account at one go.
Prerequisite
An AWS Account An IAM User with:
- AWS Management Console access to verify your EC2 instances launched,listed and terminated.
- The IAM permissions required to perform IAM, EC2, and CloudWatch activities. IAM policy creation and AWS Application Programming Interface (API) permissions are outside this articleβs scope. Always adhere to the principle of least privilege when authorizing accounts to perform actions. Administrative access to an EC2 Instance.
- Install awscli using aws official documentationΒ here
- Install python and boto3
- Configure aws cli by using official documentationΒ here
Boto3
Boto3 is the name of the Python SDK for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts.
Python Boto3 Script
- Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope.
import boto3
- We will invoke the client for logs
client = boto3.client('logs')
- To describe log groups we have to use method “describe_log_groups()”. This method helps us describe log groups already created in your account.
GotoΒ linkΒ where you will find all parameters and filter list. Based on your requirement you can use it to describe your logs. This document also mentions datatype of the parameter.response = client.describe_log_groups()
- Create an empty list in which we will save the list of log group names existing.
newlist=[]
- Now we will traverse the dict using for loop to save the log groups name for which we need to set the retention policy.
for logs in response['logGroups']: newlist.append(logs['logGroupName'])
- Now we will use method “put_retention_policy()” to set retention day for all log groups name from our list “newlist” and will print the output. Make sure to check official documentationΒ hereTo view entire github code please clickΒ here
for i in newlist: log=client.put_retention_policy( logGroupName=i, retentionInDays=30 ) print(log)
π₯π₯ Conclusion π₯π₯
Boto3 provided inbuild methods for AWS resources using which many task can be automated by writing a python script.
Stay tuned for my next blog…..
π’Β Stay tuned for my next blog…..
So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks
Author - Dheeraj Choudhary
RELATED ARTICLES
Set AWS Cloudwatch log groups Retention Policy for all Log using python boto3 script
In this blog we will write python script using boto3 which will set retention policy for all existing log groups which are already created i ...
List,Create And Delete S3 Buckets Using Python Boto3 Script
In this blog we are going to create python script to list, create and delete S3 buckets using boto3.Table Of ContentPrerequisite.Create S3 B ...