https://pulumi.com logo
#python
Title
# python
s

strong-musician-98782

09/02/2020, 8:39 AM
Hey guys, i'm trying to create a class to create ec2 instance and then call it from my main but pulumi keeps thinking my userdata is changing even when its not, here's part of my class(tried to edit out as much as i can so some args might be missing):
Copy code
import pulumi
from pulumi_aws import ec2

class KSDBServerArgs:
    """
    The arguments necessary to construct an `KSDBServer` resource.
    """
    def __init__(self,
                 ks_id: str):
        """
        Constructs an KSArgs.
        :param ks_id:
        :param user_data:
        """

        self.ks_id = ks_id
        config = pulumi.Config()

        self.user_data = '''
        #!/bin/bash
        /usr/sbin/useradd -d /home/user -m -s /bin/bash user
        echo "user ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
        mkdir -p /home/user/.ssh
        chown -R user:user /home/user/.ssh
        chmod 700 /home/user/.ssh
        echo 'ssh-rsa {1}' >> /root/.ssh/authorized_keys
        '''.format(config.require('build_token'),config.require('certificate'))



class KSDBServer(pulumi.ComponentResource):
    def __init__(self,
                 name: str,
                 args: KSDBServerArgs,
                 opts:pulumi.ResourceOptions = None):
        super().__init__('kenshoo:KS:KSDBServer', name, None, opts)
        self.name = name

        # Creating ec2 instance
        self.db_instance = ec2.Instance("aws-"+ args.ks_id + "-db",
                                                instance_type=args.instance_size,
                                                vpc_security_group_ids=args.db_sec_groups,
                                                subnet_id=args.subnet,
                                                ami=args.ami,
                                                user_data=args.user_data,
                                                opts=pulumi.ResourceOptions(parent=self,delete_before_replace=True))
g

gentle-diamond-70147

09/02/2020, 4:12 PM
Can you share the output from
preview --diff
as well?
s

strong-musician-98782

09/02/2020, 5:09 PM
Copy code
pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:ks1234::ks::pulumi:pulumi:Stack::ks-ks1234]
        +-aws:ec2/instance:Instance: (replace)
            [id=i-0bbbfb2b5d7a1f1d8]
            [urn=urn:pulumi:ks1234::ks::kenshoo:KS:KSAppServer$aws:ec2/instance:Instance::ks-aws-ue-ks1234-app]
            [provider=urn:pulumi:ks1234::ks::pulumi:providers:aws::default_2_13_1::cc8952d4-1e59-4ab4-8375-6764fe176939]
          ~ userData: "ff55eb8b81f6b18b46eee35607dcc044a8d9b9b8" => "\n        #!/bin/bash\n        hostname > /tmp/mortst\n        yum install git -y\n        git pull https://[secret]@github.com/kenshoo/ansible-common.git\n        /usr/sbin/useradd -d /home/deployer -m -s /bin/bash deployer\n        echo \"deployer ALL=(ALL:ALL) NOPASSWD: ALL\" >> /etc/sudoers\n        mkdir -p /home/deployer/.ssh\n        chown -R deployer:deployer /home/deployer/.ssh\n        chmod 700 /home/deployer/.ssh\n        echo 'ssh-rsa [secret]' >> /root/.ssh/authorized_keys\n\n        mkfs -t ext4 /dev/sdb\n        mkdir /kdata\n        echo '/dev/sdb /kdata ext4 defaults,noatime,data=writeback 0       0' >> /etc/fstab\n        mount /kdata\n        "
just to be clear, when i try to use the same code outside of the class in my main it works just fine, it also works in the class if i remove all variables (even though the variables should be the same all the time as they are from the config)