Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(appconfig): never reuse a hosted configuration version number, and keep a deployment when its stack resource is deleted (#1905)

iot-rocketfa381f4fb90e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f68feabd..36f8bc81 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
- **CloudFormation — `AWS::EC2::VPCGatewayAttachment` updates in place** — a changed `InternetGatewayId` or `VpnGatewayId` moves the attachment under the same `IGW|vpc-…` / `VGW|vpc-…` physical id, `VpnGatewayId` is attached at all, and a `VpcId` change leaves the gateway attached to the new VPC, or to the old one when the update rolls back.
- **CloudFormation — `AWS::Lambda::Version` publishes through `PublishVersion`** — the version takes its `Description`, a version of a function unchanged since its latest version fails with the `AlreadyExists` error AWS reports, `FunctionScalingConfig` on a function without a capacity provider is refused, and a function keeps its `CapacityProviderConfig`. Contributed by @iot-rocket.
+- **AppConfig — hosted configuration version numbers are never reused** — a version created after a delete, and the replacement CloudFormation makes for a changed `HostedConfigurationVersion`, get the next unused number instead of overwriting the live one, and a `LatestVersionNumber` mismatch reports the AWS message.
+- **CloudFormation — a deleted `AWS::AppConfig::Deployment` stays in `ListDeployments`** — the stack delete removed the deployment, so a replaced deployment left the environment's history and the next one reused its number; it now stays until its environment is deleted, which, through the API or with its stack, also removes its deployments and their tags.
- **CloudFormation — `AWS::ECS::Service` stores `NetworkConfiguration` and `LoadBalancers` in the ECS API shape** — `DescribeServices` returns them in camelCase on create and update, and a service from a template registers its tasks in its target groups.
- **SESv2 — `CreateEmailIdentity`/`GetEmailIdentity` return Easy DKIM tokens for a DOMAIN identity** — a domain identity created without `DkimSigningAttributes` answered an empty `Tokens` list with `Status: NOT_STARTED`. AWS provides a set of DKIM tokens for its CNAME records in that case (Easy DKIM), so the Terraform `aws_sesv2_email_identity` resource's `dkim_signing_attributes[0].tokens` indexing failed. A DOMAIN identity now gets three tokens, `SigningAttributesOrigin: AWS_SES` and `Status: PENDING`, unless `DkimSigningAttributes` brings its own key (BYODKIM); EMAIL_ADDRESS identities are unchanged.
diff --git a/ministack/services/appconfig.py b/ministack/services/appconfig.py
index 0aad8deb..7d996f83 100644
--- a/ministack/services/appconfig.py
+++ b/ministack/services/appconfig.py
@@ -50,6 +50,7 @@ _applications = AccountRegionScopedDict()
_environments = AccountRegionScopedDict() # "{app_id}/{env_id}" -> record
_config_profiles = AccountRegionScopedDict() # "{app_id}/{profile_id}" -> record
_hosted_versions = AccountRegionScopedDict() # "{app_id}/{profile_id}/{version}" -> record
+_hosted_version_counters = AccountRegionScopedDict() # "{app_id}/{profile_id}" -> last issued version
_deployment_strategies = AccountRegionScopedDict()
_deployments = AccountRegionScopedDict() # "{app_id}/{env_id}/{deploy_num}" -> record
_tags = AccountRegionScopedDict() # arn -> {key: value}
@@ -66,6 +67,7 @@ def get_state():
"environments": _environments,
"config_profiles": _config_profiles,
"hosted_versions": _hosted_versions,
+ "hosted_version_counters": _hosted_version_counters,
"deployment_strategies": _deployment_strategies,
"deployments": _deployments,
"tags": _tags,
@@ -81,6 +83,7 @@ def _restore_state(data):
_environments.update(data.get("environments", {}))
_config_profiles.update(data.get("config_profiles", {}))
_hosted_versions.update(data.get("hosted_versions", {}))
+ _hosted_version_counters.update(data.get("hosted_version_counters", {}))
_deployment_strategies.update(data.get("deployment_strategies", {}))
_deployments.update(data.get("deployments", {}))
_tags.update(data.get("tags", {}))
@@ -201,6 +204,13 @@ def _update_application(app_id, body):
return _json(200, app)
+def _drop_deployments(prefix):
+ """Drop the deployments under an application or environment key prefix, with their tags."""
+ for key in [k for k in _deployments if k.startswith(prefix)]:
+ del _deployments[key]
+ _tags.pop(_deployment_arn(*key.split("/")), None)
+
+
def _delete_application(app_id):
if app_id not in _applications:
return _error(404, "ResourceNotFoundException", f"Application {app_id} not found")
@@ -215,9 +225,10 @@ def _delete_application(app_id):
keys_to_remove = [k for k in _hosted_versions if k.startswith(f"{app_id}/")]
for k in keys_to_remove:
_hosted_versions.pop(k, None)
- keys_to_remove = [k for k in _deployments if k.startswith(f"{app_id}/")]
+ keys_to_remove = [k for k in _hosted_version_counters if k.startswith(f"{app_id}/")]
for k in keys_to_remove:
- _deployments.pop(k, None)
+ _hosted_version_counters.pop(k, None)
+ _drop_deployments(f"{app_id}/")
return _json(204, {})
@@ -281,6 +292,7 @@ def _delete_environment(app_id, env_id):
return _error(404, "ResourceNotFoundException", f"Environment {env_id} not found")
del _environments[key]
_tags.pop(_env_arn(app_id, env_id), None)
+ _drop_deployments(f"{key}/")
return _json(204, {})
@@ -343,6 +355,7 @@ def _delete_configuration_profile(app_id, profile_id):
if key not in _config_profiles:
return _error(404, "ResourceNotFoundException", f"Configuration profile {profile_id} not found")
del _config_profiles[key]
+ _hosted_version_counters.pop(key, None)
_tags.pop(_profile_arn(app_id, profile_id), None)
keys_to_remove = [k for k in _hosted_versions if k.startswith(f"{app_id}/{profile_id}/")]
for k in keys_to_remove:
@@ -355,15 +368,27 @@ def _delete_configuration_profile(app_id, profile_id):
# ---------------------------------------------------------------------------
+def _latest_hosted_version_number(app_id, profile_id):
+ """The highest existing version number of a profile, 0 when it has none."""
+ prefix = f"{app_id}/{profile_id}/"
+ return max((v["VersionNumber"] for k, v in _hosted_versions.items()
+ if k.startswith(prefix)), default=0)
+
+
+def _next_hosted_version_number(app_id, profile_id):
+ """Issue a profile's next version number; a deleted number is never reused."""
+ key = f"{app_id}/{profile_id}"
+ number = max(_hosted_version_counters.get(key, 0),
+ _latest_hosted_version_number(app_id, profile_id)) + 1
+ _hosted_version_counters[key] = number
+ return number
+
+
def _create_hosted_configuration_version(app_id, profile_id, body, content_type):
if f"{app_id}/{profile_id}" not in _config_profiles:
return _error(404, "ResourceNotFoundException", f"Configuration profile {profile_id} not found")
- existing = [
- v for k, v in _hosted_versions.items()
- if k.startswith(f"{app_id}/{profile_id}/")
- ]
- version_number = len(existing) + 1
+ version_number = _next_hosted_version_number(app_id, profile_id)
record = {
"ApplicationId": app_id,
@@ -980,6 +1005,7 @@ def reset():
_environments.clear()
_config_profiles.clear()
_hosted_versions.clear()
+ _hosted_version_counters.clear()
_deployment_strategies.clear()
_deployments.clear()
_tags.clear()
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 8ff360eb..534d212b 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -2736,6 +2736,7 @@ def _appconfig_application_update(physical_id, old_props, new_props, stack_name,
def _appconfig_application_delete(physical_id, props):
_appconfig._applications.pop(physical_id, None)
_appconfig._tags.pop(_appconfig._app_arn(physical_id), None)
+ _appconfig._drop_deployments(f"{physical_id}/")
# --- AppConfig Environment ---
@@ -2797,6 +2798,7 @@ def _appconfig_environment_delete(physical_id, props):
app_id = props.get("ApplicationId", "")
_appconfig._environments.pop(f"{app_id}/{physical_id}", None)
_appconfig._tags.pop(_appconfig._env_arn(app_id, physical_id), None)
+ _appconfig._drop_deployments(f"{app_id}/{physical_id}/")
# --- AppConfig ConfigurationProfile ---
@@ -2873,6 +2875,7 @@ def _appconfig_configuration_profile_update(physical_id, old_props, new_props,
def _appconfig_configuration_profile_delete(physical_id, props):
app_id = props.get("ApplicationId", "")
_appconfig._config_profiles.pop(f"{app_id}/{physical_id}", None)
+ _appconfig._hosted_version_counters.pop(f"{app_id}/{physical_id}", None)
_appconfig._tags.pop(_appconfig._profile_arn(app_id, physical_id), None)
@@ -2890,20 +2893,16 @@ def _appconfig_hosted_version_create(logical_id, props, stack_name):
# CDK / Fn::ToJsonString may pass parsed JSON; AWS wire shape is a string.
if isinstance(content, (dict, list)):
content = json.dumps(content)
- existing = [
- v for k, v in _appconfig._hosted_versions.items()
- if k.startswith(f"{app_id}/{profile_id}/")
- ]
- version_number = len(existing) + 1
# AWS optimistic-concurrency: if LatestVersionNumber is supplied, it must
- # match the most-recent version_number — otherwise reject with a
+ # match the most-recent existing version — otherwise reject with a
# ConflictException-shape error (mirrors real AppConfig's lock check).
+ latest = _appconfig._latest_hosted_version_number(app_id, profile_id)
latest_lock = props.get("LatestVersionNumber")
- if latest_lock is not None and int(latest_lock) != version_number - 1:
+ if latest_lock is not None and int(latest_lock) != latest:
raise ValueError(
- f"AWS::AppConfig::HostedConfigurationVersion LatestVersionNumber "
- f"mismatch: supplied {latest_lock}, current latest is {version_number - 1}"
+ f"Expected latest version {latest_lock} does not match actual latest version {latest}"
)
+ version_number = _appconfig._next_hosted_version_number(app_id, profile_id)
_appconfig._hosted_versions[f"{app_id}/{profile_id}/{version_number}"] = {
"ApplicationId": app_id,
"ConfigurationProfileId": profile_id,
@@ -3038,18 +3037,6 @@ def _appconfig_deployment_create(logical_id, props, stack_name):
return str(deploy_num), {"DeploymentNumber": deploy_num, "State": "COMPLETE"}
-def _appconfig_deployment_delete(physical_id, props):
- app_id = props.get("ApplicationId", "")
- env_id = props.get("EnvironmentId", "")
- _appconfig._deployments.pop(f"{app_id}/{env_id}/{physical_id}", None)
- deploy_arn = (
- f"arn:aws:appconfig:{_appconfig.get_region()}:"
- f"{_appconfig.get_account_id()}:application/{app_id}/"
- f"environment/{env_id}/deployment/{physical_id}"
- )
- _appconfig._tags.pop(deploy_arn, None)
-
-
# --- CloudWatch Logs LogGroup ---
def _cwlogs_create(logical_id, props, stack_name):
@@ -12133,7 +12120,8 @@ _RESOURCE_HANDLERS = {
},
"AWS::AppConfig::Deployment": {
"create": _appconfig_deployment_create,
- "delete": _appconfig_deployment_delete,
+ # A deleted deployment stays in ListDeployments, and its number stays taken.
+ "delete": _cfn_noop_delete,
},
"AWS::Logs::LogGroup": {
"create": _cwlogs_create,
diff --git a/tests/test_appconfig.py b/tests/test_appconfig.py
index ad7e73bd..a8f97849 100644
--- a/tests/test_appconfig.py
+++ b/tests/test_appconfig.py
@@ -140,6 +140,28 @@ def test_appconfig_delete_environment(appconfig_client):
assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
+def test_appconfig_delete_environment_drops_its_deployments(appconfig_client):
+ """A deployment of a deleted environment is not found."""
+ app = appconfig_client.create_application(Name="env-delete-deploy-app")
+ env = appconfig_client.create_environment(ApplicationId=app["Id"], Name="to-delete")
+ profile = appconfig_client.create_configuration_profile(
+ ApplicationId=app["Id"], Name="env-delete-deploy-profile", LocationUri="hosted")
+ appconfig_client.create_hosted_configuration_version(
+ ApplicationId=app["Id"], ConfigurationProfileId=profile["Id"],
+ Content=b"config", ContentType="text/plain")
+ strategy = appconfig_client.create_deployment_strategy(
+ Name="env-delete-deploy-strat", DeploymentDurationInMinutes=0,
+ GrowthFactor=100.0, ReplicateTo="NONE")
+ ids = {"ApplicationId": app["Id"], "EnvironmentId": env["Id"]}
+ number = appconfig_client.start_deployment(
+ **ids, DeploymentStrategyId=strategy["Id"], ConfigurationProfileId=profile["Id"],
+ ConfigurationVersion="1")["DeploymentNumber"]
+ appconfig_client.delete_environment(**ids)
+ with pytest.raises(ClientError) as exc:
+ appconfig_client.get_deployment(**ids, DeploymentNumber=number)
+ assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
+
+
# ---------------------------------------------------------------------------
# Configuration Profiles
# ---------------------------------------------------------------------------
@@ -307,6 +329,28 @@ def test_appconfig_delete_hosted_configuration_version(appconfig_client):
assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
+def test_appconfig_hosted_version_number_is_never_reused(appconfig_client):
+ """A version created after deleting one gets a fresh number."""
+ app = appconfig_client.create_application(Name="hcv-reuse-app")
+ ids = {"ApplicationId": app["Id"],
+ "ConfigurationProfileId": appconfig_client.create_configuration_profile(
+ ApplicationId=app["Id"], Name="hcv-reuse-profile", LocationUri="hosted")["Id"]}
+
+ def create(content):
+ return appconfig_client.create_hosted_configuration_version(
+ **ids, Content=content, ContentType="text/plain")["VersionNumber"]
+
+ assert [create(b"v1"), create(b"v2")] == [1, 2]
+ appconfig_client.delete_hosted_configuration_version(**ids, VersionNumber=1)
+ assert create(b"v3") == 3
+ appconfig_client.delete_hosted_configuration_version(**ids, VersionNumber=3)
+ assert create(b"v4") == 4
+ listed = appconfig_client.list_hosted_configuration_versions(**ids)["Items"]
+ assert sorted(i["VersionNumber"] for i in listed) == [2, 4]
+ assert appconfig_client.get_hosted_configuration_version(
+ **ids, VersionNumber=2)["Content"].read() == b"v2"
+
+
# ---------------------------------------------------------------------------
# Deployment Strategies
# ---------------------------------------------------------------------------
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 879678bd..a4b0703d 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -26122,6 +26122,158 @@ def test_cfn_appconfig_deployment_strategy_update_and_rename(cfn, appconfig_clie
_delete_cfn_test_stack(cfn, f"cfn-ac-strat-{suffix}")
+def test_cfn_appconfig_hosted_version_replacement_takes_a_new_number(cfn, appconfig_client):
+ """Every replacement is a new version number, a rolled-back one included."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"cfn-ac-hcv-{suffix}"
+
+ def template(content):
+ return json.dumps({
+ "Resources": {
+ "App": {"Type": "AWS::AppConfig::Application",
+ "Properties": {"Name": stack_name}},
+ "Profile": {"Type": "AWS::AppConfig::ConfigurationProfile", "Properties": {
+ "ApplicationId": {"Ref": "App"}, "Name": stack_name,
+ "LocationUri": "hosted"}},
+ "HCV": {"Type": "AWS::AppConfig::HostedConfigurationVersion", "Properties": {
+ "ApplicationId": {"Ref": "App"},
+ "ConfigurationProfileId": {"Ref": "Profile"},
+ "ContentType": "text/plain", "Content": content}},
+ },
+ "Outputs": {"AppId": {"Value": {"Ref": "App"}},
+ "ProfileId": {"Value": {"Ref": "Profile"}},
+ "Version": {"Value": {"Ref": "HCV"}}},
+ })
+
+ def update(body):
+ cfn.update_stack(StackName=stack_name, TemplateBody=body)
+ return _wait_stack(cfn, stack_name)["StackStatus"]
+
+ def versions():
+ ids = {"ApplicationId": _cfn_output(cfn, stack_name, "AppId"),
+ "ConfigurationProfileId": _cfn_output(cfn, stack_name, "ProfileId")}
+ version = int(_cfn_output(cfn, stack_name, "Version"))
+ content = appconfig_client.get_hosted_configuration_version(
+ **ids, VersionNumber=version)["Content"].read()
+ listed = appconfig_client.list_hosted_configuration_versions(**ids)["Items"]
+ return version, content, [i["VersionNumber"] for i in listed]
+
+ cfn.create_stack(StackName=stack_name, TemplateBody=template("v1"))
+ try:
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "CREATE_COMPLETE"
+ assert versions() == (1, b"v1", [1])
+ assert update(template("v2")) == "UPDATE_COMPLETE"
+ assert versions() == (2, b"v2", [2])
+ assert update(template("v3")) == "UPDATE_COMPLETE"
+ assert versions() == (3, b"v3", [3]), "the replacement overwrote the live version"
+ assert update(_cfn_with_failing_resource(template("v4"), "HCV")) == "UPDATE_ROLLBACK_COMPLETE"
+ assert versions() == (3, b"v3", [3])
+ assert update(template("v5")) == "UPDATE_COMPLETE"
+ assert versions() == (5, b"v5", [5])
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_appconfig_hosted_version_lock_checks_the_latest_existing_version(cfn, appconfig_client):
+ """LatestVersionNumber must name the highest existing version, not the last issued."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ app = appconfig_client.create_application(Name=f"cfn-ac-lock-{suffix}")
+ ids = {"ApplicationId": app["Id"],
+ "ConfigurationProfileId": appconfig_client.create_configuration_profile(
+ ApplicationId=app["Id"], Name=f"cfn-ac-lock-{suffix}", LocationUri="hosted")["Id"]}
+ for content in (b"v1", b"v2", b"v3"):
+ appconfig_client.create_hosted_configuration_version(
+ **ids, Content=content, ContentType="text/plain")
+ for number in (3, 1):
+ appconfig_client.delete_hosted_configuration_version(**ids, VersionNumber=number)
+
+ def create(lock):
+ stack_name = f"cfn-ac-lock{lock}-{suffix}"
+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps({"Resources": {"HCV": {
+ "Type": "AWS::AppConfig::HostedConfigurationVersion", "Properties": {
+ **ids, "ContentType": "text/plain", "Content": "locked",
+ "LatestVersionNumber": lock}}}}))
+ return stack_name, _wait_stack(cfn, stack_name)["StackStatus"]
+
+ stacks = []
+ try:
+ for lock, status in ((3, "ROLLBACK_COMPLETE"), (2, "CREATE_COMPLETE")):
+ stack_name, actual = create(lock)
+ stacks.append(stack_name)
+ assert actual == status
+ listed = appconfig_client.list_hosted_configuration_versions(**ids)["Items"]
+ assert sorted(i["VersionNumber"] for i in listed) == [2, 4]
+ finally:
+ for stack_name in stacks:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_appconfig_deployment_replacement_keeps_the_old_deployment(cfn, appconfig_client):
+ """A replaced deployment stays listed until its environment is deleted with the stack."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"cfn-ac-dep-{suffix}"
+
+ def template(description):
+ return json.dumps({
+ "Resources": {
+ "App": {"Type": "AWS::AppConfig::Application",
+ "Properties": {"Name": stack_name}},
+ "Env": {"Type": "AWS::AppConfig::Environment", "Properties": {
+ "ApplicationId": {"Ref": "App"}, "Name": stack_name}},
+ "Profile": {"Type": "AWS::AppConfig::ConfigurationProfile", "Properties": {
+ "ApplicationId": {"Ref": "App"}, "Name": stack_name,
+ "LocationUri": "hosted"}},
+ "Strategy": {"Type": "AWS::AppConfig::DeploymentStrategy", "Properties": {
+ "Name": stack_name, "DeploymentDurationInMinutes": 0,
+ "FinalBakeTimeInMinutes": 0, "GrowthFactor": 100,
+ "ReplicateTo": "NONE"}},
+ "HCV": {"Type": "AWS::AppConfig::HostedConfigurationVersion", "Properties": {
+ "ApplicationId": {"Ref": "App"},
+ "ConfigurationProfileId": {"Ref": "Profile"},
+ "ContentType": "application/json", "Content": "{}"}},
+ "Deployment": {"Type": "AWS::AppConfig::Deployment", "Properties": {
+ "ApplicationId": {"Ref": "App"}, "EnvironmentId": {"Ref": "Env"},
+ "ConfigurationProfileId": {"Ref": "Profile"},
+ "DeploymentStrategyId": {"Ref": "Strategy"},
+ "ConfigurationVersion": {"Ref": "HCV"},
+ "Description": description, "Tags": [{"Key": "k", "Value": "v"}]}},
+ },
+ "Outputs": {"AppId": {"Value": {"Ref": "App"}},
+ "EnvId": {"Value": {"Ref": "Env"}},
+ "Number": {"Value": {"Fn::GetAtt": ["Deployment", "DeploymentNumber"]}}},
+ })
+
+ def deployments():
+ listed = appconfig_client.list_deployments(
+ ApplicationId=_cfn_output(cfn, stack_name, "AppId"),
+ EnvironmentId=_cfn_output(cfn, stack_name, "EnvId"))["Items"]
+ return (int(_cfn_output(cfn, stack_name, "Number")),
+ sorted((i["DeploymentNumber"], i["State"]) for i in listed))
+
+ cfn.create_stack(StackName=stack_name, TemplateBody=template("d1"))
+ try:
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "CREATE_COMPLETE"
+ assert deployments() == (1, [(1, "COMPLETE")])
+ for number, description in ((2, "d2"), (3, "d3")):
+ cfn.update_stack(StackName=stack_name, TemplateBody=template(description))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "UPDATE_COMPLETE"
+ assert deployments() == (
+ number, [(n, "COMPLETE") for n in range(1, number + 1)])
+ ids = {"ApplicationId": _cfn_output(cfn, stack_name, "AppId"),
+ "EnvironmentId": _cfn_output(cfn, stack_name, "EnvId")}
+ arn = (f"arn:aws:appconfig:us-east-1:000000000000:application/{ids['ApplicationId']}/"
+ f"environment/{ids['EnvironmentId']}/deployment/3")
+ assert appconfig_client.list_tags_for_resource(ResourceArn=arn)["Tags"]["k"] == "v"
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+ for call in (lambda: appconfig_client.get_deployment(**ids, DeploymentNumber=3),
+ lambda: appconfig_client.stop_deployment(**ids, DeploymentNumber=3),
+ lambda: appconfig_client.list_tags_for_resource(ResourceArn=arn)):
+ with pytest.raises(ClientError) as exc:
+ call()
+ assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
+
+
def test_cfn_appsync_api_update_keeps_id_and_children(cfn, appsync):
"""A GraphQL API update keeps its id and its data sources."""
suffix = _uuid_mod.uuid4().hex[:8]