Could someone please help me understand why Pulumi...
# python
f
Could someone please help me understand why Pulumi is not recognizing my output? I've been grappling with this issue for two and a half days and it's becoming increasingly frustrating. Your prompt assistance would be greatly appreciated. This is my error; it keeps coming up in this class no matter what I try.
Copy code
error: Program failed with an unhandled exception:
    Traceback (most recent call last):
      File "C:\DevOps\GreenStone\InvestorPortal\cority.awsinfrastructure.pulumi.greenstone.investorportal\infrastructure\__main__.py", line 62, in <module>
        security_groups=[security_group_creation.security_group_id],
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: 'CoritySECG' object has no attribute 'security_group_id'. Did you mean: 'security_group_type'?
My code:
Copy code
import pulumi
import pulumi_aws as aws
from pulumi import ResourceOptions, Output
from typing import Mapping, List


class SECGArgs:
    """The arguments necessary to construct a security group resource"""

    def __init__(
        self,
        base_tags: Mapping[str, str],
        internal: bool,
        enable_deletion_protection: bool,
        service_type: List[str],
        cidr_blocks: List[str],
        vpc_id: str,
    ):
        self.internal = internal
        self.service_type = service_type
        self.cidr_blocks = cidr_blocks
        self.enable_deletion_protection = enable_deletion_protection
        self.vpc_id = vpc_id
        self.base_tags = base_tags


class CoritySECG(pulumi.ComponentResource):
    def __init__(
        self, resource_name: str, args: SECGArgs, opts: ResourceOptions = None
    ):
        super().__init__("cority:aws:ecs", resource_name, {}, opts)

        self.sec_group_name = resource_name
        self.internal = args.internal
        self.enable_deletion_protection = args.enable_deletion_protection
        self.vpc_id = args.vpc_id
        self.base_tags = args.base_tags
        self.service_type = args.service_type
        self.cidr_blocks = args.cidr_blocks

    def security_group_type(self, args: SECGArgs, sec_group_name: str):
        if args.service_type == "ecs":
            # Create a Security Group for ECS
            security_group = aws.ec2.SecurityGroup(
                sec_group_name,
                description="Allow all inbound traffic for ECS tasks.",
                vpc_id=args.vpc_id,
                ingress=[
                    aws.ec2.SecurityGroupIngressArgs(
                        protocol="-1",  # All protocols
                        from_port=0,
                        to_port=0,
                        cidr_blocks=["0.0.0.0/0"],
                    ),
                ],
                egress=[
                    aws.ec2.SecurityGroupEgressArgs(
                        protocol="-1",  # All protocols
                        from_port=0,
                        to_port=0,
                        cidr_blocks=["0.0.0.0/0"],
                    ),
                ],
                tags=args.base_tags,
            )
        elif args.service_type == "ec2":
            # Create a Security Group for EC2
            security_group = aws.ec2.SecurityGroup(
                sec_group_name,
                description="Allow SSH and HTTP inbound traffic for EC2 instances.",
                vpc_id=args.vpc_id,
                ingress=[
                    aws.ec2.SecurityGroupIngressArgs(
                        protocol="tcp",
                        from_port=22,
                        to_port=22,
                        cidr_blocks=["0.0.0.0/0"],  # Allow SSH from anywhere
                    ),
                    aws.ec2.SecurityGroupIngressArgs(
                        protocol="tcp",
                        from_port=80,
                        to_port=80,
                        cidr_blocks=["0.0.0.0/0"],  # Allow HTTP from anywhere
                    ),
                ],
                egress=[
                    aws.ec2.SecurityGroupEgressArgs(
                        protocol="-1",  # All protocols
                        from_port=0,
                        to_port=0,
                        cidr_blocks=["0.0.0.0/0"],
                    ),
                ],
                tags=args.base_tags,
            )
        elif args.service_type in ["ecs", "ec2"]:
            # Create a Security Group for both ECS and EC2
            security_group = aws.ec2.SecurityGroup(
                sec_group_name,
                description="Allow all inbound traffic for ECS tasks and SSH/HTTP for EC2 instances.",
                vpc_id=args.vpc_id,
                ingress=[
                    aws.ec2.SecurityGroupIngressArgs(
                        protocol="-1",  # All protocols for ECS
                        from_port=0,
                        to_port=0,
                        cidr_blocks=["0.0.0.0/0"],
                    ),
                    aws.ec2.SecurityGroupIngressArgs(
                        protocol="tcp",
                        from_port=22,
                        to_port=22,
                        cidr_blocks=["0.0.0.0/0"],  # Allow SSH from anywhere for EC2
                    ),
                    aws.ec2.SecurityGroupIngressArgs(
                        protocol="tcp",
                        from_port=80,
                        to_port=80,
                        cidr_blocks=["0.0.0.0/0"],  # Allow HTTP from anywhere for EC2
                    ),
                ],
                egress=[
                    aws.ec2.SecurityGroupEgressArgs(
                        protocol="-1",  # All protocols
                        from_port=0,
                        to_port=0,
                        cidr_blocks=["0.0.0.0/0"],
                    ),
                ],
                tags=args.base_tags,
            )

            return security_group

        self.security_group_id = security_group.id

        self.register_outputs({"security_group_id": self.security_group_id})
I am calling the class like so (Please assume all imports are working, cant share the whole of main because of company data):
Copy code
secg_args = security_group.SECGArgs(
    base_tags={"Project": "Investor Portal"},
    internal=False,
    enable_deletion_protection=False,
    service_type=["ecs", "ec2"],
    cidr_blocks=[cnetworking.vpc_cidr_block],
    vpc_id=vpc_id,
)

# Create Security Group
security_group_creation = security_group.CoritySECG(
    resource_name="ecs-ec2-ip-sec-group",
    args=secg_args,
    opts=None,
)
d
It sounds like you haven't called the
security_group_type
, which is what assigns
security_group_id
☝️ 1