https://pulumi.com logo
Title
m

millions-furniture-75402

06/24/2021, 12:34 PM
How do I set log retention on EC2 Instances that are part of an autoscaling group? With a single EC2 Instance I handle it like so:
const myEc2 = new aws.ec2.Instance(...);

myEc2.id.apply(
  myEc2Id =>
    new aws.cloudwatch.LogGroup(`${appName}-ec2-lg`, {
      name: `/custom/ec2/${appName}/${myEc2Id}`,
      retentionInDays: 14,
    }),
);
I would think I could retrieve a collection of EC2 Instances from the autoscaling group, but I’m not finding that to be true. https://www.pulumi.com/docs/reference/pkg/aws/autoscaling/group/#outputs
b

brave-planet-10645

06/24/2021, 12:49 PM
I don't think you can even get the instances in an ASG from the AWS SDK. Could you tag the instances (with something like the ASG name) and then use getInstances() and filter by tag?
m

millions-furniture-75402

06/24/2021, 1:02 PM
Maybe? I haven’t wrapped my head around how I name or tag the instances in the group yet… I’d like
<appName>-<availabilityZone>
as the name.
b

brave-planet-10645

06/24/2021, 1:13 PM
Looks like if you're using launch templates, you can set them here: https://www.pulumi.com/docs/reference/pkg/aws/ec2/launchtemplate/#tagspecifications_nodejs
m

millions-furniture-75402

06/24/2021, 1:14 PM
Yeah, but I’m unsure of how to do interpolation when an instance is created. e.g. I want it to automatically tag it with
Name: <appName>-<availabilityZone>
Potentially I could create the instances and attachment them to a Group, but then I’m bypassing the template, also Pulumi doesn’t support this type of attachment. https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/autoscaling.html#AutoScaling.Client.attach_instances https://www.pulumi.com/docs/reference/pkg/aws/autoscaling/attachment/
b

brave-planet-10645

06/24/2021, 5:06 PM
Let me take a look and put together a quick demo to see what's possible
👍 1
Ok, so I got this working. In the example below, you can see that I've tagged the EC2 instance with a tag called
appName
(although you can tag them whatever you want) Then I'm getting the instances using
aws.ec2.getInstances()
(worth noting that the instance data that's returned is not the same as on the
aws.ec2.Instance
resource)
const myAppName = "pk-app";

let instances = pulumi.output(aws.ec2.getInstances({
    instanceTags: {
        appName: myAppName
    }
}));

instances.ids.apply(ids => {
    ids.map(id => {
        new aws.cloudwatch.LogGroup(`${appName}-ec2-lg`, {
            name: pulumi.interpolate`/custom/ec2/${myAppName}/${id}`,
            retentionInDays: 14
        })
    })
});
m

millions-furniture-75402

06/25/2021, 9:13 PM
Ahh, interesting approach, thank you, I will try it. I think I’ve given up on the ASG route, ran into pitfall after pitfall.