Created
May 22, 2024 16:08
-
-
Save iwstkhr/bea38d3cd2bf13f901d04ad092bdb593 to your computer and use it in GitHub Desktop.
Deleting All Inactive Deployments of AWS IoT Greengrass V2 Components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run this scripts like the following: | |
# python inactive_deployments_deleter.py --target-arn "arn:aws:iot:<AWS_REGION>:<AWS_ACCOUNT_ID>:thing/<YOUR_THING_NAME>" | |
import argparse | |
from time import sleep | |
import boto3 | |
client = boto3.client('greengrassv2') | |
def list_inactive_deployments(arn: str, next_token: str = '') -> list: | |
""" List inactive deployments. | |
Args: | |
arn (str): ARN of an AWS IoT Thing/Thing Group | |
next_token (str): Next token returned by `list_deployments` API | |
Returns: | |
list: All INACTIVE deployments | |
""" | |
result = client.list_deployments( | |
targetArn=arn, | |
historyFilter='ALL', | |
nextToken=next_token, | |
maxResults=100, | |
) | |
deployments = result.get('deployments', []) | |
deployments = [deployment for deployment in deployments if deployment.get('deploymentStatus') == 'INACTIVE'] | |
if result.get('nextToken'): | |
sleep(0.5) | |
deployments.extend(list_inactive_deployments(arn, result.get('nextToken'))) | |
return deployments | |
def delete_deployments(deployments: list) -> None: | |
""" Delete deployments. | |
Args: | |
deployments (list): Deployments returned by `list_deployments` API | |
""" | |
for deployment in deployments: | |
client.delete_deployment(deploymentId=deployment.get('deploymentId')) | |
sleep(0.5) | |
def parse_args() -> argparse.Namespace: | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--target-arn', required=True) | |
return parser.parse_args() | |
def main() -> None: | |
args = parse_args() | |
deployments = list_inactive_deployments(args.target_arn) | |
delete_deployments(deployments) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment