Hi Guys, Im getting this error in my code and I c...
# python
f
Hi Guys, Im getting this error in my code and I cant figure out why. Its like one resource cant be called while another is doing something but I have no idea what resource it is referring to. This is the error:
error: cannot complete a resource 'urn:pulumi:jcontent_dev::jcontent-dev::aws:compute:ec2instance::investp-sql-server' whose registration isn't pending
This is the code it is mainly calling:
Copy code
class CoritySQLServerBuild(pulumi.Resource):
    def __init__(
        self,
        name: str,
        cidr_block: str,  # This needs importing to create the SQL Sec Group for SSH from the VPC only, not the whole world.
        vpc_id: str,
        base_tags: Mapping[str, str],
        ami_id: str,
        subnet_id: str,
        opts=None,
    ):
        super().__init__("cority:aws:database:sqlservercreation", name, None, opts)
        sql_server_list_disks_policy = aws.iam.Policy(
            "myPolicy",
            policy="""
            {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Sid": "IamPassRole",
                        "Effect": "Allow",
                        "Action": "iam:PassRole",
                        "Resource": "*",
                        "Condition": {
                            "StringEquals": {
                                "iam:PassedToService": "<http://ec2.amazonaws.com|ec2.amazonaws.com>"
                            }
                        }
                    },
                    {
                        "Sid": "ListEc2AndListInstanceProfiles",
                        "Effect": "Allow",
                        "Action": [
                            "iam:ListInstanceProfiles",
                            "ec2:Describe*",
                            "ec2:Search*",
                            "ec2:Get*"
                        ],
                        "Resource": "*"
                    }
                ]
            }
            """,
        )

        sql_server_list_disks_role = aws.iam.Role(
            "SqlServerListDisksRole",
            assume_role_policy="""
                {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "<http://ec2.amazonaws.com|ec2.amazonaws.com>"
                            },
                            "Action": "sts:AssumeRole"
                        }
                    ]
                }
            """,
        )

        role_policy_attachment = aws.iam.RolePolicyAttachment(
            "SQLServerListDisksPolicyAttachment",
            role=sql_server_list_disks_role.name,
            policy_arn=sql_server_list_disks_policy.arn,
        )

        self.ec2_profile = aws.iam.InstanceProfile(
            "jc-instance-profile",
            name="jc-instance-profile",
            path="/",
            role=sql_server_list_disks_role.name,
            opts=pulumi.ResourceOptions(depends_on=[sql_server_list_disks_role]),
        )

        self.sql_security_group = secgroup.CoritySECG(
            "sql_server_sec_group",
            secgroup.SECGArgs(
                internal=False,
                enable_deletion_protection=False,
                service_type="ec2",
                cidr_blocks=cidr_block,
                vpc_id=vpc_id,
                base_tags=base_tags,
            ),
            opts=None,
        )

        pulumi.export("secgroupid", self.sql_security_group.security_group_id)

        self.sql_ec2_instance = CorityAwsComputeEc2Instance(
            name,
            CorityAwsComputeEc2InstanceArgs(
                name=name,
                description="EC2 instance for SQL Server with specific volume setup",
                base_tags=base_tags,
                type="r5.xlarge",
                enable_deletion_protection=False,
                ami_id=ami_id,
                purpose="SQL Server",
                ebs_block_device_mappings=[
                    {
                        "deviceName": "/dev/xvda",
                        "ebs": {
                            "delete_on_termination": True,
                            "volume_size": 100,  # C: 100GB
                            "iops": 3000,
                            "encrypted": True,
                            "volume_type": "gp3",
                        },
                    }
                ],
                volumes=[
                    {
                        "resource_suffix": "vold",
                        "attachment": {"device": "/dev/sdb"},
                        "properties": {
                            "encrypted": True,
                            "iops": 3000,
                            "size": 20,  # D: 20GB
                            "throughput": 300,
                            "volume_type": "gp3",
                        },
                    },
                    {
                        "resource_suffix": "volm",
                        "attachment": {"device": "/dev/sdc"},
                        "properties": {
                            "encrypted": True,
                            "iops": 3000,
                            "size": 45,  # M: 45GB
                            "throughput": 300,
                            "volume_type": "gp3",
                        },
                    },
                    {
                        "resource_suffix": "voln",
                        "attachment": {"device": "/dev/sdd"},
                        "properties": {
                            "encrypted": True,
                            "iops": 3000,
                            "size": 150,  # N: 150GB
                            "throughput": 300,
                            "volume_type": "gp3",
                        },
                    },
                    {
                        "resource_suffix": "voly",
                        "attachment": {"device": "/dev/sde"},
                        "properties": {
                            "encrypted": True,
                            "iops": 3000,
                            "size": 175,  # Y: 175GB
                            "throughput": 300,
                            "volume_type": "gp3",
                        },
                    },
                    {
                        "resource_suffix": "volz",
                        "attachment": {"device": "/dev/sdf"},
                        "properties": {
                            "encrypted": True,
                            "iops": 3000,
                            "size": 250,  # Z: 250GB
                            "throughput": 300,
                            "volume_type": "gp3",
                        },
                    },
                ],
                key_name="jcontent-dev",
                vpc_id=vpc_id,
                subnet_id=subnet_id,
                security_groups=[
                    self.sql_security_group.security_group_id.apply(lambda x: x)
                ],
                instance_profile=self.ec2_profile.arn.apply(lambda v: v),
            ),
            opts=pulumi.ResourceOptions(depends_on=[self.ec2_profile]),
        )

        # Export the instance's private DNS or IP
        pulumi.export("sql_ec2_private_ip", self.sql_ec2_instance.private_ip)

        # Generate a new SSH key pair
        tls_key_pair = tls.PrivateKey("SQLKeyPair", algorithm="RSA", rsa_bits=4096)

        # Create an AWS key pair using the generated public key
        key_pair = aws.ec2.KeyPair(
            "SQLKeyPair",
            key_name="sql-key-pair",
            public_key=tls_key_pair.public_key_openssh,
        )

        # The configuration of our SSH connection to the instance via its private IP.
        # Ensure your machine or a VPN/Bastion host has access to this private IP.
        # The configuration of our SSH connection to the instance via its private IP.
        conn = command.remote.ConnectionArgs(
            host=self.sql_ec2_instance.public_ip,
            user="ec2-user",
            private_key=tls_key_pair.private_key_pem,
        )

        # Copy the files to the remote.
        copy = command.remote.CopyFile(
            f"{name}-file_copy",
            connection=conn,
            local_path="/Fetch-AWSProductCategories.ps1",
            remote_path="/home/ec2-user/Fetch-AWSProductCategories.ps1",
            opts=pulumi.ResourceOptions(depends_on=[self.sql_ec2_instance]),
        )

        # Running the PowerShell script
        run_script = command.remote.Command(
            f"{name}-run_script",
            create="powershell.exe -File /home/ec2-user/Fetch-AWSProductCategories.ps1",
            connection=conn,
            opts=pulumi.ResourceOptions(depends_on=[copy]),
        )