[Python] Python bool object is not subscriptable

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I get this error when trying to process a dictionary of lists from a yaml file through to Python. It is using the Pulumi package.

    error: Program failed with an unhandled exception:
    Traceback (most recent call last):
    File "C:\DevOps\infrastructure.pulumi\iac-development\jcontent-dev-v2\__main__.py", line 137, in <module>
    storage_gateway_resource = storage_gateway.AwsStorageGateway(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\DevOps\infrastructure.pulumi\iac-development\jcontent-dev-v2\../../pkg\aws_storage_storagegateway.py", line 366, in __init__
    aws_compute_ec2_instance.AwsComputeEc2InstanceArgs(
    File "C:\DevOps\infrastructure.pulumi\iac-development\jcontent-dev-v2\../../pkg\aws_compute_ec2_instance.py", line 111, in __init__
    "device_name": volume_details["device_name"]
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
    TypeError: 'bool' object is not subscriptable


    No matter what I have tried, Python keeps getting stuck with bool object is not subscribable. I know there are two bool objects being passed through, but I don't understand why Python can't simply pass them and put them into the dictionary of lists.

    This is an area of code within a class that is processing the dictionary of lists.

    # Define the primary drive
    volumes = [
    {
    "primary": {
    "device_name": "/dev/xvda",
    "size": 80,
    "volume_type": "gp3",
    "iops": 3000,
    "throughput": 125,
    "encrypted": True,
    "delete_on_termination": True,
    }
    }
    ]

    # Add secondary drives
    if storage_gateway_drives:
    for index, second_drive in enumerate(storage_gateway_drives):
    # Ensure the secondary drive is properly formatted
    volumes.append(
    {
    f"secondary-{index + 1}": {
    "attachment": {
    "device_name": second_drive.get("device_name", f"/dev/xvd{chr(98 + index)}")
    },
    "properties": {
    "encrypted": second_drive.get("encrypted", True),
    "iops": second_drive.get("iops", 3000),
    "size": second_drive.get("size", 100),
    "throughput": second_drive.get("throughput", 125),
    "volume_type": second_drive.get("volume_type", "gp3"),
    "delete_on_termination": second_drive.get("delete_on_termination", True),
    },
    }
    }
    )


    It's calling a custom class that is running the creation of the EC2 instance with Volumes.

    for volume in args.volumes:
    volume["properties"]["resource_name"] = ( resource_name + "-" + volume["resource_suffix"] + "-" + args.resource_suffix)
    volume["properties"]["encrypted"] = True # Enforce encryption
    volume_properties = volume["properties"]

    volume_resource = aws.ebs.Volume(
    **volume_properties,
    opts=ResourceOptions(parent=self.instance),
    tags={"Name": volume["properties"]["resource_name"]},
    )

    volume["attachment"]["resource_name"] = ( resource_name + "-attachment-" + volume["resource_suffix"] + "-" + args.resource_suffix)
    volume["attachment"]["instance_id"] = self.instance.id.apply(lambda v: v)
    volume["attachment"]["volume_id"] = volume_resource.id.apply(lambda v: v)
    volume["attachment"]["opts"] = pulumi.ResourceOptions(
    depends_on=[self.instance, volume_resource], parent=volume_resource
    )

    attachment = aws.ec2.VolumeAttachment(**volume["attachment"])
    self.volume_attachments.append(attachment)


    This is the yaml section that is being passed in as the input.

    storage_gateway:
    description: "Storage Gateway for testing"
    ssm_parameter_name: "sgw-jcontent-param-name"
    role_name: "sgw-jcontent-rolename"
    base_tags:
    Environment: "stage2"
    Product: "value"
    Engineer: "jcontent"
    fixed_ami_id: "ami-value"
    ad_domain_join_username: "value"
    ad_domain_join_password_ssm_parameter_name: "Value"
    ad_domain_name: "value"
    ad_name_servers: "value"
    ad_id: "d-value"
    ec2_keypair_name: "value"
    storage_gateway_drives:
    - device_name: "/dev/xvdb"
    size: 100
    volume_type: gp3
    iops: 3000
    throughput: 125
    encrypted: true
    delete_on_termination: true
    - device_name: "/dev/xvdc"
    size: 200
    volume_type: gp3
    iops: 6000
    throughput: 250
    encrypted: true
    delete_on_termination: true
    storage_gateway_volume_size: "100"
    enable_deletion_protection: false


    This is the main.py code calling the storagegateway class and processing the yaml input.

    import pulumi
    import pulumi_aws as aws
    from pulumi.config import Config
    import sys
    import json
    import yaml
    import aws_compute_ec2_instance as compute
    import aws_storage_storagegateway as storage_gateway

    stor_gate_s3_bucket_name='jc-dev-storagegateway-temp'
    s3_debug_bucket_name='jc-dev-storagegateway-debug-temp'
    gateway_storage_bucket = aws.s3.Bucket(stor_gate_s3_bucket_name)
    gateway_debug_bucket = aws.s3.Bucket(s3_debug_bucket_name)
    pulumi.export('gateway_storage_bucket', gateway_storage_bucket.id)
    pulumi.export('gateway_debug_bucket', gateway_debug_bucket.id)

    config = pulumi.Config()

    storage_gateway_yaml = config.get("storage_gateway")
    print(f"Loaded storage_gateway_yaml: {storage_gateway_yaml}")

    if storage_gateway_yaml is None:
    raise ValueError("The 'storage_gateway' key is missing from the Pulumi configuration.")

    # Parse the YAML configuration for the storage gateway
    storage_gateway_config = yaml.safe_load(storage_gateway_yaml)

    # Create the storage gateway arguments
    storage_gateway_args = storage_gateway.AwsStorageGatewayArgs(
    description=storage_gateway_config.get("description", "Default Storage Gateway"),
    s3_bucket_name=gateway_storage_bucket.id,
    s3_debug_bucket_name=gateway_debug_bucket.id,
    vpc_id=cfn_network.vpc_id,
    subnet_id_0=cfn_network.get_subnets_by_role(roles=["storage-gateway"],az=1),
    subnet_id_1=cfn_network.get_subnets_by_role(roles=["storage-gateway"],az=0),
    security_groups=cfn_network.get_security_groups_by_role(roles=["storage-gateway"]),
    vpc_endpoint_securitygroups = cfn_network.get_security_groups_by_role(roles=["vpc-endpoint"]),
    ad_domain_join_username = storage_gateway_config.get("ad_domain_join_username"),
    ad_domain_join_password_ssm_parameter_name = storage_gateway_config.get("ad_domain_join_password_ssm_parameter_name"),
    ad_domain_name = storage_gateway_config.get("ad_domain_name"),
    ad_name_servers = storage_gateway_config.get("ad_name_servers", {}),
    ad_id = storage_gateway_config.get("ad_id"),
    resource_suffix = resource_suffix,
    ec2_key_pair_name = storage_gateway_config.get("ec2_key_pair_name"),
    lambda_activator_securitygroups=cfn_network.get_security_groups_by_role(roles=["storage-gateway"]),
    base_tags=storage_gateway_config.get("base_tags", {}),
    fixed_ami_id=storage_gateway_config.get("fixed_ami_id"),
    storage_gateway_drives=storage_gateway_config.get("storage_gateway_drives", []),
    storage_gateway_volume_size=storage_gateway_config.get("storage_gateway_volume_size"),
    enable_deletion_protection=storage_gateway_config.get("enable_deletion_protection", False),
    )

    # Create the storage gateway resource
    storage_gateway_resource = storage_gateway.AwsStorageGateway(
    resource_name="jc-testing-storage-gateway",
    args=storage_gateway_args,
    )

    Continue reading...

Compartilhe esta Página