• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

localstack / localstack / 16766254352

05 Aug 2025 04:40PM UTC coverage: 86.892% (-0.02%) from 86.91%
16766254352

push

github

web-flow
CFNV2: defer deletions for correcting deploy order (#12936)

92 of 99 new or added lines in 6 files covered. (92.93%)

185 existing lines in 21 files now uncovered.

66597 of 76643 relevant lines covered (86.89%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.99
/localstack-core/localstack/services/cloudformation/engine/template_deployer.py
1
import base64
1✔
2
import json
1✔
3
import logging
1✔
4
import re
1✔
5
import traceback
1✔
6
import uuid
1✔
7
from typing import Optional
1✔
8

9
from botocore.exceptions import ClientError
1✔
10

11
from localstack import config
1✔
12
from localstack.aws.connect import connect_to
1✔
13
from localstack.constants import INTERNAL_AWS_SECRET_ACCESS_KEY
1✔
14
from localstack.services.cloudformation.analytics import track_resource_operation
1✔
15
from localstack.services.cloudformation.deployment_utils import (
1✔
16
    PLACEHOLDER_AWS_NO_VALUE,
17
    get_action_name_for_resource_change,
18
    log_not_available_message,
19
    remove_none_values,
20
)
21
from localstack.services.cloudformation.engine.changes import ChangeConfig, ResourceChange
1✔
22
from localstack.services.cloudformation.engine.entities import Stack, StackChangeSet
1✔
23
from localstack.services.cloudformation.engine.parameters import StackParameter
1✔
24
from localstack.services.cloudformation.engine.quirks import VALID_GETATT_PROPERTIES
1✔
25
from localstack.services.cloudformation.engine.resource_ordering import (
1✔
26
    order_changes,
27
    order_resources,
28
)
29
from localstack.services.cloudformation.engine.template_utils import (
1✔
30
    AWS_URL_SUFFIX,
31
    fn_equals_type_conversion,
32
    get_deps_for_resource,
33
)
34
from localstack.services.cloudformation.resource_provider import (
1✔
35
    Credentials,
36
    NoResourceProvider,
37
    OperationStatus,
38
    ProgressEvent,
39
    ResourceProviderExecutor,
40
    ResourceProviderPayload,
41
    get_resource_type,
42
)
43
from localstack.services.cloudformation.service_models import (
1✔
44
    DependencyNotYetSatisfied,
45
)
46
from localstack.services.cloudformation.stores import exports_map, find_stack
1✔
47
from localstack.utils.aws.arns import get_partition
1✔
48
from localstack.utils.functions import prevent_stack_overflow
1✔
49
from localstack.utils.json import clone_safe
1✔
50
from localstack.utils.strings import to_bytes, to_str
1✔
51
from localstack.utils.threads import start_worker_thread
1✔
52

53
from localstack.services.cloudformation.models import *  # noqa: F401, F403, isort:skip
1✔
54
from localstack.utils.urls import localstack_host
1✔
55

56
ACTION_CREATE = "create"
1✔
57
ACTION_DELETE = "delete"
1✔
58

59
REGEX_OUTPUT_APIGATEWAY = re.compile(
1✔
60
    rf"^(https?://.+\.execute-api\.)(?:[^-]+-){{2,3}}\d\.(amazonaws\.com|{AWS_URL_SUFFIX})/?(.*)$"
61
)
62
REGEX_DYNAMIC_REF = re.compile("{{resolve:([^:]+):(.+)}}")
1✔
63

64
LOG = logging.getLogger(__name__)
1✔
65

66
# list of static attribute references to be replaced in {'Fn::Sub': '...'} strings
67
STATIC_REFS = ["AWS::Region", "AWS::Partition", "AWS::StackName", "AWS::AccountId"]
1✔
68

69
# Mock value for unsupported type references
70
MOCK_REFERENCE = "unknown"
1✔
71

72

73
class NoStackUpdates(Exception):
1✔
74
    """Exception indicating that no actions are to be performed in a stack update (which is not allowed)"""
75

76
    pass
1✔
77

78

79
# ---------------------
80
# CF TEMPLATE HANDLING
81
# ---------------------
82

83

84
def get_attr_from_model_instance(
1✔
85
    resource: dict,
86
    attribute_name: str,
87
    resource_type: str,
88
    resource_id: str,
89
    attribute_sub_name: Optional[str] = None,
90
) -> str:
91
    if resource["PhysicalResourceId"] == MOCK_REFERENCE:
1✔
92
        LOG.warning(
1✔
93
            "Attribute '%s' requested from unsupported resource with id %s",
94
            attribute_name,
95
            resource_id,
96
        )
97
        return MOCK_REFERENCE
1✔
98

99
    properties = resource.get("Properties", {})
1✔
100
    # if there's no entry in VALID_GETATT_PROPERTIES for the resource type we still default to "open" and accept anything
101
    valid_atts = VALID_GETATT_PROPERTIES.get(resource_type)
1✔
102
    if valid_atts is not None and attribute_name not in valid_atts:
1✔
103
        LOG.warning(
×
104
            "Invalid attribute in Fn::GetAtt for %s:  | %s.%s",
105
            resource_type,
106
            resource_id,
107
            attribute_name,
108
        )
109
        raise Exception(
×
110
            f"Resource type {resource_type} does not support attribute {{{attribute_name}}}"
111
        )  # TODO: check CFn behavior via snapshot
112

113
    attribute_candidate = properties.get(attribute_name)
1✔
114
    if attribute_sub_name:
1✔
115
        return attribute_candidate.get(attribute_sub_name)
×
116
    if "." in attribute_name:
1✔
117
        # was used for legacy, but keeping it since it might have to work for a custom resource as well
118
        if attribute_candidate:
1✔
119
            return attribute_candidate
×
120

121
        # some resources (e.g. ElastiCache) have their readOnly attributes defined as Aa.Bb but the property is named AaBb
122
        if attribute_candidate := properties.get(attribute_name.replace(".", "")):
1✔
123
            return attribute_candidate
×
124

125
        # accessing nested properties
126
        parts = attribute_name.split(".")
1✔
127
        attribute = properties
1✔
128
        # TODO: the attribute fetching below is a temporary workaround for the dependency resolution.
129
        #  It is caused by trying to access the resource attribute that has not been deployed yet.
130
        #  This should be a hard error.“
131
        for part in parts:
1✔
132
            if attribute is None:
1✔
133
                return None
×
134
            attribute = attribute.get(part)
1✔
135
        return attribute
1✔
136

137
    # If we couldn't find the attribute, this is actually an irrecoverable error.
138
    # After the resource has a state of CREATE_COMPLETE, all attributes should already be set.
139
    # TODO: raise here instead
140
    # if attribute_candidate is None:
141
    # raise Exception(
142
    #     f"Failed to resolve attribute for Fn::GetAtt in {resource_type}: {resource_id}.{attribute_name}"
143
    # )  # TODO: check CFn behavior via snapshot
144
    return attribute_candidate
1✔
145

146

147
def resolve_ref(
1✔
148
    account_id: str,
149
    region_name: str,
150
    stack_name: str,
151
    resources: dict,
152
    parameters: dict[str, StackParameter],
153
    ref: str,
154
):
155
    """
156
    ref always needs to be a static string
157
    ref can be one of these:
158
    1. a pseudo-parameter (e.g. AWS::Region)
159
    2. a parameter
160
    3. the id of a resource (PhysicalResourceId
161
    """
162
    # pseudo parameter
163
    if ref == "AWS::Region":
1✔
164
        return region_name
1✔
165
    if ref == "AWS::Partition":
1✔
166
        return get_partition(region_name)
1✔
167
    if ref == "AWS::StackName":
1✔
168
        return stack_name
1✔
169
    if ref == "AWS::StackId":
1✔
170
        stack = find_stack(account_id, region_name, stack_name)
1✔
171
        if not stack:
1✔
172
            raise ValueError(f"No stack {stack_name} found")
×
173
        return stack.stack_id
1✔
174
    if ref == "AWS::AccountId":
1✔
175
        return account_id
1✔
176
    if ref == "AWS::NoValue":
1✔
177
        return PLACEHOLDER_AWS_NO_VALUE
1✔
178
    if ref == "AWS::NotificationARNs":
1✔
179
        # TODO!
180
        return {}
×
181
    if ref == "AWS::URLSuffix":
1✔
182
        return AWS_URL_SUFFIX
1✔
183

184
    # parameter
185
    if parameter := parameters.get(ref):
1✔
186
        parameter_type: str = parameter["ParameterType"]
1✔
187
        parameter_value = parameter.get("ResolvedValue") or parameter.get("ParameterValue")
1✔
188

189
        if "CommaDelimitedList" in parameter_type or parameter_type.startswith("List<"):
1✔
190
            return [p.strip() for p in parameter_value.split(",")]
1✔
191
        else:
192
            return parameter_value
1✔
193

194
    # resource
195
    resource = resources.get(ref)
1✔
196
    if not resource:
1✔
197
        raise Exception(
×
198
            f"Resource target for `Ref {ref}` could not be found. Is there a resource with name {ref} in your stack?"
199
        )
200

201
    return resources[ref].get("PhysicalResourceId")
1✔
202

203

204
# Using a @prevent_stack_overflow decorator here to avoid infinite recursion
205
# in case we load stack exports that have circular dependencies (see issue 3438)
206
# TODO: Potentially think about a better approach in the future
207
@prevent_stack_overflow(match_parameters=True)
1✔
208
def resolve_refs_recursively(
1✔
209
    account_id: str,
210
    region_name: str,
211
    stack_name: str,
212
    resources: dict,
213
    mappings: dict,
214
    conditions: dict[str, bool],
215
    parameters: dict,
216
    value,
217
):
218
    result = _resolve_refs_recursively(
1✔
219
        account_id, region_name, stack_name, resources, mappings, conditions, parameters, value
220
    )
221

222
    # localstack specific patches
223
    if isinstance(result, str):
1✔
224
        # we're trying to filter constructed API urls here (e.g. via Join in the template)
225
        api_match = REGEX_OUTPUT_APIGATEWAY.match(result)
1✔
226
        if api_match and result in config.CFN_STRING_REPLACEMENT_DENY_LIST:
1✔
227
            return result
1✔
228
        elif api_match:
1✔
229
            prefix = api_match[1]
1✔
230
            host = api_match[2]
1✔
231
            path = api_match[3]
1✔
232
            port = localstack_host().port
1✔
233
            return f"{prefix}{host}:{port}/{path}"
1✔
234

235
        # basic dynamic reference support
236
        # see: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html
237
        # technically there are more restrictions for each of these services but checking each of these
238
        # isn't really necessary for the current level of emulation
239
        dynamic_ref_match = REGEX_DYNAMIC_REF.match(result)
1✔
240
        if dynamic_ref_match:
1✔
241
            service_name = dynamic_ref_match[1]
1✔
242
            reference_key = dynamic_ref_match[2]
1✔
243

244
            # only these 3 services are supported for dynamic references right now
245
            if service_name == "ssm":
1✔
246
                ssm_client = connect_to(aws_access_key_id=account_id, region_name=region_name).ssm
1✔
247
                try:
1✔
248
                    return ssm_client.get_parameter(Name=reference_key)["Parameter"]["Value"]
1✔
249
                except ClientError as e:
×
250
                    LOG.error("client error accessing SSM parameter '%s': %s", reference_key, e)
×
251
                    raise
×
252
            elif service_name == "ssm-secure":
1✔
253
                ssm_client = connect_to(aws_access_key_id=account_id, region_name=region_name).ssm
1✔
254
                try:
1✔
255
                    return ssm_client.get_parameter(Name=reference_key, WithDecryption=True)[
1✔
256
                        "Parameter"
257
                    ]["Value"]
258
                except ClientError as e:
×
259
                    LOG.error("client error accessing SSM parameter '%s': %s", reference_key, e)
×
260
                    raise
×
261
            elif service_name == "secretsmanager":
1✔
262
                # reference key needs to be parsed further
263
                # because {{resolve:secretsmanager:secret-id:secret-string:json-key:version-stage:version-id}}
264
                # we match for "secret-id:secret-string:json-key:version-stage:version-id"
265
                # where
266
                #   secret-id can either be the secret name or the full ARN of the secret
267
                #   secret-string *must* be SecretString
268
                #   all other values are optional
269
                secret_id = reference_key
1✔
270
                [json_key, version_stage, version_id] = [None, None, None]
1✔
271
                if "SecretString" in reference_key:
1✔
272
                    parts = reference_key.split(":SecretString:")
1✔
273
                    secret_id = parts[0]
1✔
274
                    # json-key, version-stage and version-id are optional.
275
                    [json_key, version_stage, version_id] = f"{parts[1]}::".split(":")[:3]
1✔
276

277
                kwargs = {}  # optional args for get_secret_value
1✔
278
                if version_id:
1✔
279
                    kwargs["VersionId"] = version_id
×
280
                if version_stage:
1✔
281
                    kwargs["VersionStage"] = version_stage
×
282

283
                secretsmanager_client = connect_to(
1✔
284
                    aws_access_key_id=account_id, region_name=region_name
285
                ).secretsmanager
286
                try:
1✔
287
                    secret_value = secretsmanager_client.get_secret_value(
1✔
288
                        SecretId=secret_id, **kwargs
289
                    )["SecretString"]
290
                except ClientError:
×
291
                    LOG.error("client error while trying to access key '%s': %s", secret_id)
×
292
                    raise
×
293

294
                if json_key:
1✔
295
                    json_secret = json.loads(secret_value)
×
296
                    if json_key not in json_secret:
×
297
                        raise DependencyNotYetSatisfied(
×
298
                            resource_ids=secret_id,
299
                            message=f"Key {json_key} is not yet available in secret {secret_id}.",
300
                        )
301
                    return json_secret[json_key]
×
302
                else:
303
                    return secret_value
1✔
304
            else:
305
                LOG.warning(
×
306
                    "Unsupported service for dynamic parameter: service_name=%s", service_name
307
                )
308

309
    return result
1✔
310

311

312
@prevent_stack_overflow(match_parameters=True)
1✔
313
def _resolve_refs_recursively(
1✔
314
    account_id: str,
315
    region_name: str,
316
    stack_name: str,
317
    resources: dict,
318
    mappings: dict,
319
    conditions: dict,
320
    parameters: dict,
321
    value: dict | list | str | bytes | None,
322
):
323
    if isinstance(value, dict):
1✔
324
        keys_list = list(value.keys())
1✔
325
        stripped_fn_lower = keys_list[0].lower().split("::")[-1] if len(keys_list) == 1 else None
1✔
326

327
        # process special operators
328
        if keys_list == ["Ref"]:
1✔
329
            ref = resolve_ref(
1✔
330
                account_id, region_name, stack_name, resources, parameters, value["Ref"]
331
            )
332
            if ref is None:
1✔
333
                msg = 'Unable to resolve Ref for resource "%s" (yet)' % value["Ref"]
×
334
                LOG.debug("%s - %s", msg, resources.get(value["Ref"]) or set(resources.keys()))
×
335

336
                raise DependencyNotYetSatisfied(resource_ids=value["Ref"], message=msg)
×
337

338
            ref = resolve_refs_recursively(
1✔
339
                account_id,
340
                region_name,
341
                stack_name,
342
                resources,
343
                mappings,
344
                conditions,
345
                parameters,
346
                ref,
347
            )
348
            return ref
1✔
349

350
        if stripped_fn_lower == "getatt":
1✔
351
            attr_ref = value[keys_list[0]]
1✔
352
            attr_ref = attr_ref.split(".") if isinstance(attr_ref, str) else attr_ref
1✔
353
            resource_logical_id = attr_ref[0]
1✔
354
            attribute_name = attr_ref[1]
1✔
355
            attribute_sub_name = attr_ref[2] if len(attr_ref) > 2 else None
1✔
356

357
            # the attribute name can be a Ref
358
            attribute_name = resolve_refs_recursively(
1✔
359
                account_id,
360
                region_name,
361
                stack_name,
362
                resources,
363
                mappings,
364
                conditions,
365
                parameters,
366
                attribute_name,
367
            )
368
            resource = resources.get(resource_logical_id)
1✔
369

370
            resource_type = get_resource_type(resource)
1✔
371
            resolved_getatt = get_attr_from_model_instance(
1✔
372
                resource,
373
                attribute_name,
374
                resource_type,
375
                resource_logical_id,
376
                attribute_sub_name,
377
            )
378

379
            # TODO: we should check the deployment state and not try to GetAtt from a resource that is still IN_PROGRESS or hasn't started yet.
380
            if resolved_getatt is None:
1✔
381
                raise DependencyNotYetSatisfied(
×
382
                    resource_ids=resource_logical_id,
383
                    message=f"Could not resolve attribute '{attribute_name}' on resource '{resource_logical_id}'",
384
                )
385

386
            return resolved_getatt
1✔
387

388
        if stripped_fn_lower == "join":
1✔
389
            join_values = value[keys_list[0]][1]
1✔
390

391
            # this can actually be another ref that produces a list as output
392
            if isinstance(join_values, dict):
1✔
393
                join_values = resolve_refs_recursively(
1✔
394
                    account_id,
395
                    region_name,
396
                    stack_name,
397
                    resources,
398
                    mappings,
399
                    conditions,
400
                    parameters,
401
                    join_values,
402
                )
403

404
            # resolve reference in the items list
405
            assert isinstance(join_values, list)
1✔
406
            join_values = resolve_refs_recursively(
1✔
407
                account_id,
408
                region_name,
409
                stack_name,
410
                resources,
411
                mappings,
412
                conditions,
413
                parameters,
414
                join_values,
415
            )
416

417
            none_values = [v for v in join_values if v is None]
1✔
418
            if none_values:
1✔
419
                LOG.warning(
×
420
                    "Cannot resolve Fn::Join '%s' due to null values: '%s'", value, join_values
421
                )
422
                raise Exception(
×
423
                    f"Cannot resolve CF Fn::Join {value} due to null values: {join_values}"
424
                )
425
            return value[keys_list[0]][0].join([str(v) for v in join_values])
1✔
426

427
        if stripped_fn_lower == "sub":
1✔
428
            item_to_sub = value[keys_list[0]]
1✔
429

430
            attr_refs = {r: {"Ref": r} for r in STATIC_REFS}
1✔
431
            if not isinstance(item_to_sub, list):
1✔
432
                item_to_sub = [item_to_sub, {}]
1✔
433
            result = item_to_sub[0]
1✔
434
            item_to_sub[1].update(attr_refs)
1✔
435

436
            for key, val in item_to_sub[1].items():
1✔
437
                resolved_val = resolve_refs_recursively(
1✔
438
                    account_id,
439
                    region_name,
440
                    stack_name,
441
                    resources,
442
                    mappings,
443
                    conditions,
444
                    parameters,
445
                    val,
446
                )
447

448
                if isinstance(resolved_val, (list, dict, tuple)):
1✔
449
                    # We don't have access to the resource that's a dependency in this case,
450
                    # so do the best we can with the resource ids
451
                    raise DependencyNotYetSatisfied(
×
452
                        resource_ids=key, message=f"Could not resolve {val} to terminal value type"
453
                    )
454
                result = result.replace("${%s}" % key, str(resolved_val))
1✔
455

456
            # resolve placeholders
457
            result = resolve_placeholders_in_string(
1✔
458
                account_id,
459
                region_name,
460
                result,
461
                stack_name,
462
                resources,
463
                mappings,
464
                conditions,
465
                parameters,
466
            )
467
            return result
1✔
468

469
        if stripped_fn_lower == "findinmap":
1✔
470
            # "Fn::FindInMap"
471
            mapping_id = value[keys_list[0]][0]
1✔
472

473
            if isinstance(mapping_id, dict) and "Ref" in mapping_id:
1✔
474
                # TODO: ??
475
                mapping_id = resolve_ref(
1✔
476
                    account_id, region_name, stack_name, resources, parameters, mapping_id["Ref"]
477
                )
478

479
            selected_map = mappings.get(mapping_id)
1✔
480
            if not selected_map:
1✔
481
                raise Exception(
×
482
                    f"Cannot find Mapping with ID {mapping_id} for Fn::FindInMap: {value[keys_list[0]]} {list(resources.keys())}"
483
                    # TODO: verify
484
                )
485

486
            first_level_attribute = value[keys_list[0]][1]
1✔
487
            first_level_attribute = resolve_refs_recursively(
1✔
488
                account_id,
489
                region_name,
490
                stack_name,
491
                resources,
492
                mappings,
493
                conditions,
494
                parameters,
495
                first_level_attribute,
496
            )
497

498
            if first_level_attribute not in selected_map:
1✔
499
                raise Exception(
1✔
500
                    f"Cannot find map key '{first_level_attribute}' in mapping '{mapping_id}'"
501
                )
502
            first_level_mapping = selected_map[first_level_attribute]
1✔
503

504
            second_level_attribute = value[keys_list[0]][2]
1✔
505
            if not isinstance(second_level_attribute, str):
1✔
506
                second_level_attribute = resolve_refs_recursively(
1✔
507
                    account_id,
508
                    region_name,
509
                    stack_name,
510
                    resources,
511
                    mappings,
512
                    conditions,
513
                    parameters,
514
                    second_level_attribute,
515
                )
516
            if second_level_attribute not in first_level_mapping:
1✔
517
                raise Exception(
1✔
518
                    f"Cannot find map key '{second_level_attribute}' in mapping '{mapping_id}' under key '{first_level_attribute}'"
519
                )
520

521
            return first_level_mapping[second_level_attribute]
1✔
522

523
        if stripped_fn_lower == "importvalue":
1✔
524
            import_value_key = resolve_refs_recursively(
1✔
525
                account_id,
526
                region_name,
527
                stack_name,
528
                resources,
529
                mappings,
530
                conditions,
531
                parameters,
532
                value[keys_list[0]],
533
            )
534
            exports = exports_map(account_id, region_name)
1✔
535
            stack_export = exports.get(import_value_key) or {}
1✔
536
            if not stack_export.get("Value"):
1✔
537
                LOG.info(
×
538
                    'Unable to find export "%s" in stack "%s", existing export names: %s',
539
                    import_value_key,
540
                    stack_name,
541
                    list(exports.keys()),
542
                )
543
                return None
×
544
            return stack_export["Value"]
1✔
545

546
        if stripped_fn_lower == "if":
1✔
547
            condition, option1, option2 = value[keys_list[0]]
1✔
548
            condition = conditions.get(condition)
1✔
549
            if condition is None:
1✔
550
                LOG.warning(
×
551
                    "Cannot find condition '%s' in conditions mapping: '%s'",
552
                    condition,
553
                    conditions.keys(),
554
                )
555
                raise KeyError(
×
556
                    f"Cannot find condition '{condition}' in conditions mapping: '{conditions.keys()}'"
557
                )
558

559
            result = resolve_refs_recursively(
1✔
560
                account_id,
561
                region_name,
562
                stack_name,
563
                resources,
564
                mappings,
565
                conditions,
566
                parameters,
567
                option1 if condition else option2,
568
            )
569
            return result
1✔
570

571
        if stripped_fn_lower == "condition":
1✔
572
            # FIXME: this should only allow strings, no evaluation should be performed here
573
            #   see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-condition.html
574
            key = value[keys_list[0]]
×
575
            result = conditions.get(key)
×
576
            if result is None:
×
577
                LOG.warning("Cannot find key '%s' in conditions: '%s'", key, conditions.keys())
×
578
                raise KeyError(f"Cannot find key '{key}' in conditions: '{conditions.keys()}'")
×
579
            return result
×
580

581
        if stripped_fn_lower == "not":
1✔
582
            condition = value[keys_list[0]][0]
×
583
            condition = resolve_refs_recursively(
×
584
                account_id,
585
                region_name,
586
                stack_name,
587
                resources,
588
                mappings,
589
                conditions,
590
                parameters,
591
                condition,
592
            )
593
            return not condition
×
594

595
        if stripped_fn_lower in ["and", "or"]:
1✔
596
            conditions = value[keys_list[0]]
×
597
            results = [
×
598
                resolve_refs_recursively(
599
                    account_id,
600
                    region_name,
601
                    stack_name,
602
                    resources,
603
                    mappings,
604
                    conditions,
605
                    parameters,
606
                    cond,
607
                )
608
                for cond in conditions
609
            ]
610
            result = all(results) if stripped_fn_lower == "and" else any(results)
×
611
            return result
×
612

613
        if stripped_fn_lower == "equals":
1✔
614
            operand1, operand2 = value[keys_list[0]]
×
615
            operand1 = resolve_refs_recursively(
×
616
                account_id,
617
                region_name,
618
                stack_name,
619
                resources,
620
                mappings,
621
                conditions,
622
                parameters,
623
                operand1,
624
            )
625
            operand2 = resolve_refs_recursively(
×
626
                account_id,
627
                region_name,
628
                stack_name,
629
                resources,
630
                mappings,
631
                conditions,
632
                parameters,
633
                operand2,
634
            )
635
            # TODO: investigate type coercion here
636
            return fn_equals_type_conversion(operand1) == fn_equals_type_conversion(operand2)
×
637

638
        if stripped_fn_lower == "select":
1✔
639
            index, values = value[keys_list[0]]
1✔
640
            index = resolve_refs_recursively(
1✔
641
                account_id,
642
                region_name,
643
                stack_name,
644
                resources,
645
                mappings,
646
                conditions,
647
                parameters,
648
                index,
649
            )
650
            values = resolve_refs_recursively(
1✔
651
                account_id,
652
                region_name,
653
                stack_name,
654
                resources,
655
                mappings,
656
                conditions,
657
                parameters,
658
                values,
659
            )
660
            try:
1✔
661
                return values[index]
1✔
662
            except TypeError:
1✔
663
                return values[int(index)]
1✔
664

665
        if stripped_fn_lower == "split":
1✔
666
            delimiter, string = value[keys_list[0]]
1✔
667
            delimiter = resolve_refs_recursively(
1✔
668
                account_id,
669
                region_name,
670
                stack_name,
671
                resources,
672
                mappings,
673
                conditions,
674
                parameters,
675
                delimiter,
676
            )
677
            string = resolve_refs_recursively(
1✔
678
                account_id,
679
                region_name,
680
                stack_name,
681
                resources,
682
                mappings,
683
                conditions,
684
                parameters,
685
                string,
686
            )
687
            return string.split(delimiter)
1✔
688

689
        if stripped_fn_lower == "getazs":
1✔
690
            region = (
1✔
691
                resolve_refs_recursively(
692
                    account_id,
693
                    region_name,
694
                    stack_name,
695
                    resources,
696
                    mappings,
697
                    conditions,
698
                    parameters,
699
                    value["Fn::GetAZs"],
700
                )
701
                or region_name
702
            )
703

704
            ec2_client = connect_to(aws_access_key_id=account_id, region_name=region).ec2
1✔
705
            try:
1✔
706
                get_availability_zones = ec2_client.describe_availability_zones()[
1✔
707
                    "AvailabilityZones"
708
                ]
709
            except ClientError:
×
710
                LOG.error("client error describing availability zones")
×
711
                raise
×
712

713
            azs = [az["ZoneName"] for az in get_availability_zones]
1✔
714

715
            return azs
1✔
716

717
        if stripped_fn_lower == "base64":
1✔
718
            value_to_encode = value[keys_list[0]]
1✔
719
            value_to_encode = resolve_refs_recursively(
1✔
720
                account_id,
721
                region_name,
722
                stack_name,
723
                resources,
724
                mappings,
725
                conditions,
726
                parameters,
727
                value_to_encode,
728
            )
729
            return to_str(base64.b64encode(to_bytes(value_to_encode)))
1✔
730

731
        for key, val in dict(value).items():
1✔
732
            value[key] = resolve_refs_recursively(
1✔
733
                account_id,
734
                region_name,
735
                stack_name,
736
                resources,
737
                mappings,
738
                conditions,
739
                parameters,
740
                val,
741
            )
742

743
    if isinstance(value, list):
1✔
744
        # in some cases, intrinsic functions are passed in as, e.g., `[['Fn::Sub', '${MyRef}']]`
745
        if len(value) == 1 and isinstance(value[0], list) and len(value[0]) == 2:
1✔
746
            inner_list = value[0]
×
747
            if str(inner_list[0]).lower().startswith("fn::"):
×
748
                return resolve_refs_recursively(
×
749
                    account_id,
750
                    region_name,
751
                    stack_name,
752
                    resources,
753
                    mappings,
754
                    conditions,
755
                    parameters,
756
                    {inner_list[0]: inner_list[1]},
757
                )
758

759
        # remove _aws_no_value_ from resulting references
760
        clean_list = []
1✔
761
        for item in value:
1✔
762
            temp_value = resolve_refs_recursively(
1✔
763
                account_id,
764
                region_name,
765
                stack_name,
766
                resources,
767
                mappings,
768
                conditions,
769
                parameters,
770
                item,
771
            )
772
            if not (isinstance(temp_value, str) and temp_value == PLACEHOLDER_AWS_NO_VALUE):
1✔
773
                clean_list.append(temp_value)
1✔
774
        value = clean_list
1✔
775

776
    return value
1✔
777

778

779
def resolve_placeholders_in_string(
1✔
780
    account_id: str,
781
    region_name: str,
782
    result,
783
    stack_name: str,
784
    resources: dict,
785
    mappings: dict,
786
    conditions: dict[str, bool],
787
    parameters: dict,
788
):
789
    """
790
    Resolve individual Fn::Sub variable replacements
791

792
    Variables can be template parameter names, resource logical IDs, resource attributes, or a variable in a key-value map
793
    """
794

795
    def _validate_result_type(value: str):
1✔
796
        is_another_account_id = value.isdigit() and len(value) == len(account_id)
1✔
797
        if value == account_id or is_another_account_id:
1✔
798
            return value
×
799

800
        if value.isdigit():
1✔
801
            return int(value)
1✔
802
        else:
803
            try:
1✔
804
                res = float(value)
1✔
805
                return res
1✔
806
            except ValueError:
1✔
807
                return value
1✔
808

809
    def _replace(match):
1✔
810
        ref_expression = match.group(1)
1✔
811
        parts = ref_expression.split(".")
1✔
812
        if len(parts) >= 2:
1✔
813
            # Resource attributes specified => Use GetAtt to resolve
814
            logical_resource_id, _, attr_name = ref_expression.partition(".")
1✔
815
            resolved = get_attr_from_model_instance(
1✔
816
                resources[logical_resource_id],
817
                attr_name,
818
                get_resource_type(resources[logical_resource_id]),
819
                logical_resource_id,
820
            )
821
            if resolved is None:
1✔
822
                raise DependencyNotYetSatisfied(
×
823
                    resource_ids=logical_resource_id,
824
                    message=f"Unable to resolve attribute ref {ref_expression}",
825
                )
826
            if not isinstance(resolved, str):
1✔
827
                resolved = str(resolved)
×
828
            return resolved
1✔
829
        if len(parts) == 1:
1✔
830
            if parts[0] in resources or parts[0].startswith("AWS::"):
1✔
831
                # Logical resource ID or parameter name specified => Use Ref for lookup
832
                result = resolve_ref(
1✔
833
                    account_id, region_name, stack_name, resources, parameters, parts[0]
834
                )
835

836
                if result is None:
1✔
837
                    raise DependencyNotYetSatisfied(
×
838
                        resource_ids=parts[0],
839
                        message=f"Unable to resolve attribute ref {ref_expression}",
840
                    )
841
                # TODO: is this valid?
842
                # make sure we resolve any functions/placeholders in the extracted string
843
                result = resolve_refs_recursively(
1✔
844
                    account_id,
845
                    region_name,
846
                    stack_name,
847
                    resources,
848
                    mappings,
849
                    conditions,
850
                    parameters,
851
                    result,
852
                )
853
                # make sure we convert the result to string
854
                # TODO: do this more systematically
855
                result = "" if result is None else str(result)
1✔
856
                return result
1✔
857
            elif parts[0] in parameters:
1✔
858
                parameter = parameters[parts[0]]
1✔
859
                parameter_type: str = parameter["ParameterType"]
1✔
860
                parameter_value = parameter.get("ResolvedValue") or parameter.get("ParameterValue")
1✔
861

862
                if parameter_type in ["CommaDelimitedList"] or parameter_type.startswith("List<"):
1✔
863
                    return [p.strip() for p in parameter_value.split(",")]
×
864
                elif parameter_type == "Number":
1✔
865
                    return str(parameter_value)
1✔
866
                else:
867
                    return parameter_value
1✔
868
            else:
869
                raise DependencyNotYetSatisfied(
×
870
                    resource_ids=parts[0],
871
                    message=f"Unable to resolve attribute ref {ref_expression}",
872
                )
873
        # TODO raise exception here?
874
        return match.group(0)
×
875

876
    regex = r"\$\{([^\}]+)\}"
1✔
877
    result = re.sub(regex, _replace, result)
1✔
878
    return _validate_result_type(result)
1✔
879

880

881
def evaluate_resource_condition(conditions: dict[str, bool], resource: dict) -> bool:
1✔
882
    if condition := resource.get("Condition"):
1✔
883
        return conditions.get(condition, True)
1✔
884
    return True
1✔
885

886

887
# -----------------------
888
# MAIN TEMPLATE DEPLOYER
889
# -----------------------
890

891

892
class TemplateDeployer:
1✔
893
    def __init__(self, account_id: str, region_name: str, stack):
1✔
894
        self.stack = stack
1✔
895
        self.account_id = account_id
1✔
896
        self.region_name = region_name
1✔
897

898
    @property
1✔
899
    def resources(self):
1✔
900
        return self.stack.resources
1✔
901

902
    @property
1✔
903
    def mappings(self):
1✔
904
        return self.stack.mappings
×
905

906
    @property
1✔
907
    def stack_name(self):
1✔
908
        return self.stack.stack_name
×
909

910
    # ------------------
911
    # MAIN ENTRY POINTS
912
    # ------------------
913

914
    def deploy_stack(self):
1✔
915
        self.stack.set_stack_status("CREATE_IN_PROGRESS")
1✔
916
        try:
1✔
917
            self.apply_changes(
1✔
918
                self.stack,
919
                self.stack,
920
                initialize=True,
921
                action="CREATE",
922
            )
923
        except Exception as e:
×
924
            log_method = LOG.info
×
925
            if config.CFN_VERBOSE_ERRORS:
×
926
                log_method = LOG.exception
×
927
            log_method("Unable to create stack %s: %s", self.stack.stack_name, e)
×
928
            self.stack.set_stack_status("CREATE_FAILED")
×
929
            raise
×
930

931
    def apply_change_set(self, change_set: StackChangeSet):
1✔
932
        action = (
1✔
933
            "UPDATE"
934
            if change_set.stack.status in {"CREATE_COMPLETE", "UPDATE_COMPLETE"}
935
            else "CREATE"
936
        )
937
        change_set.stack.set_stack_status(f"{action}_IN_PROGRESS")
1✔
938
        # update parameters on parent stack
939
        change_set.stack.set_resolved_parameters(change_set.resolved_parameters)
1✔
940
        # update conditions on parent stack
941
        change_set.stack.set_resolved_stack_conditions(change_set.resolved_conditions)
1✔
942

943
        # update attributes that the stack inherits from the changeset
944
        change_set.stack.metadata["Capabilities"] = change_set.metadata.get("Capabilities")
1✔
945

946
        try:
1✔
947
            self.apply_changes(
1✔
948
                change_set.stack,
949
                change_set,
950
                action=action,
951
            )
952
        except Exception as e:
×
953
            LOG.info(
×
954
                "Unable to apply change set %s: %s", change_set.metadata.get("ChangeSetName"), e
955
            )
956
            change_set.metadata["Status"] = f"{action}_FAILED"
×
957
            self.stack.set_stack_status(f"{action}_FAILED")
×
958
            raise
×
959

960
    def update_stack(self, new_stack):
1✔
961
        self.stack.set_stack_status("UPDATE_IN_PROGRESS")
1✔
962
        # apply changes
963
        self.apply_changes(self.stack, new_stack, action="UPDATE")
1✔
964
        self.stack.set_time_attribute("LastUpdatedTime")
1✔
965

966
    # ----------------------------
967
    # DEPENDENCY RESOLUTION UTILS
968
    # ----------------------------
969

970
    def is_deployed(self, resource):
1✔
971
        return self.stack.resource_states.get(resource["LogicalResourceId"], {}).get(
1✔
972
            "ResourceStatus"
973
        ) in [
974
            "CREATE_COMPLETE",
975
            "UPDATE_COMPLETE",
976
        ]
977

978
    def all_resource_dependencies_satisfied(self, resource) -> bool:
1✔
979
        unsatisfied = self.get_unsatisfied_dependencies(resource)
×
980
        return not unsatisfied
×
981

982
    def get_unsatisfied_dependencies(self, resource):
1✔
983
        res_deps = self.get_resource_dependencies(
×
984
            resource
985
        )  # the output here is currently a set of merged IDs from both resources and parameters
986
        parameter_deps = {d for d in res_deps if d in self.stack.resolved_parameters}
×
987
        resource_deps = res_deps.difference(parameter_deps)
×
988
        res_deps_mapped = {v: self.stack.resources.get(v) for v in resource_deps}
×
989
        return self.get_unsatisfied_dependencies_for_resources(res_deps_mapped, resource)
×
990

991
    def get_unsatisfied_dependencies_for_resources(
1✔
992
        self, resources, depending_resource=None, return_first=True
993
    ):
994
        result = {}
×
995
        for resource_id, resource in resources.items():
×
996
            if not resource:
×
997
                raise Exception(
×
998
                    f"Resource '{resource_id}' not found in stack {self.stack.stack_name}"
999
                )
1000
            if not self.is_deployed(resource):
×
1001
                LOG.debug(
×
1002
                    "Dependency for resource %s not yet deployed: %s %s",
1003
                    depending_resource,
1004
                    resource_id,
1005
                    resource,
1006
                )
1007
                result[resource_id] = resource
×
1008
                if return_first:
×
1009
                    break
×
1010
        return result
×
1011

1012
    def get_resource_dependencies(self, resource: dict) -> set[str]:
1✔
1013
        """
1014
        Takes a resource and returns its dependencies on other resources via a str -> str mapping
1015
        """
1016
        # Note: using the original, unmodified template here to preserve Ref's ...
1017
        raw_resources = self.stack.template_original["Resources"]
×
1018
        raw_resource = raw_resources[resource["LogicalResourceId"]]
×
1019
        return get_deps_for_resource(raw_resource, self.stack.resolved_conditions)
×
1020

1021
    # -----------------
1022
    # DEPLOYMENT UTILS
1023
    # -----------------
1024

1025
    def init_resource_status(self, resources=None, stack=None, action="CREATE"):
1✔
1026
        resources = resources or self.resources
1✔
1027
        stack = stack or self.stack
1✔
1028
        for resource_id, resource in resources.items():
1✔
1029
            stack.set_resource_status(resource_id, f"{action}_IN_PROGRESS")
1✔
1030

1031
    def get_change_config(
1✔
1032
        self, action: str, resource: dict, change_set_id: Optional[str] = None
1033
    ) -> ChangeConfig:
1034
        result = ChangeConfig(
1✔
1035
            **{
1036
                "Type": "Resource",
1037
                "ResourceChange": ResourceChange(
1038
                    **{
1039
                        "Action": action,
1040
                        # TODO(srw): how can the resource not contain a logical resource id?
1041
                        "LogicalResourceId": resource.get("LogicalResourceId"),
1042
                        "PhysicalResourceId": resource.get("PhysicalResourceId"),
1043
                        "ResourceType": resource["Type"],
1044
                        # TODO ChangeSetId is only set for *nested* change sets
1045
                        # "ChangeSetId": change_set_id,
1046
                        "Scope": [],  # TODO
1047
                        "Details": [],  # TODO
1048
                    }
1049
                ),
1050
            }
1051
        )
1052
        if action == "Modify":
1✔
1053
            result["ResourceChange"]["Replacement"] = "False"
1✔
1054
        return result
1✔
1055

1056
    def resource_config_differs(self, resource_new):
1✔
1057
        """Return whether the given resource properties differ from the existing config (for stack updates)."""
1058
        # TODO: this is broken for default fields and result_handler property modifications when they're added to the properties in the model
1059
        resource_id = resource_new["LogicalResourceId"]
1✔
1060
        resource_old = self.resources[resource_id]
1✔
1061
        props_old = resource_old.get("SpecifiedProperties", {})
1✔
1062
        props_new = resource_new["Properties"]
1✔
1063
        ignored_keys = ["LogicalResourceId", "PhysicalResourceId"]
1✔
1064
        old_keys = set(props_old.keys()) - set(ignored_keys)
1✔
1065
        new_keys = set(props_new.keys()) - set(ignored_keys)
1✔
1066
        if old_keys != new_keys:
1✔
1067
            return True
1✔
1068
        for key in old_keys:
1✔
1069
            if props_old[key] != props_new[key]:
1✔
1070
                return True
1✔
1071
        old_status = self.stack.resource_states.get(resource_id) or {}
1✔
1072
        previous_state = (
1✔
1073
            old_status.get("PreviousResourceStatus") or old_status.get("ResourceStatus") or ""
1074
        )
1075
        if old_status and "DELETE" in previous_state:
1✔
1076
            return True
×
1077

1078
    # TODO: ?
1079
    def merge_properties(self, resource_id: str, old_stack, new_stack) -> None:
1✔
1080
        old_resources = old_stack.template["Resources"]
1✔
1081
        new_resources = new_stack.template["Resources"]
1✔
1082
        new_resource = new_resources[resource_id]
1✔
1083

1084
        old_resource = old_resources[resource_id] = old_resources.get(resource_id) or {}
1✔
1085
        for key, value in new_resource.items():
1✔
1086
            if key == "Properties":
1✔
1087
                continue
1✔
1088
            old_resource[key] = old_resource.get(key, value)
1✔
1089
        old_res_props = old_resource["Properties"] = old_resource.get("Properties", {})
1✔
1090
        for key, value in new_resource["Properties"].items():
1✔
1091
            old_res_props[key] = value
1✔
1092

1093
        old_res_props = {
1✔
1094
            k: v for k, v in old_res_props.items() if k in new_resource["Properties"].keys()
1095
        }
1096
        old_resource["Properties"] = old_res_props
1✔
1097

1098
        # overwrite original template entirely
1099
        old_stack.template_original["Resources"][resource_id] = new_stack.template_original[
1✔
1100
            "Resources"
1101
        ][resource_id]
1102

1103
    def construct_changes(
1✔
1104
        self,
1105
        existing_stack,
1106
        new_stack,
1107
        # TODO: remove initialize argument from here, and determine action based on resource status
1108
        initialize: Optional[bool] = False,
1109
        change_set_id=None,
1110
        append_to_changeset: Optional[bool] = False,
1111
        filter_unchanged_resources: Optional[bool] = False,
1112
    ) -> list[ChangeConfig]:
1113
        old_resources = existing_stack.template["Resources"]
1✔
1114
        new_resources = new_stack.template["Resources"]
1✔
1115
        deletes = [val for key, val in old_resources.items() if key not in new_resources]
1✔
1116
        adds = [val for key, val in new_resources.items() if initialize or key not in old_resources]
1✔
1117
        modifies = [
1✔
1118
            val for key, val in new_resources.items() if not initialize and key in old_resources
1119
        ]
1120

1121
        changes = []
1✔
1122
        for action, items in (("Remove", deletes), ("Add", adds), ("Modify", modifies)):
1✔
1123
            for item in items:
1✔
1124
                item["Properties"] = item.get("Properties", {})
1✔
1125
                if (
1✔
1126
                    not filter_unchanged_resources  # TODO: find out purpose of this
1127
                    or action != "Modify"
1128
                    or self.resource_config_differs(item)
1129
                ):
1130
                    change = self.get_change_config(action, item, change_set_id=change_set_id)
1✔
1131
                    changes.append(change)
1✔
1132

1133
        # append changes to change set
1134
        if append_to_changeset and isinstance(new_stack, StackChangeSet):
1✔
1135
            new_stack.changes.extend(changes)
1✔
1136

1137
        return changes
1✔
1138

1139
    def apply_changes(
1✔
1140
        self,
1141
        existing_stack: Stack,
1142
        new_stack: StackChangeSet,
1143
        change_set_id: Optional[str] = None,
1144
        initialize: Optional[bool] = False,
1145
        action: Optional[str] = None,
1146
    ):
1147
        old_resources = existing_stack.template["Resources"]
1✔
1148
        new_resources = new_stack.template["Resources"]
1✔
1149
        action = action or "CREATE"
1✔
1150
        # TODO: this seems wrong, not every resource here will be in an UPDATE_IN_PROGRESS state? (only the ones that will actually be updated)
1151
        self.init_resource_status(old_resources, action="UPDATE")
1✔
1152

1153
        # apply parameter changes to existing stack
1154
        # self.apply_parameter_changes(existing_stack, new_stack)
1155

1156
        # construct changes
1157
        changes = self.construct_changes(
1✔
1158
            existing_stack,
1159
            new_stack,
1160
            initialize=initialize,
1161
            change_set_id=change_set_id,
1162
        )
1163

1164
        # check if we have actual changes in the stack, and prepare properties
1165
        contains_changes = False
1✔
1166
        for change in changes:
1✔
1167
            res_action = change["ResourceChange"]["Action"]
1✔
1168
            resource = new_resources.get(change["ResourceChange"]["LogicalResourceId"])
1✔
1169
            #  FIXME: we need to resolve refs before diffing to detect if for example a parameter causes the change or not
1170
            #   unfortunately this would currently cause issues because we might not be able to resolve everything yet
1171
            # resource = resolve_refs_recursively(
1172
            #     self.stack_name,
1173
            #     self.resources,
1174
            #     self.mappings,
1175
            #     self.stack.resolved_conditions,
1176
            #     self.stack.resolved_parameters,
1177
            #     resource,
1178
            # )
1179
            if res_action in ["Add", "Remove"] or self.resource_config_differs(resource):
1✔
1180
                contains_changes = True
1✔
1181
            if res_action in ["Modify", "Add"]:
1✔
1182
                # mutating call that overwrites resource properties with new properties and overwrites the template in old stack with new template
1183
                self.merge_properties(resource["LogicalResourceId"], existing_stack, new_stack)
1✔
1184
        if not contains_changes:
1✔
1185
            raise NoStackUpdates("No updates are to be performed.")
1✔
1186

1187
        # merge stack outputs and conditions
1188
        existing_stack.outputs.update(new_stack.outputs)
1✔
1189
        existing_stack.conditions.update(new_stack.conditions)
1✔
1190

1191
        # TODO: ideally the entire template has to be replaced, but tricky at this point
1192
        existing_stack.template["Metadata"] = new_stack.template.get("Metadata")
1✔
1193
        existing_stack.template_body = new_stack.template_body
1✔
1194

1195
        # start deployment loop
1196
        return self.apply_changes_in_loop(
1✔
1197
            changes, existing_stack, action=action, new_stack=new_stack
1198
        )
1199

1200
    def apply_changes_in_loop(
1✔
1201
        self,
1202
        changes: list[ChangeConfig],
1203
        stack: Stack,
1204
        action: Optional[str] = None,
1205
        new_stack=None,
1206
    ):
1207
        def _run(*args):
1✔
1208
            status_reason = None
1✔
1209
            try:
1✔
1210
                self.do_apply_changes_in_loop(changes, stack)
1✔
1211
                status = f"{action}_COMPLETE"
1✔
1212
            except Exception as e:
1✔
1213
                log_method = LOG.debug
1✔
1214
                if config.CFN_VERBOSE_ERRORS:
1✔
1215
                    log_method = LOG.exception
×
1216
                log_method(
1✔
1217
                    'Error applying changes for CloudFormation stack "%s": %s %s',
1218
                    stack.stack_name,
1219
                    e,
1220
                    traceback.format_exc(),
1221
                )
1222
                status = f"{action}_FAILED"
1✔
1223
                status_reason = str(e)
1✔
1224
            stack.set_stack_status(status, status_reason)
1✔
1225
            if isinstance(new_stack, StackChangeSet):
1✔
1226
                new_stack.metadata["Status"] = status
1✔
1227
                exec_result = "EXECUTE_FAILED" if "FAILED" in status else "EXECUTE_COMPLETE"
1✔
1228
                new_stack.metadata["ExecutionStatus"] = exec_result
1✔
1229
                result = "failed" if "FAILED" in status else "succeeded"
1✔
1230
                new_stack.metadata["StatusReason"] = status_reason or f"Deployment {result}"
1✔
1231

1232
        # run deployment in background loop, to avoid client network timeouts
1233
        return start_worker_thread(_run)
1✔
1234

1235
    def prepare_should_deploy_change(
1✔
1236
        self, resource_id: str, change: ResourceChange, stack, new_resources: dict
1237
    ) -> bool:
1238
        """
1239
        TODO: document
1240
        """
1241
        resource = new_resources[resource_id]
1✔
1242
        res_change = change["ResourceChange"]
1✔
1243
        action = res_change["Action"]
1✔
1244

1245
        # check resource condition, if present
1246
        if not evaluate_resource_condition(stack.resolved_conditions, resource):
1✔
1247
            LOG.debug(
1✔
1248
                'Skipping deployment of "%s", as resource condition evaluates to false', resource_id
1249
            )
1250
            return False
1✔
1251

1252
        # resolve refs in resource details
1253
        resolve_refs_recursively(
1✔
1254
            self.account_id,
1255
            self.region_name,
1256
            stack.stack_name,
1257
            stack.resources,
1258
            stack.mappings,
1259
            stack.resolved_conditions,
1260
            stack.resolved_parameters,
1261
            resource,
1262
        )
1263

1264
        if action in ["Add", "Modify"]:
1✔
1265
            is_deployed = self.is_deployed(resource)
1✔
1266
            # TODO: Attaching the cached _deployed info here, as we should not change the "Add"/"Modify" attribute
1267
            #  here, which is used further down the line to determine the resource action CREATE/UPDATE. This is a
1268
            #  temporary workaround for now - to be refactored once we introduce proper stack resource state models.
1269
            res_change["_deployed"] = is_deployed
1✔
1270
            if not is_deployed:
1✔
1271
                return True
1✔
1272
            if action == "Add":
×
1273
                return False
×
1274
        elif action == "Remove":
1✔
1275
            return True
1✔
1276
        return True
×
1277

1278
    # Stack is needed here
1279
    def apply_change(self, change: ChangeConfig, stack: Stack) -> None:
1✔
1280
        change_details = change["ResourceChange"]
1✔
1281
        action = change_details["Action"]
1✔
1282
        resource_id = change_details["LogicalResourceId"]
1✔
1283
        resources = stack.resources
1✔
1284
        resource = resources[resource_id]
1✔
1285

1286
        # TODO: this should not be needed as resources are filtered out if the
1287
        # condition evaluates to False.
1288
        if not evaluate_resource_condition(stack.resolved_conditions, resource):
1✔
1289
            return
×
1290

1291
        # remove AWS::NoValue entries
1292
        resource_props = resource.get("Properties")
1✔
1293
        if resource_props:
1✔
1294
            resource["Properties"] = remove_none_values(resource_props)
1✔
1295

1296
        executor = self.create_resource_provider_executor()
1✔
1297
        resource_provider_payload = self.create_resource_provider_payload(
1✔
1298
            action, logical_resource_id=resource_id
1299
        )
1300

1301
        resource_type = get_resource_type(resource)
1✔
1302
        resource_provider = executor.try_load_resource_provider(resource_type)
1✔
1303
        track_resource_operation(action, resource_type, missing=resource_provider is None)
1✔
1304
        if resource_provider is not None:
1✔
1305
            # add in-progress event
1306
            resource_status = f"{get_action_name_for_resource_change(action)}_IN_PROGRESS"
1✔
1307
            physical_resource_id = None
1✔
1308
            if action in ("Modify", "Remove"):
1✔
1309
                previous_state = self.resources[resource_id].get("_last_deployed_state")
1✔
1310
                if not previous_state:
1✔
1311
                    # TODO: can this happen?
1312
                    previous_state = self.resources[resource_id]["Properties"]
1✔
1313
                physical_resource_id = executor.extract_physical_resource_id_from_model_with_schema(
1✔
1314
                    resource_model=previous_state,
1315
                    resource_type=resource["Type"],
1316
                    resource_type_schema=resource_provider.SCHEMA,
1317
                )
1318
            stack.add_stack_event(
1✔
1319
                resource_id=resource_id,
1320
                physical_res_id=physical_resource_id,
1321
                status=resource_status,
1322
            )
1323

1324
            # perform the deploy
1325
            progress_event = executor.deploy_loop(
1✔
1326
                resource_provider, resource, resource_provider_payload
1327
            )
1328
        else:
1329
            # track that we don't handle the resource, and possibly raise an exception
1330
            log_not_available_message(
1✔
1331
                resource_type,
1332
                f'No resource provider found for "{resource_type}"',
1333
            )
1334

1335
            if not config.CFN_IGNORE_UNSUPPORTED_RESOURCE_TYPES:
1✔
1336
                raise NoResourceProvider
×
1337

1338
            resource["PhysicalResourceId"] = MOCK_REFERENCE
1✔
1339
            progress_event = ProgressEvent(OperationStatus.SUCCESS, resource_model={})
1✔
1340

1341
        # TODO: clean up the surrounding loop (do_apply_changes_in_loop) so that the responsibilities are clearer
1342
        stack_action = get_action_name_for_resource_change(action)
1✔
1343
        match progress_event.status:
1✔
1344
            case OperationStatus.FAILED:
1✔
1345
                stack.set_resource_status(
1✔
1346
                    resource_id,
1347
                    f"{stack_action}_FAILED",
1348
                    status_reason=progress_event.message or "",
1349
                )
1350
                # TODO: remove exception raising here?
1351
                # TODO: fix request token
1352
                raise Exception(
1✔
1353
                    f'Resource handler returned message: "{progress_event.message}" (RequestToken: 10c10335-276a-33d3-5c07-018b684c3d26, HandlerErrorCode: InvalidRequest){progress_event.error_code}'
1354
                )
1355
            case OperationStatus.SUCCESS:
1✔
1356
                stack.set_resource_status(resource_id, f"{stack_action}_COMPLETE")
1✔
1357
            case OperationStatus.PENDING:
×
1358
                # signal to the main loop that we should come back to this resource in the future
1359
                raise DependencyNotYetSatisfied(
×
1360
                    resource_ids=[], message="Resource dependencies not yet satisfied"
1361
                )
1362
            case OperationStatus.IN_PROGRESS:
×
1363
                raise Exception("Resource deployment loop should not finish in this state")
×
1364
            case unknown_status:
×
1365
                raise Exception(f"Unknown operation status: {unknown_status}")
×
1366

1367
        # TODO: this is probably already done in executor, try removing this
1368
        resource["Properties"] = progress_event.resource_model
1✔
1369

1370
    def create_resource_provider_executor(self) -> ResourceProviderExecutor:
1✔
1371
        return ResourceProviderExecutor(
1✔
1372
            stack_name=self.stack.stack_name,
1373
            stack_id=self.stack.stack_id,
1374
        )
1375

1376
    def create_resource_provider_payload(
1✔
1377
        self, action: str, logical_resource_id: str
1378
    ) -> ResourceProviderPayload:
1379
        # FIXME: use proper credentials
1380
        creds: Credentials = {
1✔
1381
            "accessKeyId": self.account_id,
1382
            "secretAccessKey": INTERNAL_AWS_SECRET_ACCESS_KEY,
1383
            "sessionToken": "",
1384
        }
1385
        resource = self.resources[logical_resource_id]
1✔
1386

1387
        resource_provider_payload: ResourceProviderPayload = {
1✔
1388
            "awsAccountId": self.account_id,
1389
            "callbackContext": {},
1390
            "stackId": self.stack.stack_name,
1391
            "resourceType": resource["Type"],
1392
            "resourceTypeVersion": "000000",
1393
            # TODO: not actually a UUID
1394
            "bearerToken": str(uuid.uuid4()),
1395
            "region": self.region_name,
1396
            "action": action,
1397
            "requestData": {
1398
                "logicalResourceId": logical_resource_id,
1399
                "resourceProperties": resource["Properties"],
1400
                "previousResourceProperties": resource.get("_last_deployed_state"),  # TODO
1401
                "callerCredentials": creds,
1402
                "providerCredentials": creds,
1403
                "systemTags": {},
1404
                "previousSystemTags": {},
1405
                "stackTags": {},
1406
                "previousStackTags": {},
1407
            },
1408
        }
1409
        return resource_provider_payload
1✔
1410

1411
    def delete_stack(self):
1✔
1412
        if not self.stack:
1✔
1413
            return
×
1414
        self.stack.set_stack_status("DELETE_IN_PROGRESS")
1✔
1415
        stack_resources = list(self.stack.resources.values())
1✔
1416
        resources = {r["LogicalResourceId"]: clone_safe(r) for r in stack_resources}
1✔
1417
        original_resources = self.stack.template_original["Resources"]
1✔
1418

1419
        # TODO: what is this doing?
1420
        for key, resource in resources.items():
1✔
1421
            resource["Properties"] = resource.get(
1✔
1422
                "Properties", clone_safe(resource)
1423
            )  # TODO: why is there a fallback?
1424
            resource["ResourceType"] = get_resource_type(resource)
1✔
1425

1426
        ordered_resource_ids = list(
1✔
1427
            order_resources(
1428
                resources=original_resources,
1429
                resolved_conditions=self.stack.resolved_conditions,
1430
                resolved_parameters=self.stack.resolved_parameters,
1431
                reverse=True,
1432
            ).keys()
1433
        )
1434
        for i, resource_id in enumerate(ordered_resource_ids):
1✔
1435
            resource = resources[resource_id]
1✔
1436
            resource_type = get_resource_type(resource)
1✔
1437
            try:
1✔
1438
                # TODO: cache condition value in resource details on deployment and use cached value here
1439
                if not evaluate_resource_condition(
1✔
1440
                    self.stack.resolved_conditions,
1441
                    resource,
1442
                ):
1443
                    continue
1✔
1444

1445
                action = "Remove"
1✔
1446
                executor = self.create_resource_provider_executor()
1✔
1447
                resource_provider_payload = self.create_resource_provider_payload(
1✔
1448
                    action, logical_resource_id=resource_id
1449
                )
1450
                LOG.debug(
1✔
1451
                    'Handling "Remove" for resource "%s" (%s/%s) type "%s"',
1452
                    resource_id,
1453
                    i + 1,
1454
                    len(resources),
1455
                    resource_type,
1456
                )
1457
                resource_provider = executor.try_load_resource_provider(resource_type)
1✔
1458
                track_resource_operation(action, resource_type, missing=resource_provider is None)
1✔
1459
                if resource_provider is not None:
1✔
1460
                    event = executor.deploy_loop(
1✔
1461
                        resource_provider, resource, resource_provider_payload
1462
                    )
1463
                else:
1464
                    log_not_available_message(
1✔
1465
                        resource_type,
1466
                        f'No resource provider found for "{resource_type}"',
1467
                    )
1468

1469
                    if not config.CFN_IGNORE_UNSUPPORTED_RESOURCE_TYPES:
1✔
1470
                        raise NoResourceProvider
×
1471
                    event = ProgressEvent(OperationStatus.SUCCESS, resource_model={})
1✔
1472
                match event.status:
1✔
1473
                    case OperationStatus.SUCCESS:
1✔
1474
                        self.stack.set_resource_status(resource_id, "DELETE_COMPLETE")
1✔
1475
                    case OperationStatus.PENDING:
1✔
1476
                        # the resource is still being deleted, specifically the provider has
1477
                        # signalled that the deployment loop should skip this resource this
1478
                        # time and come back to it later, likely due to unmet child
1479
                        # resources still existing because we don't delete things in the
1480
                        # correct order yet.
1481
                        continue
×
1482
                    case OperationStatus.FAILED:
1✔
1483
                        LOG.exception(
1✔
1484
                            "Failed to delete resource with id %s. Reason: %s",
1485
                            resource_id,
1486
                            event.message or "unknown",
1487
                        )
1488
                    case OperationStatus.IN_PROGRESS:
×
1489
                        # the resource provider executor should not return this state, so
1490
                        # this state is a programming error
1491
                        raise Exception(
×
1492
                            "Programming error: ResourceProviderExecutor cannot return IN_PROGRESS"
1493
                        )
1494
                    case other_status:
×
1495
                        raise Exception(f"Use of unsupported status found: {other_status}")
×
1496

UNCOV
1497
            except Exception as e:
×
UNCOV
1498
                LOG.exception(
×
1499
                    "Failed to delete resource with id %s. Final exception: %s",
1500
                    resource_id,
1501
                    e,
1502
                )
1503

1504
        # update status
1505
        self.stack.set_stack_status("DELETE_COMPLETE")
1✔
1506
        self.stack.set_time_attribute("DeletionTime")
1✔
1507

1508
    def do_apply_changes_in_loop(self, changes: list[ChangeConfig], stack: Stack) -> list:
1✔
1509
        # apply changes in a retry loop, to resolve resource dependencies and converge to the target state
1510
        changes_done = []
1✔
1511
        new_resources = stack.resources
1✔
1512

1513
        sorted_changes = order_changes(
1✔
1514
            given_changes=changes,
1515
            resources=new_resources,
1516
            resolved_conditions=stack.resolved_conditions,
1517
            resolved_parameters=stack.resolved_parameters,
1518
        )
1519
        for change_idx, change in enumerate(sorted_changes):
1✔
1520
            res_change = change["ResourceChange"]
1✔
1521
            action = res_change["Action"]
1✔
1522
            is_add_or_modify = action in ["Add", "Modify"]
1✔
1523
            resource_id = res_change["LogicalResourceId"]
1✔
1524

1525
            # TODO: do resolve_refs_recursively once here
1526
            try:
1✔
1527
                if is_add_or_modify:
1✔
1528
                    should_deploy = self.prepare_should_deploy_change(
1✔
1529
                        resource_id, change, stack, new_resources
1530
                    )
1531
                    LOG.debug(
1✔
1532
                        'Handling "%s" for resource "%s" (%s/%s) type "%s" (should_deploy=%s)',
1533
                        action,
1534
                        resource_id,
1535
                        change_idx + 1,
1536
                        len(changes),
1537
                        res_change["ResourceType"],
1538
                        should_deploy,
1539
                    )
1540
                    if not should_deploy:
1✔
1541
                        stack_action = get_action_name_for_resource_change(action)
1✔
1542
                        stack.set_resource_status(resource_id, f"{stack_action}_COMPLETE")
1✔
1543
                        continue
1✔
1544
                elif action == "Remove":
1✔
1545
                    should_remove = self.prepare_should_deploy_change(
1✔
1546
                        resource_id, change, stack, new_resources
1547
                    )
1548
                    if not should_remove:
1✔
1549
                        continue
×
1550
                    LOG.debug(
1✔
1551
                        'Handling "%s" for resource "%s" (%s/%s) type "%s"',
1552
                        action,
1553
                        resource_id,
1554
                        change_idx + 1,
1555
                        len(changes),
1556
                        res_change["ResourceType"],
1557
                    )
1558
                self.apply_change(change, stack=stack)
1✔
1559
                changes_done.append(change)
1✔
1560
            except Exception as e:
1✔
1561
                status_action = {
1✔
1562
                    "Add": "CREATE",
1563
                    "Modify": "UPDATE",
1564
                    "Dynamic": "UPDATE",
1565
                    "Remove": "DELETE",
1566
                }[action]
1567
                stack.add_stack_event(
1✔
1568
                    resource_id=resource_id,
1569
                    physical_res_id=new_resources[resource_id].get("PhysicalResourceId"),
1570
                    status=f"{status_action}_FAILED",
1571
                    status_reason=str(e),
1572
                )
1573
                if config.CFN_VERBOSE_ERRORS:
1✔
1574
                    LOG.exception("Failed to deploy resource %s, stack deploy failed", resource_id)
×
1575
                raise
1✔
1576

1577
        # clean up references to deleted resources in stack
1578
        deletes = [c for c in changes_done if c["ResourceChange"]["Action"] == "Remove"]
1✔
1579
        for delete in deletes:
1✔
1580
            stack.template["Resources"].pop(delete["ResourceChange"]["LogicalResourceId"], None)
1✔
1581

1582
        # resolve outputs
1583
        stack.resolved_outputs = resolve_outputs(self.account_id, self.region_name, stack)
1✔
1584

1585
        return changes_done
1✔
1586

1587

1588
# FIXME: resolve_refs_recursively should not be needed, the resources themselves should have those values available already
1589
def resolve_outputs(account_id: str, region_name: str, stack) -> list[dict]:
1✔
1590
    result = []
1✔
1591
    for k, details in stack.outputs.items():
1✔
1592
        if not evaluate_resource_condition(stack.resolved_conditions, details):
1✔
1593
            continue
1✔
1594
        value = None
1✔
1595
        try:
1✔
1596
            resolve_refs_recursively(
1✔
1597
                account_id,
1598
                region_name,
1599
                stack.stack_name,
1600
                stack.resources,
1601
                stack.mappings,
1602
                stack.resolved_conditions,
1603
                stack.resolved_parameters,
1604
                details,
1605
            )
1606
            value = details["Value"]
1✔
1607
        except Exception as e:
×
1608
            log_method = LOG.debug
×
1609
            if config.CFN_VERBOSE_ERRORS:
×
1610
                raise  # unresolvable outputs cause a stack failure
×
1611
                # log_method = getattr(LOG, "exception")
1612
            log_method("Unable to resolve references in stack outputs: %s - %s", details, e)
×
1613
        exports = details.get("Export") or {}
1✔
1614
        export = exports.get("Name")
1✔
1615
        export = resolve_refs_recursively(
1✔
1616
            account_id,
1617
            region_name,
1618
            stack.stack_name,
1619
            stack.resources,
1620
            stack.mappings,
1621
            stack.resolved_conditions,
1622
            stack.resolved_parameters,
1623
            export,
1624
        )
1625
        description = details.get("Description")
1✔
1626
        entry = {
1✔
1627
            "OutputKey": k,
1628
            "OutputValue": value,
1629
            "Description": description,
1630
            "ExportName": export,
1631
        }
1632
        result.append(entry)
1✔
1633
    return result
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc