-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(terraform): add documentdb scheduler
This commit introduces a new feature to our Terraform configuration, enabling the addition of a Documentdb scheduler module. This module allows for the automated stopping or starting of Documentdb clusters based on predefined tags. By leveraging this scheduler, we can effectively manage the usage and costs associated with our Documentdb clusters, ensuring they are active only when needed and maximizing resource efficiency.
- Loading branch information
1 parent
7f8a896
commit 942f713
Showing
5 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""documentdb instances scheduler.""" | ||
|
||
from typing import Dict, List | ||
|
||
import boto3 | ||
|
||
from botocore.exceptions import ClientError | ||
|
||
from .exceptions import documentdb_exception | ||
from .filter_resources_by_tags import FilterByTags | ||
|
||
|
||
class DocumentDBScheduler: | ||
"""documentdb scheduler.""" | ||
|
||
def __init__(self, region_name=None) -> None: | ||
"""Initialize documentdb scheduler.""" | ||
if region_name: | ||
self.documentdb = boto3.client("docdb", region_name=region_name) | ||
else: | ||
self.documentdb = boto3.client("docdb") | ||
self.tag_api = FilterByTags(region_name=region_name) | ||
|
||
def stop(self, aws_tags: List[Dict]) -> None: | ||
"""Aws documentdb cluster stop function. | ||
Stop documentdb clusters with defined tags. | ||
:param list[map] aws_tags: | ||
Aws tags to use for filter resources. | ||
For example: | ||
[ | ||
{ | ||
'Key': 'string', | ||
'Values': [ | ||
'string', | ||
] | ||
} | ||
] | ||
""" | ||
for cluster_arn in self.tag_api.get_resources("rds:cluster", aws_tags): | ||
cluster_id = cluster_arn.split(":")[-1] | ||
try: | ||
self.documentdb.stop_db_cluster(DBClusterIdentifier=cluster_id) | ||
print(f"Stop documentdb cluster {cluster_id}") | ||
except ClientError as exc: | ||
documentdb_exception("documentdb cluster", cluster_id, exc) | ||
|
||
def start(self, aws_tags: List[Dict]) -> None: | ||
"""Aws documentdb cluster start function. | ||
Start documentdb clusters with defined tags. | ||
:param list[map] aws_tags: | ||
Aws tags to use for filter resources. | ||
For example: | ||
[ | ||
{ | ||
'Key': 'string', | ||
'Values': [ | ||
'string', | ||
] | ||
} | ||
] | ||
""" | ||
for cluster_arn in self.tag_api.get_resources("rds:cluster", aws_tags): | ||
cluster_id = cluster_arn.split(":")[-1] | ||
try: | ||
self.documentdb.start_db_cluster(DBClusterIdentifier=cluster_id) | ||
print(f"Start documentdb cluster {cluster_id}") | ||
except ClientError as exc: | ||
documentdb_exception("documentdb cluster", cluster_id, exc) |
This file contains 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
This file contains 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
This file contains 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