---
title: "Discussion on \"Set AWS Cloudwatch log groups Retention Policy for all Log  using python boto3 script\" | Hashnode"
description: "Discussion on \"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 in the account"
url: "https://hashnode.com/posts/set-aws-cloudwatch-log-groups-retention-policy-for-all-log-using-python-boto3-script/61362fc5e9421a617c32f157"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Set AWS Cloudwatch log groups Retention Policy for all Log using python boto3 script](https://dheerajchoudhary.hashnode.dev/set-aws-cloudwatch-log-groups-retention-policy-for-all-log-using-python-boto3-script?utm_source=hashnode&utm_medium=feed)

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

Sep 3, 2021 · 2 min read · dheerajchoudhary.hashnode.dev  
Updated Jan 25, 2022  
Read the article

-   [#2articles1week](https://hashnode.com/tag/2articles1week)
-   [#aws](https://hashnode.com/tag/aws)
-   [#cloud-computing](https://hashnode.com/tag/cloud-computing)
-   [#cloud](https://hashnode.com/tag/cloud)
-   [#logging](https://hashnode.com/tag/logging)

## Written by

[Dheeraj Choudhary](https://hashnode.com/@Dheeraj-Choudhary)

@Dheeraj-Choudhary · India

  
[View profile](https://hashnode.com/@Dheeraj-Choudhary)

Lets start from somewhere

I am a Technology Designer and YouTuber who likes to write Technical Blogs and contribute to Open Source which mainly includes Cloud, DevOps,Linux etc.

## Responses 1

Markdown supported · ⌘↵ to post

[Manish Gautam](https://hashnode.com/@GAUTAM67)

[Jun 27, 2022](https://hashnode.com/posts/set-aws-cloudwatch-log-groups-retention-policy-for-all-log-using-python-boto3-script/61362fc5e9421a617c32f157/comment/62b9e837f2e6c7d70c1244d5)

Great article. But I need to apply the retention policy only to those log group whose retention days is more then 30 days. Please help

[Dheeraj Choudhary](https://hashnode.com/@Dheeraj-Choudhary)

[Jun 27, 2022](https://hashnode.com/posts/set-aws-cloudwatch-log-groups-retention-policy-for-all-log-using-python-boto3-script/61362fc5e9421a617c32f157/comment/62b9e837f2e6c7d70c1244d5#reply-62b9f289f2e6c7d70c124637)

response = client.describe\_log\_groups() will return below dictionary as seen below { 'logGroups': \[ { 'logGroupName': 'string', 'creationTime': 123, 'retentionInDays': 123, 'metricFilterCount': 123, 'arn': 'string', 'storedBytes': 123, 'kmsKeyId': 'string' }, \], 'nextToken': 'string' } The way I iterated through this dict response using for loop to fetch loggroupname and append in list same way do it for retention days and append it to dictionary, then based on key value run conditions to execute program
