-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from kiraum/kiraum/lightsail
feat(lightsail): adding ansiv resume
- Loading branch information
Showing
13 changed files
with
1,496 additions
and
30 deletions.
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
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from unittest.mock import MagicMock | ||
|
||
import boto3 | ||
|
||
from lambda_function import lambda_handler | ||
|
||
|
||
|
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,37 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: 'v4.5.0' | ||
hooks: | ||
- id: check-yaml | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
- repo: local | ||
hooks: | ||
- id: isort | ||
name: isort | ||
entry: isort -m3 --tc | ||
language: system | ||
- repo: local | ||
hooks: | ||
- id: black | ||
name: black | ||
entry: black | ||
language: python | ||
types_or: [python, pyi] | ||
- repo: local | ||
hooks: | ||
- id: ruff | ||
name: ruff | ||
entry: ruff check --force-exclude | ||
language: python | ||
types_or: [python, pyi] | ||
- repo: local | ||
hooks: | ||
- id: pylint | ||
name: pylint | ||
entry: pylint | ||
args: | ||
- --rcfile=${PWD}/pyproject.toml | ||
- -d format | ||
language: system | ||
types: [python] |
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,62 @@ | ||
""" | ||
Lightsail Container Service Disabler for AWS Lambda. | ||
Disables a Lightsail container service in response to a budget alert. | ||
Uses boto3 to interact with AWS services and sends notifications via SNS. | ||
""" | ||
|
||
import json | ||
import os | ||
|
||
import boto3 | ||
|
||
|
||
def lambda_handler(event, context): | ||
""" | ||
AWS Lambda function to disable a Lightsail container service and send notifications. | ||
Args: | ||
event (dict): The event dict that contains the parameters passed when the function | ||
is invoked. | ||
context (object): The context in which the function is called. | ||
Returns: | ||
dict: A dictionary containing a statusCode and a JSON-formatted body message. | ||
Environment Variables: | ||
CONTAINER_SERVICE_NAME (str): The name of the Lightsail container service to be disabled. | ||
SNS_TOPIC_ARN (str): The ARN of the SNS topic to publish notifications. | ||
""" | ||
lightsail = boto3.client("lightsail") | ||
sns = boto3.client("sns") | ||
container_service_name = os.environ["CONTAINER_SERVICE_NAME"] | ||
|
||
try: | ||
# Disable the container service | ||
lightsail.update_container_service( | ||
serviceName=container_service_name, | ||
isDisabled=True | ||
) | ||
|
||
message = f"Lightsail container service {container_service_name} has been disabled due to budget alert." | ||
|
||
# Send SNS notification | ||
sns.publish( | ||
TopicArn=os.environ["SNS_TOPIC_ARN"], | ||
Message=message, | ||
Subject="Lightsail Container Service Disabled" | ||
) | ||
|
||
return {"statusCode": 200, "body": json.dumps(message)} | ||
except Exception as exc: | ||
error_message = f"Error disabling Lightsail container service: {str(exc)}" | ||
print(error_message) | ||
|
||
# Send SNS notification for error | ||
sns.publish( | ||
TopicArn=os.environ["SNS_TOPIC_ARN"], | ||
Message=error_message, | ||
Subject="Error Disabling Lightsail Container Service" | ||
) | ||
|
||
return {"statusCode": 500, "body": json.dumps(error_message)} | ||
|
Oops, something went wrong.