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.
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 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.
- 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.
- 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 here
To view entire github code please click here
for i in newlist: log=client.put_retention_policy( logGroupName=i, retentionInDays=30 ) print(log)
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.....