-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
358 additions
and
84 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,8 @@ | ||
[run] | ||
omit = | ||
tests/* | ||
.venv-*/* | ||
test/* | ||
*/__init__.py | ||
source = | ||
. |
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,139 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
import json | ||
import boto3 | ||
import os | ||
from botocore import config | ||
import logging | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.INFO) | ||
|
||
stack_name = os.environ["STACK_NAME"] | ||
|
||
solution_version = os.environ.get("SOLUTION_VERSION", "v1.0.0") | ||
solution_id = os.environ.get("SOLUTION_ID", "SO8002") | ||
user_agent_config = { | ||
"user_agent_extra": f"AwsSolution/{solution_id}/{solution_version}" | ||
} | ||
default_config = config.Config(**user_agent_config) | ||
|
||
default_region = os.environ.get("AWS_REGION") | ||
|
||
bucket_name = os.environ.get("BUCKET_NAME", "") | ||
object_prefix = os.environ.get("OBJECT_PREFIX", "") | ||
|
||
event_queue_name = os.environ.get("EVENT_QUEUE_NAME", "") | ||
event_queue_arn = os.environ.get("EVENT_QUEUE_ARN", "") | ||
|
||
event_action = os.environ.get("EVENT_ACTION", "") | ||
|
||
notification_id = f"{stack_name}-{event_queue_name}" | ||
|
||
|
||
def lambda_handler(event, context): | ||
request_type = event["RequestType"] | ||
if request_type == "Create" or request_type == "Update": | ||
return on_create() | ||
if request_type == "Delete": | ||
return on_delete() | ||
raise Exception("Invalid request type: %s" % request_type) | ||
|
||
|
||
def on_create(): | ||
config_events = [] | ||
if event_action == "CreateAndDelete": | ||
config_events = ["s3:ObjectCreated:*", "s3:ObjectRemoved:*"] | ||
elif event_action == "Create": | ||
config_events = ["s3:ObjectCreated:*"] | ||
else: | ||
return { | ||
"statusCode": 200, | ||
"body": json.dumps("Skip creating s3 events."), | ||
} | ||
|
||
try: | ||
s3 = boto3.client("s3") | ||
history_config = s3.get_bucket_notification_configuration( | ||
Bucket=bucket_name, | ||
) | ||
logger.info(f"history notification config is {history_config}") | ||
|
||
queue_configurations = history_config.get("QueueConfigurations", []) | ||
queue_configurations.append( | ||
{ | ||
"Id": notification_id, | ||
"QueueArn": event_queue_arn, | ||
"Events": config_events, | ||
"Filter": { | ||
"Key": {"FilterRules": [{"Name": "prefix", "Value": object_prefix}]} | ||
}, | ||
} | ||
) | ||
|
||
notification_config = { | ||
"QueueConfigurations": queue_configurations, | ||
"TopicConfigurations": history_config.get("TopicConfigurations", []), | ||
"LambdaFunctionConfigurations": history_config.get( | ||
"LambdaFunctionConfigurations", [] | ||
), | ||
} | ||
if "EventBridgeConfiguration" in history_config: | ||
notification_config["EventBridgeConfiguration"] = history_config[ | ||
"EventBridgeConfiguration" | ||
] | ||
resp = s3.put_bucket_notification_configuration( | ||
Bucket=bucket_name, | ||
NotificationConfiguration=notification_config, | ||
) | ||
logger.info(f"put_bucket_notification_configuration resp is {resp}") | ||
except Exception as err: | ||
logger.error("Create log source s3 bucket notification failed, %s" % err) | ||
raise | ||
|
||
return { | ||
"statusCode": 200, | ||
"body": json.dumps("Create log source s3 bucket notification success!"), | ||
} | ||
|
||
|
||
def on_delete(): | ||
if event_action in ["CreateAndDelete", "Create"]: | ||
try: | ||
s3 = boto3.client("s3") | ||
history_config = s3.get_bucket_notification_configuration( | ||
Bucket=bucket_name, | ||
) | ||
logger.info(f"history notification config is {history_config}") | ||
queue_configurations = history_config.get("QueueConfigurations", []) | ||
deleted_queue_configurations = [ | ||
x for x in queue_configurations if x["Id"] != notification_id | ||
] | ||
|
||
notification_config = { | ||
"QueueConfigurations": deleted_queue_configurations, | ||
"TopicConfigurations": history_config.get("TopicConfigurations", []), | ||
"LambdaFunctionConfigurations": history_config.get( | ||
"LambdaFunctionConfigurations", [] | ||
), | ||
} | ||
if "EventBridgeConfiguration" in history_config: | ||
notification_config["EventBridgeConfiguration"] = history_config[ | ||
"EventBridgeConfiguration" | ||
] | ||
|
||
resp = s3.put_bucket_notification_configuration( | ||
Bucket=bucket_name, | ||
NotificationConfiguration=notification_config, | ||
) | ||
logger.info(f"put_bucket_notification_configuration resp is {resp}") | ||
except Exception as err: | ||
print("Delete log source s3 bucket notification failed, %s" % err) | ||
raise | ||
|
||
return { | ||
"statusCode": 200, | ||
"body": json.dumps("Delete log source s3 bucket notification success!"), | ||
} |
Empty file.
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,29 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import pytest | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def default_environment_variables(): | ||
"""Mocked AWS evivronment variables such as AWS credentials and region""" | ||
os.environ["AWS_ACCESS_KEY_ID"] = "mocked-aws-access-key-id" | ||
os.environ["AWS_SECRET_ACCESS_KEY"] = "mocked-aws-secret-access-key" | ||
os.environ["AWS_SESSION_TOKEN"] = "mocked-aws-session-token" | ||
os.environ["AWS_REGION"] = "us-east-1" | ||
os.environ["SOLUTION_VERSION"] = "v1.0.0" | ||
os.environ["SOLUTION_ID"] = "SO8002" | ||
|
||
os.environ["STACK_NAME"] = "test" | ||
|
||
os.environ["BUCKET_NAME"] = "test-bucket" | ||
os.environ["OBJECT_PREFIX"] = "test" | ||
|
||
os.environ["EVENT_QUEUE_ARN"] = "arn:aws:sqs:us-east-1:123456789012:test-queue" | ||
os.environ[ | ||
"LOG_EVENT_QUEUE_URL" | ||
] = "https://sqs.us-east-1.amazonaws.com/123456789012/test-queue" | ||
os.environ["EVENT_QUEUE_NAME"] = "test-queue" | ||
|
||
os.environ["EVENT_ACTION"] = "CreateAndDelete" |
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,4 @@ | ||
boto3 | ||
moto | ||
pytest | ||
pytest-cov |
39 changes: 39 additions & 0 deletions
39
source/lambda/custom-resource/test/test_custom_resource.py
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,39 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from moto import mock_logs, mock_s3, settings | ||
import pytest | ||
import os | ||
import boto3 | ||
|
||
|
||
@pytest.fixture | ||
def s3_client(): | ||
bucket_name = os.environ.get("BUCKET_NAME") | ||
with mock_s3(): | ||
s3 = boto3.resource("s3", region_name="us-east-1") | ||
# Create the bucket | ||
s3.create_bucket(Bucket=bucket_name) | ||
yield | ||
|
||
|
||
def test_lambda_handler_on_create(s3_client): | ||
from lambda_function import lambda_handler | ||
|
||
assert lambda_handler( | ||
{ | ||
"RequestType": "Create", | ||
}, | ||
None, | ||
) | ||
|
||
|
||
def test_lambda_handler_on_delete(s3_client): | ||
from lambda_function import lambda_handler | ||
|
||
assert lambda_handler( | ||
{ | ||
"RequestType": "Delete", | ||
}, | ||
None, | ||
) |
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
Oops, something went wrong.