Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4933e0

Browse files
authored
update api-dependencies and check cloudwatch/logs enabled (localstack#9576)
1 parent 0750764 commit d4933e0

File tree

7 files changed

+30
-10
lines changed

7 files changed

+30
-10
lines changed

‎localstack/services/lambda_/event_source_listeners/event_source_listener.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import logging
12
from typing import Dict, Optional, Type
23

34
from localstack.services.lambda_.event_source_listeners.adapters import (
45
EventSourceAdapter,
56
EventSourceAsfAdapter,
67
)
78
from localstack.services.lambda_.invocation.lambda_service import LambdaService
9+
from localstack.utils.bootstrap import is_api_enabled
810
from localstack.utils.objects import SubtypesInstanceManager
911

12+
LOG = logging.getLogger(__name__)
13+
1014

1115
class EventSourceListener(SubtypesInstanceManager):
1216
INSTANCES: Dict[str, "EventSourceListener"] = {}
@@ -40,6 +44,12 @@ def start_listeners_for_asf(event_source_mapping: Dict, lambda_service: LambdaSe
4044
)
4145
if self_managed_endpoints.get("KAFKA_BOOTSTRAP_SERVERS"):
4246
service_type = "kafka"
47+
elif not is_api_enabled(service_type):
48+
LOG.info(
49+
"Service %s is not enabled, cannot enable event-source-mapping. Please check your 'SERVICES' configuration variable.",
50+
service_type,
51+
)
52+
return
4353
instance = EventSourceListener.get(service_type, raise_if_missing=False)
4454
if instance:
4555
instance.start(EventSourceAsfAdapter(lambda_service))

‎localstack/services/lambda_/invocation/logs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from localstack.aws.connect import connect_to
88
from localstack.utils.aws.client_types import ServicePrincipal
9+
from localstack.utils.bootstrap import is_api_enabled
910
from localstack.utils.cloudwatch.cloudwatch_util import store_cloudwatch_logs
1011
from localstack.utils.threads import FuncThread
1112

@@ -62,10 +63,15 @@ def run_log_loop(self, *args, **kwargs) -> None:
6263
)
6364

6465
def start_subscriber(self) -> None:
66+
if not is_api_enabled("logs"):
67+
LOG.debug("Service 'logs' is disabled, not storing any logs for lambda executions")
68+
return
6569
self._thread = FuncThread(self.run_log_loop, name="log_handler")
6670
self._thread.start()
6771

6872
def add_logs(self, log_item: LogItem) -> None:
73+
if not is_api_enabled("logs"):
74+
return
6975
self.log_queue.put(log_item)
7076

7177
def stop(self) -> None:

‎localstack/services/lambda_/provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
from localstack.services.plugins import ServiceLifecycleHook
189189
from localstack.state import StateVisitor
190190
from localstack.utils.aws.arns import extract_service_from_arn
191+
from localstack.utils.bootstrap import is_api_enabled
191192
from localstack.utils.collections import PaginatedList
192193
from localstack.utils.files import load_file
193194
from localstack.utils.strings import get_random_hex, long_uid, short_uid, to_bytes, to_str
@@ -393,7 +394,7 @@ def _build_vpc_config(
393394
region_name: str,
394395
vpc_config: Optional[dict] = None,
395396
) -> VpcConfig | None:
396-
if not vpc_config:
397+
if not vpc_config or not is_api_enabled("ec2"):
397398
return None
398399

399400
subnet_ids = vpc_config.get("SubnetIds", [])

‎localstack/services/logs/provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from localstack.services.plugins import ServiceLifecycleHook
4242
from localstack.utils.aws import arns
4343
from localstack.utils.aws.arns import extract_region_from_arn
44+
from localstack.utils.bootstrap import is_api_enabled
4445
from localstack.utils.common import is_number
4546
from localstack.utils.patch import patch
4647

@@ -61,7 +62,7 @@ def put_log_events(
6162
sequence_token: SequenceToken = None,
6263
) -> PutLogEventsResponse:
6364
logs_backend = get_moto_logs_backend(context.account_id, context.region)
64-
metric_filters = logs_backend.filters.metric_filters
65+
metric_filters = logs_backend.filters.metric_filters if is_api_enabled("cloudwatch") else []
6566
for metric_filter in metric_filters:
6667
pattern = metric_filter.get("filterPattern", "")
6768
transformations = metric_filter.get("metricTransformations", [])

‎localstack/utils/bootstrap.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
"dynamodb": ["dynamodbstreams"],
4747
"dynamodbstreams": ["kinesis"],
4848
"es": ["opensearch"],
49-
"lambda": ["logs", "cloudwatch", "s3", "sqs"],
50-
"kinesis": ["dynamodb"],
49+
"lambda": ["s3", "sqs", "sts"],
5150
"firehose": ["kinesis"],
5251
}
5352
# composites define an abstract name like "serverless" that maps to a set of services

‎localstack/utils/cloudwatch/cloudwatch_util.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
from werkzeug import Response as WerkzeugResponse
77

8-
from localstack import config
98
from localstack.aws.connect import connect_to
9+
from localstack.utils.bootstrap import is_api_enabled
1010
from localstack.utils.strings import to_str
1111
from localstack.utils.time import now_utc
1212

@@ -25,7 +25,7 @@ def dimension_lambda(kwargs):
2525

2626
def publish_lambda_metric(metric, value, kwargs, region_name: Optional[str] = None):
2727
# publish metric only if CloudWatch service is available
28-
if not config.service_port("cloudwatch"):
28+
if not is_api_enabled("cloudwatch"):
2929
return
3030
cw_client = connect_to(region_name=region_name).cloudwatch
3131
try:
@@ -62,6 +62,8 @@ def publish_sqs_metric(
6262
:param value The value of the metric data, default: 1
6363
:param unit The unit of the metric data, default: "Count"
6464
"""
65+
if not is_api_enabled("cloudwatch"):
66+
return
6567
cw_client = connect_to(region_name=region, aws_access_key_id=account_id).cloudwatch
6668
try:
6769
cw_client.put_metric_data(
@@ -104,6 +106,8 @@ def store_cloudwatch_logs(
104106
start_time=None,
105107
auto_create_group: Optional[bool] = True,
106108
):
109+
if not is_api_enabled("logs"):
110+
return
107111
start_time = start_time or int(time.time() * 1000)
108112
log_output = to_str(log_output)
109113

‎tests/unit/utils/test_bootstrap.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,17 @@ def test_resolve_meta(self):
156156
with temporary_env({"SERVICES": "es,lambda", "STRICT_SERVICE_LOADING": "1"}):
157157
result = get_enabled_apis()
158158

159-
assert len(result) == 7
159+
assert len(result) == 6
160160
assert result == {
161161
# directly given
162162
"lambda",
163163
"es",
164164
# a dependency of es
165165
"opensearch",
166-
# lambda has internal dependencies on s3, sqs, logs and cloudwatch
166+
# lambda has internal dependencies on s3, sqs, and sts
167167
"s3",
168168
"sqs",
169-
"logs",
170-
"cloudwatch",
169+
"sts",
171170
}
172171

173172

0 commit comments

Comments
 (0)