Skip to content

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

fix(cloudformation): update AWS::EC2::VPCGatewayAttachment in place (#1902)

iot-rocketc3a86a92c132
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 969209f5..1b0e4e5b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### 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.
- **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.
- **API Gateway v2 (HTTP API) — a missing identity source is a 401 without caching too** — a REQUEST authorizer's short circuit for a declared identity source missing from the request only fired with `authorizerResultTtlInSeconds` caching on; with caching off the authorizer was invoked. AWS answers `401 {"message":"Unauthorized"}` (compact JSON) without invoking it either way, as observed on a deployed HTTP API with a TTL of 0. `$context.*` identity sources, which MiniStack does not model, do not count as missing without caching.
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 2f0bbde0..13fdd614 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -7332,22 +7332,44 @@ def _ec2_igw_delete(physical_id, props):
_ec2._tags.pop(physical_id, None)
-def _ec2_vpc_gw_attach_create(logical_id, props, stack_name):
+def _ec2_vpc_gw_attachment(props):
+ """(gateway record, attachment, physical id) of an attachment's properties."""
vpc_id = props.get("VpcId", "")
- igw_id = props.get("InternetGatewayId", "")
- igw = _ec2._internet_gateways.get(igw_id)
- if igw:
- igw["Attachments"] = [{"VpcId": vpc_id, "State": "available"}]
- physical_id = f"{igw_id}|{vpc_id}"
+ if props.get("VpnGatewayId"):
+ return (_ec2._vpn_gateways.get(props["VpnGatewayId"]),
+ {"VpcId": vpc_id, "State": "attached"}, f"VGW|{vpc_id}")
+ return (_ec2._internet_gateways.get(props.get("InternetGatewayId", "")),
+ {"VpcId": vpc_id, "State": "available"}, f"IGW|{vpc_id}")
+
+
+def _ec2_vpc_gw_attach_create(logical_id, props, stack_name):
+ """Attach the gateway; a replaced attachment stays until its own delete."""
+ gateway, attachment, physical_id = _ec2_vpc_gw_attachment(props)
+ if gateway:
+ _ec2_vpc_gw_attach_delete(physical_id, props)
+ gateway["Attachments"].append(attachment)
+ return physical_id, {}
+
+
+def _ec2_vpc_gw_attach_update(physical_id, old_props, new_props, stack_name, logical_id=None):
+ """Swap the gateway in place; a changed VpcId or gateway type is a replacement."""
+ replaced = _rename_replacement(
+ physical_id, old_props, new_props, stack_name, logical_id,
+ _ec2_vpc_gw_attachment(new_props)[2], _ec2_vpc_gw_attachment(old_props)[2],
+ _ec2_vpc_gw_attach_create, _ec2_vpc_gw_attach_delete,
+ )
+ if replaced is not None:
+ return replaced
+ _ec2_vpc_gw_attach_delete(physical_id, old_props)
+ _ec2_vpc_gw_attach_create(logical_id or physical_id, new_props, stack_name)
return physical_id, {}
def _ec2_vpc_gw_attach_delete(physical_id, props):
- parts = physical_id.split("|")
- if len(parts) == 2:
- igw = _ec2._internet_gateways.get(parts[0])
- if igw:
- igw["Attachments"] = []
+ gateway, attachment, _ = _ec2_vpc_gw_attachment(props)
+ if gateway:
+ gateway["Attachments"] = [a for a in gateway.get("Attachments", [])
+ if a.get("VpcId") != attachment["VpcId"]]
def _ec2_rtb_create(logical_id, props, stack_name):
@@ -11707,7 +11729,12 @@ _RESOURCE_HANDLERS = {
"update_with_logical_id": True,
"delete": _ec2_igw_delete,
},
- "AWS::EC2::VPCGatewayAttachment": {"create": _ec2_vpc_gw_attach_create, "delete": _ec2_vpc_gw_attach_delete},
+ "AWS::EC2::VPCGatewayAttachment": {
+ "create": _ec2_vpc_gw_attach_create,
+ "update": _ec2_vpc_gw_attach_update,
+ "update_with_logical_id": True,
+ "delete": _ec2_vpc_gw_attach_delete,
+ },
"AWS::EC2::RouteTable": {
"create": _ec2_rtb_create,
"update": _ec2_rtb_update,
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index f5ee5c9c..e551ab34 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -27412,6 +27412,141 @@ def test_cfn_ec2_internet_gateway_tag_change_is_rolled_back(cfn, ec2):
_delete_cfn_test_stack(cfn, stack_name)
+def _cfn_gateway_attachment_template(gateway, vpc="Vpc", failing=False):
+ """Two VPCs, two internet gateways and an attachment of ``gateway`` to ``vpc``."""
+ template = {
+ "Parameters": {"Vgw1": {"Type": "String", "Default": ""},
+ "Vgw2": {"Type": "String", "Default": ""}},
+ "Resources": {
+ "Vpc": {"Type": "AWS::EC2::VPC", "Properties": {"CidrBlock": "10.53.0.0/16"}},
+ "Vpc2": {"Type": "AWS::EC2::VPC", "Properties": {"CidrBlock": "10.54.0.0/16"}},
+ "Igw1": {"Type": "AWS::EC2::InternetGateway"},
+ "Igw2": {"Type": "AWS::EC2::InternetGateway"},
+ "Attach": {"Type": "AWS::EC2::VPCGatewayAttachment", "Properties": {
+ "VpcId": {"Ref": vpc}, gateway[0]: {"Ref": gateway[1]}}},
+ },
+ "Outputs": {k: {"Value": {"Ref": k}} for k in ("Vpc", "Vpc2", "Igw1", "Igw2", "Attach")},
+ }
+ body = json.dumps(template)
+ return _cfn_with_failing_resource(body, "Attach") if failing else body
+
+
+def _cfn_igw_vpcs(ec2, igw_id):
+ return [a["VpcId"] for a in ec2.describe_internet_gateways(
+ InternetGatewayIds=[igw_id])["InternetGateways"][0].get("Attachments", [])]
+
+
+def test_cfn_ec2_gateway_attachment_swaps_the_internet_gateway_in_place(cfn, ec2):
+ """A new InternetGatewayId moves the attachment under the same IGW|vpc id."""
+ stack_name = f"cfn-gwa-igw-{_uuid_mod.uuid4().hex[:8]}"
+ try:
+ cfn.create_stack(StackName=stack_name,
+ TemplateBody=_cfn_gateway_attachment_template(("InternetGatewayId", "Igw1")))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "CREATE_COMPLETE"
+ vpc, igw1, igw2 = (_cfn_output(cfn, stack_name, k) for k in ("Vpc", "Igw1", "Igw2"))
+ assert _cfn_output(cfn, stack_name, "Attach") == f"IGW|{vpc}"
+
+ cfn.update_stack(StackName=stack_name,
+ TemplateBody=_cfn_gateway_attachment_template(("InternetGatewayId", "Igw2")))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "UPDATE_COMPLETE"
+ assert _cfn_output(cfn, stack_name, "Attach") == f"IGW|{vpc}"
+ assert _cfn_igw_vpcs(ec2, igw1) == []
+ assert _cfn_igw_vpcs(ec2, igw2) == [vpc]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_ec2_gateway_attachment_attaches_and_swaps_a_vpn_gateway(cfn, ec2):
+ """VpnGatewayId attaches the virtual private gateway; a new one swaps in place."""
+ stack_name = f"cfn-gwa-vgw-{_uuid_mod.uuid4().hex[:8]}"
+ vgws = [ec2.create_vpn_gateway(Type="ipsec.1")["VpnGateway"]["VpnGatewayId"] for _ in range(2)]
+ params = [{"ParameterKey": f"Vgw{i}", "ParameterValue": v} for i, v in enumerate(vgws, 1)]
+
+ def vpcs(vgw):
+ return [a["VpcId"] for a in ec2.describe_vpn_gateways(
+ VpnGatewayIds=[vgw])["VpnGateways"][0].get("VpcAttachments", [])
+ if a["State"] == "attached"]
+
+ try:
+ cfn.create_stack(StackName=stack_name, Parameters=params,
+ TemplateBody=_cfn_gateway_attachment_template(("VpnGatewayId", "Vgw1")))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "CREATE_COMPLETE"
+ vpc = _cfn_output(cfn, stack_name, "Vpc")
+ assert _cfn_output(cfn, stack_name, "Attach") == f"VGW|{vpc}"
+ assert vpcs(vgws[0]) == [vpc]
+
+ cfn.update_stack(StackName=stack_name, Parameters=params,
+ TemplateBody=_cfn_gateway_attachment_template(("VpnGatewayId", "Vgw2")))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "UPDATE_COMPLETE"
+ assert _cfn_output(cfn, stack_name, "Attach") == f"VGW|{vpc}"
+ assert vpcs(vgws[0]) == []
+ assert vpcs(vgws[1]) == [vpc]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+ for vgw in vgws:
+ ec2.delete_vpn_gateway(VpnGatewayId=vgw)
+
+
+def test_cfn_ec2_gateway_attachment_vpc_change_keeps_the_gateway_attached(cfn, ec2):
+ """A VpcId change replaces the attachment and leaves the gateway on the new VPC."""
+ stack_name = f"cfn-gwa-vpc-{_uuid_mod.uuid4().hex[:8]}"
+ try:
+ cfn.create_stack(StackName=stack_name,
+ TemplateBody=_cfn_gateway_attachment_template(("InternetGatewayId", "Igw1")))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "CREATE_COMPLETE"
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=_cfn_gateway_attachment_template(
+ ("InternetGatewayId", "Igw1"), vpc="Vpc2"))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "UPDATE_COMPLETE"
+ vpc2, igw1 = (_cfn_output(cfn, stack_name, k) for k in ("Vpc2", "Igw1"))
+ assert _cfn_output(cfn, stack_name, "Attach") == f"IGW|{vpc2}"
+ assert _cfn_igw_vpcs(ec2, igw1) == [vpc2]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_ec2_gateway_attachment_vpc_change_is_rolled_back(cfn, ec2):
+ """A rolled-back VpcId change leaves the gateway attached to the old VPC."""
+ stack_name = f"cfn-gwa-vrb-{_uuid_mod.uuid4().hex[:8]}"
+ try:
+ cfn.create_stack(StackName=stack_name,
+ TemplateBody=_cfn_gateway_attachment_template(("InternetGatewayId", "Igw1")))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "CREATE_COMPLETE"
+ vpc, igw1 = (_cfn_output(cfn, stack_name, k) for k in ("Vpc", "Igw1"))
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=_cfn_gateway_attachment_template(
+ ("InternetGatewayId", "Igw1"), vpc="Vpc2", failing=True))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert _cfn_output(cfn, stack_name, "Attach") == f"IGW|{vpc}"
+ assert _cfn_igw_vpcs(ec2, igw1) == [vpc]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_ec2_gateway_attachment_swap_is_rolled_back(cfn, ec2):
+ """A gateway swap is sent back by the rollback under the same id."""
+ stack_name = f"cfn-gwa-rb-{_uuid_mod.uuid4().hex[:8]}"
+ try:
+ cfn.create_stack(StackName=stack_name,
+ TemplateBody=_cfn_gateway_attachment_template(("InternetGatewayId", "Igw1")))
+ assert _wait_stack(cfn, stack_name)["StackStatus"] == "CREATE_COMPLETE"
+ vpc, igw1, igw2 = (_cfn_output(cfn, stack_name, k) for k in ("Vpc", "Igw1", "Igw2"))
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=_cfn_gateway_attachment_template(
+ ("InternetGatewayId", "Igw2"), failing=True))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ events = [e["ResourceStatus"] for e in cfn.describe_stack_events(StackName=stack_name)["StackEvents"]
+ if e["LogicalResourceId"] == "Attach"]
+ assert events[:4] == ["UPDATE_COMPLETE", "UPDATE_IN_PROGRESS"] * 2
+ assert _cfn_output(cfn, stack_name, "Attach") == f"IGW|{vpc}"
+ assert _cfn_igw_vpcs(ec2, igw1) == [vpc]
+ assert _cfn_igw_vpcs(ec2, igw2) == []
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
def test_cfn_ec2_route_table_tag_update_keeps_id_and_routes(cfn, ec2):
"""A tag change keeps the table's id and routes."""
suffix = _uuid_mod.uuid4().hex[:8]