https://pulumi.com logo
Title
a

ambitious-afternoon-55254

07/21/2021, 3:52 PM
I’m trying to create an EC2 Spot instance and I’m running into 2 problems. 1. The tags are assigned to the spot request, but not the instance itself. How can I create a spot instance that is tagged?
tags_all
doesn’t seem to do anything in my tests. 2. When I delete the resource (remove the code from pulumi and
pulumi up
) — the spot request is cancelled, but the underlying EC2 instance continues to run. How do I get pulumi to shut down the EC2 instance as well?
import pulumi           
import pulumi_aws as aws

test_instance = aws.ec2.SpotInstanceRequest(
    "test_spot_instance",
    ami="ami-09e67e426f25ce0d7",
    spot_price='0.55',
    spot_type='one-time',
    subnet_id='subnet-302a5579',
    associate_public_ip_address=True,
    ebs_optimized=True,
    ebs_block_devices=[ebs],
    iam_instance_profile="ecsInstanceRole",
    instance_type="m5.xlarge",
    key_name="test-keypair",
    source_dest_check=True,
    tags={
        "Name": "TestSpot",
    },
    tags_all={
        "Name": "TestSpotAll",
        "tags_all": "yes",
    },
)
b

bumpy-grass-54508

07/21/2021, 5:06 PM
i'm not sure what your use case it, but it sounds like you might want to use an AutoScalingGroup with Spot instances, instead of just using a spot request. you can create a
LaunchTemplate
and then a
AutoScalingGroup
that references the template. your ASG can say it wants X number of instances and will do its best to keep that many spot instances up and running (depending on your spot price limit which you can control). then when you delete the ASG it will also remove the instances it has under its control
then on top of that, the LaunchTemplate has
TagSpecifications
where you can specify certain tags that will get applied to the instances, the volumes, and the spot-instance-requests all seperately
i will say that for years i have been confused on the ever-converging set of features between
spot fleet
/
ec2-fleet
/
spot-requests
/ and
autoscaling groups
but for my current use cases, autoscaling groups (with spot instances) is the best option for me. instance weighting, keeping capacity at a certain amount, and using spot pricing make for a good time
g

great-sunset-355

07/22/2021, 9:45 AM
I do not have much experience with EC2 but I've learned that using AutoScaling group even for 1 instance is the best solution in most cases
☝️ 1