Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(provisioners): pendinf cfn provisioner (#949)
Nahuel Nucera9215027482c3
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.pyindex 52b55a26..fe6f5b0d 100644--- a/ministack/services/cloudformation/provisioners.py+++ b/ministack/services/cloudformation/provisioners.py@@ -1901,6 +1901,9 @@ def _lambda_esm_create(logical_id, props, stack_name):"FunctionResponseTypes": props.get("FunctionResponseTypes", []),}_lambda_svc._esms[esm_id] = esm+ # Anchor a DynamoDB-stream ESM's LATEST position at create time, matching the+ # API CreateEventSourceMapping path; no-op for SQS/Kinesis sources (#936).+ _lambda_svc._init_stream_position(esm_id, esm["EventSourceArn"], esm["StartingPosition"])_lambda_svc._ensure_poller()return esm_id, {"UUID": esm_id}@@ -3015,9 +3018,15 @@ def _lambda_layer_create(logical_id, props, stack_name):"LicenseInfo": props.get("LicenseInfo", ""),"CreatedDate": now_iso(),"Content": {+ "Location": _lambda_svc._layer_content_url(layer_name, ver),"CodeSha256": (base64.b64encode(hashlib.sha256(zip_data).digest()).decode() if zip_data else ""),"CodeSize": len(zip_data) if zip_data else 0,},+ # Without _zip_data the layer is silently skipped at worker spawn+ # (_resolve_layer_zip returns None), so functions deployed via CDK/CFN+ # can't import their layer packages even though list-layers shows them.+ "_zip_data": zip_data,+ "_policy": {"Version": "2012-10-17", "Id": "default", "Statement": []},}layer["versions"].append(ver_config)return version_arn, {"LayerVersionArn": version_arn, "Arn": version_arn}diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex aad8ec7c..24307960 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -3573,3 +3573,70 @@ def test_cfn_change_set_detects_parameter_driven_change(cfn, s3):# nothing changed -> empty change set (no false positive)noop = _change_set("cs-noop", "a.zip")assert len(noop.get("Changes", [])) == 0+++def test_cfn_lambda_layer_packages_importable(cfn, s3, lam):+ """A Lambda layer deployed via CloudFormation (CDK pattern: Content from S3)+ must make its packages importable at invoke time.++ Regression: the CFN LayerVersion provisioner fetched the layer zip but never+ stored it as ``_zip_data``, so ``_resolve_layer_zip`` returned None and the+ layer was silently skipped at worker spawn — ``No module named ...`` even+ though ``list-layers`` showed the layer. Reported by @ocr-lasagna."""+ stack_name = "cfn-layer-import"+ bucket_name = "cfn-layer-assets"+ fn_name = "cfn-layer-fn"++ s3.create_bucket(Bucket=bucket_name)++ # Layer zip with a Python module under python/ (the AWS layer convention).+ layer_buf = io.BytesIO()+ with zipfile.ZipFile(layer_buf, "w") as z:+ z.writestr("python/cfn_layer_helper.py", "LAYER_VALUE = 'from-cfn-layer'\n")+ s3.put_object(Bucket=bucket_name, Key="layer.zip", Body=layer_buf.getvalue())++ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "MyLayer": {+ "Type": "AWS::Lambda::LayerVersion",+ "Properties": {+ "LayerName": "cfn-import-layer",+ "CompatibleRuntimes": ["python3.12"],+ "Content": {"S3Bucket": bucket_name, "S3Key": "layer.zip"},+ },+ },+ "MyFunction": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": fn_name,+ "Runtime": "python3.12",+ "Handler": "index.handler",+ "Role": "arn:aws:iam::000000000000:role/cfn-role",+ "Layers": [{"Ref": "MyLayer"}],+ "Code": {+ "ZipFile": (+ "import cfn_layer_helper\n"+ "def handler(event, context):\n"+ " return {'value': cfn_layer_helper.LAYER_VALUE}\n"+ ),+ },+ },+ },+ },+ }++ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE"++ try:+ resp = lam.invoke(FunctionName=fn_name, Payload=b"{}")+ assert resp["StatusCode"] == 200+ assert "FunctionError" not in resp, (+ f"Lambda error: {resp['Payload'].read().decode()}"+ )+ payload = json.loads(resp["Payload"].read())+ assert payload["value"] == "from-cfn-layer"+ finally:+ cfn.delete_stack(StackName=stack_name)