Skip to content

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

Add EC2 IAM instance profile association APIs for Terraform compatibility (#968)

Daniel Nti Boakye904c0a26fd94
diff --git a/README.md b/README.md
index 27dff0d7..82a02460 100644
--- a/README.md
+++ b/README.md
@@ -504,7 +504,7 @@ Unsupported resource types fail with `CREATE_FAILED` (or `ROLLBACK_COMPLETE` if
| **Athena** | StartQueryExecution, GetQueryExecution, GetQueryResults, StopQueryExecution, ListQueryExecutions, BatchGetQueryExecution, CreateWorkGroup, DeleteWorkGroup, GetWorkGroup, ListWorkGroups, UpdateWorkGroup, CreateNamedQuery, DeleteNamedQuery, GetNamedQuery, ListNamedQueries, BatchGetNamedQuery, CreateDataCatalog, GetDataCatalog, ListDataCatalogs, DeleteDataCatalog, UpdateDataCatalog, CreatePreparedStatement, GetPreparedStatement, DeletePreparedStatement, ListPreparedStatements, GetTableMetadata, ListTableMetadata, TagResource, UntagResource, ListTagsForResource | Real SQL via **DuckDB** when installed (`pip install duckdb`), otherwise returns mock results; result pagination; column type metadata |
| **Firehose** | CreateDeliveryStream, DeleteDeliveryStream, DescribeDeliveryStream, ListDeliveryStreams, PutRecord, PutRecordBatch, UpdateDestination, TagDeliveryStream, UntagDeliveryStream, ListTagsForDeliveryStream, StartDeliveryStreamEncryption, StopDeliveryStreamEncryption | S3 destinations write records to the local S3 emulator; all other destination types buffer in-memory; concurrency-safe `UpdateDestination` via `VersionId`; `DeliveryStreamType=KinesisStreamAsSource` consumes records from the source Kinesis stream on `PutRecord` / `PutRecords` and forwards them to the configured S3 destination, honoring `Prefix` and `DeliveryStartTimestamp`; `ProcessingConfiguration.Processors[].Type=Lambda` invoked per-batch with the AWS-shape event (`{invocationId, deliveryStreamArn, region, records:[{recordId, approximateArrivalTimestamp, data}]}`) — `Ok` records ship the transformed data downstream, `Dropped`/`ProcessingFailed` are omitted, Lambda failures pass records through unchanged (best-effort per AWS) |
| **Route53** | CreateHostedZone, GetHostedZone, DeleteHostedZone, ListHostedZones, ListHostedZonesByName, UpdateHostedZoneComment, ChangeResourceRecordSets (CREATE/UPSERT/DELETE), ListResourceRecordSets, GetChange, CreateHealthCheck, GetHealthCheck, DeleteHealthCheck, ListHealthChecks, UpdateHealthCheck, ChangeTagsForResource, ListTagsForResource | REST/XML protocol; SOA + NS records auto-created; CallerReference idempotency; alias records, weighted/failover/latency routing; marker-based pagination |
-| **EC2** | RunInstances, DescribeInstances, DescribeInstanceAttribute, DescribeInstanceTypes, DescribeVpcAttribute, TerminateInstances, StopInstances, StartInstances, RebootInstances, DescribeImages, CreateSecurityGroup, DeleteSecurityGroup, DescribeSecurityGroups, AuthorizeSecurityGroupIngress, RevokeSecurityGroupIngress, AuthorizeSecurityGroupEgress, RevokeSecurityGroupEgress, DescribeSecurityGroupRules, CreateKeyPair, DeleteKeyPair, DescribeKeyPairs, ImportKeyPair, CreateVpc, DeleteVpc, DescribeVpcs, ModifyVpcAttribute, CreateSubnet, DeleteSubnet, DescribeSubnets, ModifySubnetAttribute, CreateInternetGateway, DeleteInternetGateway, DescribeInternetGateways, AttachInternetGateway, DetachInternetGateway, CreateRouteTable, DeleteRouteTable, DescribeRouteTables, AssociateRouteTable, DisassociateRouteTable, ReplaceRouteTableAssociation, CreateRoute, ReplaceRoute, DeleteRoute, CreateNetworkInterface, DeleteNetworkInterface, DescribeNetworkInterfaces, AttachNetworkInterface, DetachNetworkInterface, CreateVpcEndpoint, DeleteVpcEndpoints, DescribeVpcEndpoints, ModifyVpcEndpoint, DescribePrefixLists, DescribeAvailabilityZones, AllocateAddress, ReleaseAddress, AssociateAddress, DisassociateAddress, DescribeAddresses, DescribeAddressesAttribute, CreateTags, DeleteTags, DescribeTags, CreateNatGateway, DescribeNatGateways, DeleteNatGateway, CreateNetworkAcl, DescribeNetworkAcls, DeleteNetworkAcl, CreateNetworkAclEntry, DeleteNetworkAclEntry, ReplaceNetworkAclEntry, ReplaceNetworkAclAssociation, CreateFlowLogs, DescribeFlowLogs, DeleteFlowLogs, CreateVpcPeeringConnection, AcceptVpcPeeringConnection, DescribeVpcPeeringConnections, DeleteVpcPeeringConnection, CreateDhcpOptions, AssociateDhcpOptions, DescribeDhcpOptions, DeleteDhcpOptions, CreateEgressOnlyInternetGateway, DescribeEgressOnlyInternetGateways, DeleteEgressOnlyInternetGateway, CreateManagedPrefixList, DescribeManagedPrefixLists, GetManagedPrefixListEntries, ModifyManagedPrefixList, DeleteManagedPrefixList, CreateVpnGateway, DescribeVpnGateways, AttachVpnGateway, DetachVpnGateway, DeleteVpnGateway, EnableVgwRoutePropagation, DisableVgwRoutePropagation, CreateCustomerGateway, DescribeCustomerGateways, DeleteCustomerGateway, DescribeInstanceCreditSpecifications, DescribeInstanceMaintenanceOptions, DescribeInstanceAutoRecoveryAttribute, ModifyInstanceMaintenanceOptions, DescribeInstanceTopology, DescribeSpotInstanceRequests, DescribeCapacityReservations, DescribeInstanceStatus, DescribeVpcClassicLink, DescribeVpcClassicLinkDnsSupport, CreateLaunchTemplate, CreateLaunchTemplateVersion, DescribeLaunchTemplates, DescribeLaunchTemplateVersions, ModifyLaunchTemplate, DeleteLaunchTemplate, CreateFleet, DescribeFleets | 138 actions; EC2 Fleet (`CreateFleet` / `DescribeFleets`) with `instant`-type synchronous launch and `maintain` / `request` async fulfillment, multi-config × multi-override round-robin capacity distribution, and `DefaultTargetCapacityType`-driven spot/on-demand selection — unblocks Karpenter / Cluster Autoscaler local validation;; `AuthorizeSecurityGroupIngress` is idempotent on duplicate rules (same behavior as egress; avoids Terraform re-apply failures); in-memory state only — no real VMs; CreateVpc provisions per-VPC default route table, network ACL, and security group; full Terraform VPC module v6.6.0 compatible; VPN/Customer gateways, managed prefix lists, VPC endpoints with modify support; launch templates with versioning ($Latest/$Default) |
+| **EC2** | RunInstances, DescribeInstances, DescribeInstanceAttribute, DescribeInstanceTypes, DescribeVpcAttribute, TerminateInstances, StopInstances, StartInstances, RebootInstances, AssociateIamInstanceProfile, DescribeIamInstanceProfileAssociations, DisassociateIamInstanceProfile, ReplaceIamInstanceProfileAssociation, DescribeImages, CreateSecurityGroup, DeleteSecurityGroup, DescribeSecurityGroups, AuthorizeSecurityGroupIngress, RevokeSecurityGroupIngress, AuthorizeSecurityGroupEgress, RevokeSecurityGroupEgress, DescribeSecurityGroupRules, CreateKeyPair, DeleteKeyPair, DescribeKeyPairs, ImportKeyPair, CreateVpc, DeleteVpc, DescribeVpcs, ModifyVpcAttribute, CreateSubnet, DeleteSubnet, DescribeSubnets, ModifySubnetAttribute, CreateInternetGateway, DeleteInternetGateway, DescribeInternetGateways, AttachInternetGateway, DetachInternetGateway, CreateRouteTable, DeleteRouteTable, DescribeRouteTables, AssociateRouteTable, DisassociateRouteTable, ReplaceRouteTableAssociation, CreateRoute, ReplaceRoute, DeleteRoute, CreateNetworkInterface, DeleteNetworkInterface, DescribeNetworkInterfaces, AttachNetworkInterface, DetachNetworkInterface, CreateVpcEndpoint, DeleteVpcEndpoints, DescribeVpcEndpoints, ModifyVpcEndpoint, DescribePrefixLists, DescribeAvailabilityZones, AllocateAddress, ReleaseAddress, AssociateAddress, DisassociateAddress, DescribeAddresses, DescribeAddressesAttribute, CreateTags, DeleteTags, DescribeTags, CreateNatGateway, DescribeNatGateways, DeleteNatGateway, CreateNetworkAcl, DescribeNetworkAcls, DeleteNetworkAcl, CreateNetworkAclEntry, DeleteNetworkAclEntry, ReplaceNetworkAclEntry, ReplaceNetworkAclAssociation, CreateFlowLogs, DescribeFlowLogs, DeleteFlowLogs, CreateVpcPeeringConnection, AcceptVpcPeeringConnection, DescribeVpcPeeringConnections, DeleteVpcPeeringConnection, CreateDhcpOptions, AssociateDhcpOptions, DescribeDhcpOptions, DeleteDhcpOptions, CreateEgressOnlyInternetGateway, DescribeEgressOnlyInternetGateways, DeleteEgressOnlyInternetGateway, CreateManagedPrefixList, DescribeManagedPrefixLists, GetManagedPrefixListEntries, ModifyManagedPrefixList, DeleteManagedPrefixList, CreateVpnGateway, DescribeVpnGateways, AttachVpnGateway, DetachVpnGateway, DeleteVpnGateway, EnableVgwRoutePropagation, DisableVgwRoutePropagation, CreateCustomerGateway, DescribeCustomerGateways, DeleteCustomerGateway, DescribeInstanceCreditSpecifications, DescribeInstanceMaintenanceOptions, DescribeInstanceAutoRecoveryAttribute, ModifyInstanceMaintenanceOptions, DescribeInstanceTopology, DescribeSpotInstanceRequests, DescribeCapacityReservations, DescribeInstanceStatus, DescribeVpcClassicLink, DescribeVpcClassicLinkDnsSupport, CreateLaunchTemplate, CreateLaunchTemplateVersion, DescribeLaunchTemplates, DescribeLaunchTemplateVersions, ModifyLaunchTemplate, DeleteLaunchTemplate, CreateFleet, DescribeFleets | 142 actions; EC2 Fleet (`CreateFleet` / `DescribeFleets`) with `instant`-type synchronous launch and `maintain` / `request` async fulfillment, multi-config × multi-override round-robin capacity distribution, and `DefaultTargetCapacityType`-driven spot/on-demand selection — unblocks Karpenter / Cluster Autoscaler local validation; IAM instance profile association lifecycle is persisted (`Associate*`, `Describe*`, `Replace*`, `Disassociate*`) so Terraform `aws_instance.iam_instance_profile` round-trips without drift; `AuthorizeSecurityGroupIngress` is idempotent on duplicate rules (same behavior as egress; avoids Terraform re-apply failures); in-memory state only — no real VMs; CreateVpc provisions per-VPC default route table, network ACL, and security group; full Terraform VPC module v6.6.0 compatible; VPN/Customer gateways, managed prefix lists, VPC endpoints with modify support; launch templates with versioning ($Latest/$Default) |
| **EBS** | CreateVolume, DeleteVolume, DescribeVolumes, DescribeVolumeStatus, AttachVolume, DetachVolume, ModifyVolume, DescribeVolumesModifications, EnableVolumeIO, ModifyVolumeAttribute, DescribeVolumeAttribute, CreateSnapshot, DeleteSnapshot, DescribeSnapshots, CopySnapshot, ModifySnapshotAttribute, DescribeSnapshotAttribute | Part of EC2 Query/XML service; attach/detach updates volume state; snapshots stored as completed immediately; Pro-only on LocalStack — free here |
| **EFS** | CreateFileSystem, DescribeFileSystems, DeleteFileSystem, UpdateFileSystem, CreateMountTarget, DescribeMountTargets, DeleteMountTarget, DescribeMountTargetSecurityGroups, ModifyMountTargetSecurityGroups, CreateAccessPoint, DescribeAccessPoints, DeleteAccessPoint, TagResource, UntagResource, ListTagsForResource, PutLifecycleConfiguration, DescribeLifecycleConfiguration, PutBackupPolicy, DescribeBackupPolicy, DescribeAccountPreferences, PutAccountPreferences | REST/JSON `/2015-02-01/*`; CreationToken idempotency; FileSystem deletion blocked when mount targets exist; Pro-only on LocalStack — free here |
| **EMR** | RunJobFlow, DescribeCluster, ListClusters, TerminateJobFlows, ModifyCluster, SetTerminationProtection, SetVisibleToAllUsers, AddJobFlowSteps, DescribeStep, ListSteps, CancelSteps, AddInstanceFleet, ListInstanceFleets, ModifyInstanceFleet, AddInstanceGroups, ListInstanceGroups, ModifyInstanceGroups, ListBootstrapActions, AddTags, RemoveTags, GetBlockPublicAccessConfiguration, PutBlockPublicAccessConfiguration | Control plane only — no real Spark/Hadoop; clusters start in WAITING (KeepAlive=true) or TERMINATED (KeepAlive=false); steps stored as COMPLETED immediately; all three instance modes (simple, InstanceGroups, InstanceFleets); TerminationProtected enforced; Pro-only on LocalStack — free here |
diff --git a/ministack/core/router.py b/ministack/core/router.py
index 076f27d0..10918a61 100644
--- a/ministack/core/router.py
+++ b/ministack/core/router.py
@@ -699,6 +699,10 @@ def detect_service(method: str, path: str, headers: dict, query_params: dict) ->
"StopInstances": "ec2",
"StartInstances": "ec2",
"RebootInstances": "ec2",
+ "AssociateIamInstanceProfile": "ec2",
+ "DescribeIamInstanceProfileAssociations": "ec2",
+ "DisassociateIamInstanceProfile": "ec2",
+ "ReplaceIamInstanceProfileAssociation": "ec2",
"DescribeImages": "ec2",
"CreateSecurityGroup": "ec2",
"DeleteSecurityGroup": "ec2",
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.py
index c7cf0dec..19e1ecfe 100644
--- a/ministack/services/ec2.py
+++ b/ministack/services/ec2.py
@@ -4,7 +4,9 @@ Query API (Action=...) — instances exist in memory only, no real VMs launched.
Supports:
Instances: RunInstances, TerminateInstances, DescribeInstances,
- DescribeInstanceStatus, StartInstances, StopInstances, RebootInstances
+ DescribeInstanceStatus, StartInstances, StopInstances, RebootInstances,
+ AssociateIamInstanceProfile, DescribeIamInstanceProfileAssociations,
+ DisassociateIamInstanceProfile, ReplaceIamInstanceProfileAssociation
Images: DescribeImages (stub — returns common AMI IDs)
Security Groups: CreateSecurityGroup, DeleteSecurityGroup, DescribeSecurityGroups,
AuthorizeSecurityGroupIngress, RevokeSecurityGroupIngress,
@@ -104,6 +106,7 @@ _customer_gateways = AccountScopedDict() # cgw_id -> customer gateway record
_vpn_connections = AccountScopedDict() # vpn_id -> VPN connection record
_launch_templates = AccountScopedDict() # lt_id -> launch template record (includes versions list)
_fleets = AccountScopedDict() # fleet_id -> fleet record
+_iam_instance_profile_associations = AccountScopedDict() # assoc_id -> association record
@@ -136,6 +139,7 @@ def get_state():
"vpn_connections": copy.deepcopy(_vpn_connections),
"launch_templates": copy.deepcopy(_launch_templates),
"fleets": copy.deepcopy(_fleets),
+ "iam_instance_profile_associations": copy.deepcopy(_iam_instance_profile_associations),
}
@@ -166,6 +170,9 @@ def restore_state(data):
_vpn_connections.update(data.get("vpn_connections", {}))
_launch_templates.update(data.get("launch_templates", {}))
_fleets.update(data.get("fleets", {}))
+ _iam_instance_profile_associations.update(
+ data.get("iam_instance_profile_associations", {})
+ )
try:
@@ -299,6 +306,145 @@ async def handle_request(method, path, headers, body, query_params):
# Instances
# ---------------------------------------------------------------------------
+def _synthetic_iam_instance_profile_id(seed: str) -> str:
+ digest = hashlib.sha1(seed.encode("utf-8")).hexdigest().upper()[:17]
+ return "AIPA" + digest
+
+
+def _resolve_iam_instance_profile(iam_arn="", iam_name="", allow_missing=False):
+ if not iam_arn and not iam_name:
+ return None, None
+
+ profile = None
+ try:
+ from ministack.services import iam as iam_svc
+
+ profile = iam_svc._lookup_instance_profile(name=iam_name, arn=iam_arn)
+ except Exception:
+ logger.debug("IAM instance profile lookup failed", exc_info=True)
+
+ if profile:
+ return {
+ "Arn": profile["Arn"],
+ "Id": profile["InstanceProfileId"],
+ }, None
+
+ if not allow_missing:
+ if iam_name and not iam_arn:
+ msg = (
+ f"Value ({iam_name}) for parameter iamInstanceProfile.name is invalid. "
+ "Invalid IAM Instance Profile name"
+ )
+ elif iam_arn and not iam_name:
+ msg = (
+ f"Value ({iam_arn}) for parameter iamInstanceProfile.arn is invalid. "
+ "Invalid IAM Instance Profile ARN"
+ )
+ else:
+ msg = f"The IAM instance profile '{iam_name or iam_arn}' does not exist"
+ return None, _error("InvalidParameterValue", msg, 400)
+
+ if not iam_arn and iam_name:
+ iam_arn = f"arn:aws:iam::{get_account_id()}:instance-profile/{iam_name}"
+ seed = iam_arn or iam_name
+ return {
+ "Arn": iam_arn,
+ "Id": _synthetic_iam_instance_profile_id(seed),
+ }, None
+
+
+def _iam_instance_profile_association_id(instance_id, iam_profile):
+ seed = f"{instance_id}:{iam_profile.get('Arn', '')}:{iam_profile.get('Id', '')}"
+ return "iip-assoc-" + hashlib.sha1(seed.encode("utf-8")).hexdigest()[:17]
+
+
+def _find_active_iam_instance_profile_association(instance_id):
+ for assoc in _iam_instance_profile_associations.values():
+ if assoc.get("InstanceId") != instance_id:
+ continue
+ if assoc.get("State") == "associated":
+ return assoc
+ return None
+
+
+def _upsert_iam_instance_profile_association(
+ instance_id, iam_profile, association_id=None, state="associated"
+):
+ inst = _instances.get(instance_id)
+ if not inst:
+ return None
+
+ assoc_id = association_id or _iam_instance_profile_association_id(
+ instance_id, iam_profile
+ )
+ assoc = {
+ "AssociationId": assoc_id,
+ "InstanceId": instance_id,
+ "IamInstanceProfile": dict(iam_profile),
+ "State": state,
+ "Timestamp": _now_ts(),
+ }
+ _iam_instance_profile_associations[assoc_id] = assoc
+ inst["IamInstanceProfile"] = dict(iam_profile)
+ inst["IamInstanceProfileAssociationId"] = assoc_id
+ return assoc
+
+
+def _mark_iam_instance_profile_association_disassociated(assoc):
+ assoc["State"] = "disassociated"
+ assoc["Timestamp"] = _now_ts()
+ inst = _instances.get(assoc["InstanceId"])
+ if not inst:
+ return
+ if inst.get("IamInstanceProfileAssociationId") == assoc["AssociationId"]:
+ inst["IamInstanceProfile"] = None
+ inst.pop("IamInstanceProfileAssociationId", None)
+
+
+def _sync_iam_instance_profile_associations():
+ active_by_instance = {}
+ for assoc in _iam_instance_profile_associations.values():
+ inst = _instances.get(assoc["InstanceId"])
+ if assoc.get("State") != "associated":
+ continue
+ if not inst or inst["State"]["Name"] == "terminated":
+ _mark_iam_instance_profile_association_disassociated(assoc)
+ continue
+ if assoc["InstanceId"] in active_by_instance:
+ _mark_iam_instance_profile_association_disassociated(assoc)
+ continue
+ active_by_instance[assoc["InstanceId"]] = assoc
+ inst["IamInstanceProfile"] = dict(assoc["IamInstanceProfile"])
+ inst["IamInstanceProfileAssociationId"] = assoc["AssociationId"]
+
+ for inst in _instances.values():
+ if inst["State"]["Name"] == "terminated":
+ continue
+ iam_profile = inst.get("IamInstanceProfile")
+ if not iam_profile:
+ continue
+ if inst["InstanceId"] in active_by_instance:
+ continue
+ assoc = _upsert_iam_instance_profile_association(
+ inst["InstanceId"], iam_profile
+ )
+ active_by_instance[inst["InstanceId"]] = assoc
+
+
+def _matches_iam_instance_profile_association_filters(assoc, filters):
+ for name, vals in filters.items():
+ if name == "association-id":
+ if assoc["AssociationId"] not in vals:
+ return False
+ elif name == "instance-id":
+ if assoc["InstanceId"] not in vals:
+ return False
+ elif name == "state":
+ if assoc["State"] not in vals:
+ return False
+ return True
+
+
def _launch_instances_internal(image_id, instance_type, subnet_id, count, key_name="", user_data="", sg_ids=None, requested_private_ip=None, iam_profile=None):
now = _now_ts()
if not sg_ids:
@@ -359,6 +505,7 @@ def _launch_instances_internal(image_id, instance_type, subnet_id, count, key_na
"PublicIpAddress": _random_ip("54."),
"PrivateDnsName": f"ip-{private_ip.replace('.', '-')}.ec2.internal",
"PublicDnsName": f"ec2-{private_ip.replace('.', '-')}.compute-1.amazonaws.com",
+ "SourceDestCheck": True,
"SecurityGroups": [
{"GroupId": sg, "GroupName": _security_groups.get(sg, {}).get("GroupName", sg)}
for sg in sg_ids
@@ -377,6 +524,8 @@ def _launch_instances_internal(image_id, instance_type, subnet_id, count, key_na
"IamInstanceProfile": iam_profile,
}
_instances[instance_id] = inst
+ if iam_profile:
+ _upsert_iam_instance_profile_association(instance_id, iam_profile)
created.append(inst)
return created
@@ -404,12 +553,13 @@ def _run_instances(p):
iam_name = _p(p, "IamInstanceProfile.Name")
iam_profile = None
if iam_arn or iam_name:
- # Real AWS returns both Arn and Id. We synthesize a stable Id from the
- # name/arn so DescribeInstances reads back as it does on AWS.
- if not iam_arn and iam_name:
- iam_arn = f"arn:aws:iam::{get_account_id()}:instance-profile/{iam_name}"
- iam_id = "AIPA" + new_uuid().replace("-", "").upper()[:17]
- iam_profile = {"Arn": iam_arn, "Id": iam_id}
+ iam_profile, err = _resolve_iam_instance_profile(
+ iam_arn=iam_arn,
+ iam_name=iam_name,
+ allow_missing=True,
+ )
+ if err:
+ return err
created = _launch_instances_internal(
image_id=image_id,
@@ -532,8 +682,143 @@ def _describe_instance_status(p):
f"<instanceStatusSet>{items}</instanceStatusSet>")
+def _associate_iam_instance_profile(p):
+ instance_id = _p(p, "InstanceId")
+ if instance_id not in _instances:
+ return _error(
+ "InvalidInstanceID.NotFound",
+ f"The instance ID '{instance_id}' does not exist",
+ 400,
+ )
+
+ iam_profile, err = _resolve_iam_instance_profile(
+ iam_arn=_p(p, "IamInstanceProfile.Arn"),
+ iam_name=_p(p, "IamInstanceProfile.Name"),
+ allow_missing=False,
+ )
+ if err:
+ return err
+ if not iam_profile:
+ return _error("MissingParameter", "IamInstanceProfile is required", 400)
+
+ _sync_iam_instance_profile_associations()
+ assoc = _find_active_iam_instance_profile_association(instance_id)
+ if assoc:
+ if assoc.get("IamInstanceProfile") == iam_profile:
+ return _xml(
+ 200,
+ "AssociateIamInstanceProfileResponse",
+ _iam_instance_profile_association_xml(
+ assoc, tag="iamInstanceProfileAssociation"
+ ),
+ )
+ return _error(
+ "IncorrectState",
+ f"Instance '{instance_id}' already has an IAM instance profile association",
+ 400,
+ )
+
+ assoc = _upsert_iam_instance_profile_association(instance_id, iam_profile)
+ return _xml(
+ 200,
+ "AssociateIamInstanceProfileResponse",
+ _iam_instance_profile_association_xml(
+ assoc, tag="iamInstanceProfileAssociation"
+ ),
+ )
+
+
+def _describe_iam_instance_profile_associations(p):
+ _cleanup_terminated()
+ _sync_iam_instance_profile_associations()
+
+ association_ids = _parse_member_list(p, "AssociationId")
+ filters = _parse_filters(p)
+
+ items = []
+ for assoc in _iam_instance_profile_associations.values():
+ if association_ids and assoc["AssociationId"] not in association_ids:
+ continue
+ if not _matches_iam_instance_profile_association_filters(assoc, filters):
+ continue
+ items.append(_iam_instance_profile_association_xml(assoc))
+
+ return _xml(
+ 200,
+ "DescribeIamInstanceProfileAssociationsResponse",
+ f"<iamInstanceProfileAssociationSet>{''.join(items)}</iamInstanceProfileAssociationSet>",
+ )
+
+
+def _disassociate_iam_instance_profile(p):
+ assoc_id = _p(p, "AssociationId")
+ _sync_iam_instance_profile_associations()
+ assoc = _iam_instance_profile_associations.get(assoc_id)
+ if not assoc:
+ return _error(
+ "InvalidAssociationID.NotFound",
+ f"Association '{assoc_id}' not found",
+ 400,
+ )
+
+ _mark_iam_instance_profile_association_disassociated(assoc)
+ return _xml(
+ 200,
+ "DisassociateIamInstanceProfileResponse",
+ _iam_instance_profile_association_xml(
+ assoc, tag="iamInstanceProfileAssociation"
+ ),
+ )
+
+
+def _replace_iam_instance_profile_association(p):
+ assoc_id = _p(p, "AssociationId")
+ _sync_iam_instance_profile_associations()
+ assoc = _iam_instance_profile_associations.get(assoc_id)
+ if not assoc:
+ return _error(
+ "InvalidAssociationID.NotFound",
+ f"Association '{assoc_id}' not found",
+ 400,
+ )
+
+ instance_id = assoc["InstanceId"]
+ inst = _instances.get(instance_id)
+ if not inst or inst["State"]["Name"] == "terminated":
+ return _error(
+ "InvalidInstanceID.NotFound",
+ f"The instance ID '{instance_id}' does not exist",
+ 400,
+ )
+
+ iam_profile, err = _resolve_iam_instance_profile(
+ iam_arn=_p(p, "IamInstanceProfile.Arn"),
+ iam_name=_p(p, "IamInstanceProfile.Name"),
+ allow_missing=False,
+ )
+ if err:
+ return err
+ if not iam_profile:
+ return _error("MissingParameter", "IamInstanceProfile is required", 400)
+
+ assoc = _upsert_iam_instance_profile_association(
+ instance_id,
+ iam_profile,
+ association_id=assoc_id,
+ state="associated",
+ )
+ return _xml(
+ 200,
+ "ReplaceIamInstanceProfileAssociationResponse",
+ _iam_instance_profile_association_xml(
+ assoc, tag="iamInstanceProfileAssociation"
+ ),
+ )
+
+
def _terminate_instances(p):
ids = _parse_member_list(p, "InstanceId")
+ _sync_iam_instance_profile_associations()
for iid in ids:
if iid not in _instances:
return _error("InvalidInstanceID.NotFound", f"The instance ID '{iid}' does not exist", 400)
@@ -544,6 +829,9 @@ def _terminate_instances(p):
prev = inst["State"].copy()
inst["State"] = {"Code": 48, "Name": "terminated"}
inst["_terminated_at"] = time.time()
+ assoc = _find_active_iam_instance_profile_association(iid)
+ if assoc:
+ _mark_iam_instance_profile_association_disassociated(assoc)
items += f"""<item>
<instanceId>{iid}</instanceId>
<previousState><code>{prev['Code']}</code><name>{prev['Name']}</name></previousState>
@@ -2301,6 +2589,7 @@ def _instance_xml(inst):
<privateIpAddress>{inst['PrivateIpAddress']}</privateIpAddress>
<publicDnsName>{inst['PublicDnsName']}</publicDnsName>
<publicIpAddress>{inst['PublicIpAddress']}</publicIpAddress>
+ <sourceDestCheck>{'true' if inst.get('SourceDestCheck', True) else 'false'}</sourceDestCheck>
<subnetId>{inst['SubnetId']}</subnetId>
<vpcId>{inst['VpcId']}</vpcId>
<architecture>{inst['Architecture']}</architecture>
@@ -2316,19 +2605,35 @@ def _instance_xml(inst):
</item>"""
-def _inst_iam_xml(inst):
- """Emit <iamInstanceProfile> block when an IAM profile is attached."""
- iip = inst.get("IamInstanceProfile")
+def _iam_instance_profile_xml(iip, tag="iamInstanceProfile"):
if not iip or not (iip.get("Arn") or iip.get("Id")):
return ""
+ out = [f"<{tag}>"]
+ if iip.get("Arn"):
+ out.append(f"<arn>{_esc(iip['Arn'])}</arn>")
+ if iip.get("Id"):
+ out.append(f"<id>{_esc(iip['Id'])}</id>")
+ out.append(f"</{tag}>")
+ return "".join(out)
+
+
+def _iam_instance_profile_association_xml(assoc, tag="item"):
return (
- "<iamInstanceProfile>"
- f"<arn>{_esc(iip.get('Arn', ''))}</arn>"
- f"<id>{_esc(iip.get('Id', ''))}</id>"
- "</iamInstanceProfile>"
+ f"<{tag}>"
+ f"<associationId>{_esc(assoc['AssociationId'])}</associationId>"
+ f"<instanceId>{_esc(assoc['InstanceId'])}</instanceId>"
+ f"{_iam_instance_profile_xml(assoc.get('IamInstanceProfile'), tag='iamInstanceProfile')}"
+ f"<state>{_esc(assoc['State'])}</state>"
+ f"<timestamp>{_esc(assoc['Timestamp'])}</timestamp>"
+ f"</{tag}>"
)
+def _inst_iam_xml(inst):
+ """Emit <iamInstanceProfile> block when an IAM profile is attached."""
+ return _iam_instance_profile_xml(inst.get("IamInstanceProfile"))
+
+
def _inst_bdm_xml(inst):
"""Emit the running-instance BlockDeviceMapping shape. Distinct from the
launch-spec shape which carries VolumeSize/VolumeType/Encrypted; the
@@ -4047,6 +4352,7 @@ def reset():
_vpn_connections.clear()
_launch_templates.clear()
_fleets.clear()
+ _iam_instance_profile_associations.clear()
_init_defaults()
@@ -5058,10 +5364,11 @@ def _slot_from_lt_data(spec, lt_data):
iam_arn = lt_iam.get("Arn")
iam_name = lt_iam.get("Name")
if iam_arn or iam_name:
- if not iam_arn and iam_name:
- iam_arn = f"arn:aws:iam::{get_account_id()}:instance-profile/{iam_name}"
- iam_id = "AIPA" + new_uuid().replace("-", "").upper()[:17]
- iam_profile = {"Arn": iam_arn, "Id": iam_id}
+ iam_profile, _ = _resolve_iam_instance_profile(
+ iam_arn=iam_arn,
+ iam_name=iam_name,
+ allow_missing=True,
+ )
return {
"spec": spec or {},
"image_id": (lt_data or {}).get("ImageId") or "ami-00000000",
@@ -5134,6 +5441,10 @@ _ACTION_MAP = {
"StopInstances": _stop_instances,
"StartInstances": _start_instances,
"RebootInstances": _reboot_instances,
+ "AssociateIamInstanceProfile": _associate_iam_instance_profile,
+ "DescribeIamInstanceProfileAssociations": _describe_iam_instance_profile_associations,
+ "DisassociateIamInstanceProfile": _disassociate_iam_instance_profile,
+ "ReplaceIamInstanceProfileAssociation": _replace_iam_instance_profile_association,
"DescribeImages": _describe_images,
"CreateSecurityGroup": _create_security_group,
"DeleteSecurityGroup": _delete_security_group,
diff --git a/ministack/services/iam.py b/ministack/services/iam.py
index aeb2f725..add823a9 100644
--- a/ministack/services/iam.py
+++ b/ministack/services/iam.py
@@ -1060,6 +1060,21 @@ def _get_access_key_last_used(p):
# -------------------- Instance profiles --------------------
+def _lookup_instance_profile(name="", arn=""):
+ if name:
+ profile = _instance_profiles.get(name)
+ if not profile:
+ return None
+ if arn and profile.get("Arn") != arn:
+ return None
+ return profile
+ if arn:
+ for profile in _instance_profiles.values():
+ if profile.get("Arn") == arn:
+ return profile
+ return None
+
+
def _create_instance_profile(p):
name = _p(p, "InstanceProfileName")
if name in _instance_profiles:
diff --git a/tests/test_ec2.py b/tests/test_ec2.py
index cfd46ffb..6b2c93c6 100644
--- a/tests/test_ec2.py
+++ b/tests/test_ec2.py
@@ -10,6 +10,50 @@ import pytest
from botocore.exceptions import ClientError
+def _create_ec2_iam_instance_profile(iam, suffix):
+ assume = json.dumps(
+ {
+ "Version": "2012-10-17",
+ "Statement": [
+ {
+ "Effect": "Allow",
+ "Principal": {"Service": "ec2.amazonaws.com"},
+ "Action": "sts:AssumeRole",
+ }
+ ],
+ }
+ )
+ role_name = f"ec2-ip-role-{suffix}"
+ profile_name = f"ec2-ip-{suffix}"
+ iam.create_role(RoleName=role_name, AssumeRolePolicyDocument=assume)
+ profile = iam.create_instance_profile(
+ InstanceProfileName=profile_name
+ )["InstanceProfile"]
+ iam.add_role_to_instance_profile(
+ InstanceProfileName=profile_name,
+ RoleName=role_name,
+ )
+ return role_name, profile_name, profile
+
+
+def _delete_ec2_iam_instance_profile(iam, role_name, profile_name):
+ try:
+ iam.remove_role_from_instance_profile(
+ InstanceProfileName=profile_name,
+ RoleName=role_name,
+ )
+ except ClientError:
+ pass
+ try:
+ iam.delete_instance_profile(InstanceProfileName=profile_name)
+ except ClientError:
+ pass
+ try:
+ iam.delete_role(RoleName=role_name)
+ except ClientError:
+ pass
+
+
def test_ec2_describe_vpcs_default(ec2):
resp = ec2.describe_vpcs()
vpcs = resp["Vpcs"]
@@ -98,6 +142,134 @@ def test_ec2_run_instances_honors_private_ip_and_iam_profile(ec2):
ec2.terminate_instances(InstanceIds=[iid])
+def test_ec2_run_instances_with_real_iam_profile_creates_association(iam, ec2):
+ suffix = f"launch-{_uuid_mod.uuid4().hex[:8]}"
+ role_name, profile_name, profile = _create_ec2_iam_instance_profile(iam, suffix)
+ resp = ec2.run_instances(
+ ImageId="ami-12345678",
+ MinCount=1,
+ MaxCount=1,
+ InstanceType="t3.micro",
+ IamInstanceProfile={"Name": profile_name},
+ )
+ inst = resp["Instances"][0]
+ iid = inst["InstanceId"]
+
+ try:
+ assert inst["IamInstanceProfile"]["Arn"] == profile["Arn"]
+ assert inst["IamInstanceProfile"]["Id"] == profile["InstanceProfileId"]
+
+ described = ec2.describe_iam_instance_profile_associations(
+ Filters=[{"Name": "instance-id", "Values": [iid]}]
+ )["IamInstanceProfileAssociations"]
+ assert len(described) == 1
+ assoc = described[0]
+ assert assoc["AssociationId"].startswith("iip-assoc-")
+ assert assoc["InstanceId"] == iid
+ assert assoc["State"] == "associated"
+ assert assoc["IamInstanceProfile"]["Arn"] == profile["Arn"]
+ assert assoc["IamInstanceProfile"]["Id"] == profile["InstanceProfileId"]
+
+ ec2.terminate_instances(InstanceIds=[iid])
+
+ active = ec2.describe_iam_instance_profile_associations(
+ Filters=[
+ {"Name": "instance-id", "Values": [iid]},
+ {"Name": "state", "Values": ["associated"]},
+ ]
+ )["IamInstanceProfileAssociations"]
+ assert active == []
+
+ all_states = ec2.describe_iam_instance_profile_associations(
+ AssociationIds=[assoc["AssociationId"]]
+ )["IamInstanceProfileAssociations"]
+ assert len(all_states) == 1
+ assert all_states[0]["State"] == "disassociated"
+ finally:
+ try:
+ ec2.terminate_instances(InstanceIds=[iid])
+ except ClientError:
+ pass
+ _delete_ec2_iam_instance_profile(iam, role_name, profile_name)
+
+
+def test_ec2_iam_instance_profile_association_lifecycle(iam, ec2):
+ suffix = _uuid_mod.uuid4().hex[:8]
+ role_a, profile_a_name, profile_a = _create_ec2_iam_instance_profile(
+ iam, f"a-{suffix}"
+ )
+ role_b, profile_b_name, profile_b = _create_ec2_iam_instance_profile(
+ iam, f"b-{suffix}"
+ )
+ resp = ec2.run_instances(ImageId="ami-12345678", MinCount=1, MaxCount=1)
+ iid = resp["Instances"][0]["InstanceId"]
+
+ try:
+ assoc = ec2.associate_iam_instance_profile(
+ InstanceId=iid,
+ IamInstanceProfile={"Name": profile_a_name},
+ )["IamInstanceProfileAssociation"]
+ assert assoc["State"] == "associated"
+ assert assoc["IamInstanceProfile"]["Arn"] == profile_a["Arn"]
+ assert assoc["IamInstanceProfile"]["Id"] == profile_a["InstanceProfileId"]
+
+ all_assocs = ec2.describe_iam_instance_profile_associations()[
+ "IamInstanceProfileAssociations"
+ ]
+ assert any(
+ item["AssociationId"] == assoc["AssociationId"] for item in all_assocs
+ )
+
+ by_instance = ec2.describe_iam_instance_profile_associations(
+ Filters=[{"Name": "instance-id", "Values": [iid]}]
+ )["IamInstanceProfileAssociations"]
+ assert len(by_instance) == 1
+ assert by_instance[0]["AssociationId"] == assoc["AssociationId"]
+
+ replaced = ec2.replace_iam_instance_profile_association(
+ AssociationId=assoc["AssociationId"],
+ IamInstanceProfile={"Arn": profile_b["Arn"]},
+ )["IamInstanceProfileAssociation"]
+ assert replaced["AssociationId"] == assoc["AssociationId"]
+ assert replaced["State"] == "associated"
+ assert replaced["IamInstanceProfile"]["Arn"] == profile_b["Arn"]
+ assert replaced["IamInstanceProfile"]["Id"] == profile_b["InstanceProfileId"]
+
+ inst = ec2.describe_instances(InstanceIds=[iid])["Reservations"][0]["Instances"][0]
+ assert inst["IamInstanceProfile"]["Arn"] == profile_b["Arn"]
+ assert inst["IamInstanceProfile"]["Id"] == profile_b["InstanceProfileId"]
+
+ disassoc = ec2.disassociate_iam_instance_profile(
+ AssociationId=assoc["AssociationId"]
+ )["IamInstanceProfileAssociation"]
+ assert disassoc["AssociationId"] == assoc["AssociationId"]
+ assert disassoc["State"] == "disassociated"
+
+ active = ec2.describe_iam_instance_profile_associations(
+ Filters=[
+ {"Name": "association-id", "Values": [assoc["AssociationId"]]},
+ {"Name": "state", "Values": ["associated"]},
+ ]
+ )["IamInstanceProfileAssociations"]
+ assert active == []
+
+ described = ec2.describe_iam_instance_profile_associations(
+ AssociationIds=[assoc["AssociationId"]]
+ )["IamInstanceProfileAssociations"]
+ assert len(described) == 1
+ assert described[0]["State"] == "disassociated"
+
+ inst = ec2.describe_instances(InstanceIds=[iid])["Reservations"][0]["Instances"][0]
+ assert "IamInstanceProfile" not in inst or not inst["IamInstanceProfile"]
+ finally:
+ try:
+ ec2.terminate_instances(InstanceIds=[iid])
+ except ClientError:
+ pass
+ _delete_ec2_iam_instance_profile(iam, role_a, profile_a_name)
+ _delete_ec2_iam_instance_profile(iam, role_b, profile_b_name)
+
+
def test_ec2_run_instances_default_private_ip_is_well_formed(ec2):
"""When the caller does not pass --private-ip-address, the auto-generated
address must still be a valid IPv4. Regression for the 10.0193.216 bug
@@ -112,6 +284,18 @@ def test_ec2_run_instances_default_private_ip_is_well_formed(ec2):
ec2.terminate_instances(InstanceIds=[inst["InstanceId"]])
+def test_ec2_run_instances_source_dest_check_defaults_true(ec2):
+ resp = ec2.run_instances(ImageId="ami-12345678", MinCount=1, MaxCount=1)
+ inst = resp["Instances"][0]
+ iid = inst["InstanceId"]
+ try:
+ assert inst["SourceDestCheck"] is True
+ described = ec2.describe_instances(InstanceIds=[iid])["Reservations"][0]["Instances"][0]
+ assert described["SourceDestCheck"] is True
+ finally:
+ ec2.terminate_instances(InstanceIds=[iid])
+
+
def test_ec2_run_instances_emits_default_root_block_device_mapping(ec2):
"""RunInstances without an explicit BDM must auto-attach a root EBS volume,
matching real AWS. Cloud Custodian / AWS Config use BlockDeviceMappings to