happy-iron-52048
11/23/2024, 9:30 AMechoing-dinner-19531
11/23/2024, 4:31 PMDoes Pulumi allow granular control to update specific resources without needing to reconstruct or recreate the entire stack pulumi program?You can use --target to target specific resources in a stack for update.
Is Pulumi suited for managing “pet”-like resources that require careful handling and updates?Yes, generally you would use the protect flag to ensure you don't accidentally delete them.
little-cartoon-10569
11/24/2024, 6:12 PMhappy-iron-52048
11/26/2024, 8:16 AMCurrent stack resources (3):
TYPE NAME
pulumi:pulumi:Stack pulumi_poc-abc
│ URN: urn:pulumi:abc::pulumi_poc::pulumi:pulumi:Stack::pulumi_poc-abc
├─ aws:ec2/instance:Instance test-4
│ URN: urn:pulumi:abc::pulumi_poc::aws:ec2/instance:Instance::test-4
└─ pulumi:providers:aws default_6_59_1
URN: urn:pulumi:abc::pulumi_poc::pulumi:providers:aws::default_6_59_1
Code To Run
def new_instance():
# Import the existing EC2 instance
existing_instance = aws.ec2.Instance.get(
resource_name='existingInstance',
id='xxxxxxxxx',
)
instance = aws.ec2.Instance(
f"test-5",
ami="xxxxxxxxxx",
instance_type="c5n.4xlarge",
key_name="xxxxxxxxxx",
subnet_id="xxxxxxxxxx",
vpc_security_group_ids=["xxxxxxx"],
iam_instance_profile="xxxxxxxxx",
)
pulumi.export(
f"ip",
pulumi.Output.all(instance.private_ip)
)
pulumi.export('instance_id', instance.id)
pulumi.export('instance_type', instance.instance_type)
stack = auto.create_or_select_stack(
stack_name="abc",
project_name="pulumi_poc",
program=new_instance
)
Result
Updating (abc):
@ updating....
+ aws:ec2:Instance test-5 creating (0s)
@ updating.......
pulumi:pulumi:Stack pulumi_poc-abc running read aws:ec2:Instance existingInstance
@ updating................
+ aws:ec2:Instance test-5 created (16s)
- aws:ec2:Instance test-4 deleting (0s)
@ updating.......................................................
- aws:ec2:Instance test-4 deleted (52s)
@ updating....
pulumi:pulumi:Stack pulumi_poc-abc
Outputs:
~ instance_id : "xxxxxx" => "xxxxxx"
instance_type: "c5n.4xlarge"
~ ip : [
~ [0]: "xxxxx" => "xxxxxxx"
]
Required Behaviour
test-4 instance should also exists
test-5 instance should also exists
Question
What could be the possible solution if i wanted the required behaviour? I looked at stack reference (but as far i understand it is only for getting output from stacks), importing resources (this is also i think to get resources not managed by pulumi)echoing-dinner-19531
11/26/2024, 8:40 AMhappy-iron-52048
11/26/2024, 8:51 AMechoing-dinner-19531
11/26/2024, 8:54 AMhappy-iron-52048
11/26/2024, 9:04 AMexisting_instance = aws.ec2.Instance.get(
resource_name='existingInstance',
id='xxxxxxxxx',
)
I put the name of resource as existingInstance
instead in my stack the resource name was test-4, after modifying my program to below it didn’t delete the existing resources.
Updated
def new_program():
# Import the existing EC2 instance
test_4 = aws.ec2.Instance.get(
resource_name='test-4',
id='xxxxx',
)
test_6 = aws.ec2.Instance.get(
resource_name='test-6',
id='xxxxxx',
)
test_7 = aws.ec2.Instance.get(
resource_name='test-7',
id='xxxxxxx',
)
instance = aws.ec2.Instance(
f"test-8",
ami="xxxxxxxxxx",
instance_type="c5n.4xlarge",
key_name="xxxxxxxxxx",
subnet_id="xxxxxxxxxx",
vpc_security_group_ids=["xxxxxxx"],
iam_instance_profile="xxxxxxxxx",
)
pulumi.export(
f"ip",
pulumi.Output.all(instance.private_ip)
)
pulumi.export('instance_id', instance.id)
pulumi.export('instance_type', instance.instance_type)
This kept all my previous resources, and adds only the new one.
Just one last question:
Do you recommend
test_4 = aws.ec2.Instance.get(
resource_name='test-4',
id='xxxxx',
)
this way of putting my existing resources in the program, from what i have understood if give the same name and id it treats it as same resource.echoing-dinner-19531
11/26/2024, 9:26 AMhappy-iron-52048
11/26/2024, 9:52 AMaws.ec2.Instance(resource_name="test-8",id="xxxxxxxxxxxx")
Error:
TypeError: Instance._internal_init() got an unexpected keyword argument 'id'
python SDK version: 3.138.0
Am i using some older version?echoing-dinner-19531
11/26/2024, 9:56 AMinstance = aws.ec2.Instance(
f"test-8",
ami="xxxxxxxxxx",
instance_type="c5n.4xlarge",
key_name="xxxxxxxxxx",
subnet_id="xxxxxxxxxx",
vpc_security_group_ids=["xxxxxxx"],
iam_instance_profile="xxxxxxxxx",
)
Just leave that code exactly as is. The state file tracks the id for "test-8" you don't need it in your program as well.happy-iron-52048
11/26/2024, 9:57 AM