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(ssm): serve the public latest Amazon Linux AMI parameters (#2319)
Maximilian Bandleb386446c77ea
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.pyindex c30e6afd..9a0f42cf 100644--- a/ministack/services/ec2.py+++ b/ministack/services/ec2.py@@ -1266,6 +1266,7 @@ def _reboot_instances(p):# (Terraform's aws_ami data source) select the same images it selects there._STUB_AMIS = [("ami-0abcdef1234567890", "amzn2-ami-hvm-2.0.20231116.0-x86_64-gp2", "Amazon Linux 2", "", "/dev/xvda", "137112412989", "amazon"),+ ("ami-0c2023a1b2c3d4e5f", "al2023-ami-2023.6.20241010.0-kernel-6.1-x86_64", "Amazon Linux 2023 AMI", "", "/dev/xvda", "137112412989", "amazon"),("ami-0123456789abcdef0", "ubuntu/images/hvm-ssd/ubuntu-22.04-amd64-server", "Ubuntu 22.04", "", "/dev/xvda", "099720109477", None),("ami-0fedcba9876543210", "Windows_Server-2022-English-Full-Base", "Windows Server 2022", "windows", "/dev/sda1", "801119661308", "amazon"),]diff --git a/ministack/services/ssm.py b/ministack/services/ssm.pyindex 1fcc68ce..fee8e279 100644--- a/ministack/services/ssm.py+++ b/ministack/services/ssm.py@@ -401,6 +401,8 @@ def resolve_parameter_value(name_or_arn, version=None, decrypt=False):if not name_or_arn:return Nonename, param = _lookup_parameter(name_or_arn, allow_arn_region=True, flexible_name=True)+ if not param and version is None:+ param = _public_parameter(name_or_arn)if not param:return Noneif version is not None and str(version) != str(param.get("Version")):@@ -424,10 +426,28 @@ def _split_selector(name_or_arn):return base, selector+# Public latest-AMI parameters every account can read, pointed at the stub images in ec2.py.+_PUBLIC_AMI_PARAMETERS = {+ "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64": "ami-0c2023a1b2c3d4e5f",+ "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2": "ami-0abcdef1234567890",+}+++def _public_parameter(name):+ """A public parameter as AWS serves it, with no account in the ARN."""+ image_id = _PUBLIC_AMI_PARAMETERS.get(name)+ if image_id is None:+ return None+ return {"Name": name, "Type": "String", "Value": image_id, "Version": 1, "LastModifiedDate": 0,+ "ARN": f"arn:aws:ssm:{get_region()}::parameter{name}"}++def _select_parameter(name_or_arn):"""(param, error) for GetParameter / GetParameters, honouring name:version and name:label."""base, selector = _split_selector(name_or_arn)name, param = _lookup_parameter(base)+ if not param and selector is None:+ param = _public_parameter(base)if not param:return None, "ParameterNotFound"if selector is None:diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex 8f489b62..645ab298 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -2820,6 +2820,26 @@ def test_cfn_ssm_parameter_value_type_resolves_stored_value(cfn, ssm, sqs):assert urls_by_name == []+def test_cfn_ssm_parameter_value_type_resolves_public_ami_parameter(cfn, ec2):+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Parameters": {+ "ImageId": {+ "Type": "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>",+ "Default": "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64",+ }+ },+ "Resources": {"Handle": {"Type": "AWS::CloudFormation::WaitConditionHandle"}},+ "Outputs": {"ImageId": {"Value": {"Ref": "ImageId"}}},+ }+ cfn.create_stack(StackName="cfn-public-ami", TemplateBody=json.dumps(template))+ _wait_stack(cfn, "cfn-public-ami")+ outputs = cfn.describe_stacks(StackName="cfn-public-ami")["Stacks"][0].get("Outputs", [])+ image_id = next(o["OutputValue"] for o in outputs if o["OutputKey"] == "ImageId")+ assert ec2.describe_images(ImageIds=[image_id])["Images"][0]["Name"].startswith("al2023-ami-")+ cfn.delete_stack(StackName="cfn-public-ami")++def test_cfn_ssm_parameter_value_type_use_previous_value_on_update(cfn, ssm, sqs):"""A change set that re-sends an AWS::SSM::Parameter::Value<String>parameter as UsePreviousValue (the `aws cloudformation deploy`diff --git a/tests/test_ec2.py b/tests/test_ec2.pyindex cb410b33..72dade8e 100644--- a/tests/test_ec2.py+++ b/tests/test_ec2.py@@ -4564,7 +4564,7 @@ def test_ec2_describe_images_filters_apply_to_the_stubs(ec2):public = ec2.describe_images(Filters=[{"Name": "is-public", "Values": ["true"]}])["Images"]assert {i["ImageId"] for i in public} == {- "ami-0abcdef1234567890", "ami-0123456789abcdef0", "ami-0fedcba9876543210"}+ "ami-0abcdef1234567890", "ami-0c2023a1b2c3d4e5f", "ami-0123456789abcdef0", "ami-0fedcba9876543210"}private = ec2.describe_images(Filters=[{"Name": "is-public", "Values": ["false"]}])["Images"]diff --git a/tests/test_ssm.py b/tests/test_ssm.pyindex 031ef358..c87ff028 100644--- a/tests/test_ssm.py+++ b/tests/test_ssm.py@@ -788,6 +788,12 @@ def test_ssm_put_parameter_name_error_wins_over_other_checks(ssm):assert exc.value.response["Error"]["Code"] == "AccessDeniedException"+def test_ssm_public_latest_ami_parameter_names_a_stub_image(ssm, ec2):+ param = ssm.get_parameter(Name="/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64")["Parameter"]+ assert param["ARN"].split(":")[4] == ""+ assert ec2.describe_images(ImageIds=[param["Value"]])["Images"][0]["Name"].startswith("al2023-ami-")++def test_ssm_put_parameter_rejects_tags_with_overwrite(ssm):tags = [{"Key": "k", "Value": "v"}]new_name = f"/ps-tags/{_uuid_mod.uuid4().hex[:8]}"