RedisLabs / engineering-blog

Redis Labs Engineering Blog
https://engineering.redislabs.com/
5 stars 4 forks source link

posts/aws-autoscaling-groups-with-terraform/ #24

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Updating AWS autoscaling groups with Terraform and preserving desired capacity | Redis Labs Engineering Blog

Redis Labs Engineering Blog

https://engineering.redislabs.com/posts/aws-autoscaling-groups-with-terraform/

jay-jain commented 3 years ago

Thanks for the article Ariel. To clarify, if we have multiple unrelated launch templates and autoscaling groups in our AWS account as well, this still works because you are filtering on the given tags?

arielpeltz commented 3 years ago

Thanks for the article Ariel. To clarify, if we have multiple unrelated launch templates and autoscaling groups in our AWS account as well, this still works because you are filtering on the given tags?

@jay-jain Sorry for the delayed response, it was just pointed out to me. Yes the idea is that you identify the launch template with the security group.
Note however that the solution I gave here now does not work since aws provider went to 3.0. I need to update the blog, but I figured the original solution is more complicated than needed. You only need

# getting all the autoscaling groups matching my tags - at most one
data "aws_autoscaling_groups" "current" {
  filter {
    name   = "key"
    values = ["${var.my_special_tag_unique_per_deployment}"]
  }
  filter {
    name   = "value"
    values = ["yes"]
  }
}

# getting the details of the current scaling groups - at most one
data "aws_autoscaling_group" "current" {
  count = length(data.aws_autoscaling_groups.current.names)
  name  = data.aws_autoscaling_groups.current.names[count.index]
}

And then make sure to tag the auto-scaling group appropriately

resource "aws_autoscaling_group" "my_group" {
  ...
  # I want to tag the group so that I can find it
  tag {
    key                 = "${var.my_special_tag_unique_per_deployment}"
    value               = "yes"
    propagate_at_launch = false
  }
}

The rest is the same as in the blog.

Hashan-Rashmi-Perera commented 3 years ago

I think the way you have implemented is not necessary... You are always creating a new ASG. I am wondering why you use Aws lunch template if you need to create a new ASG all the time .. the idea of the lunch templates is not to create ASG all the time and if there is a modification to the application cluster ( configuration change, os patch updates, new release) ASG can be configured to use the latest version of lunch template all the time and we just need to modify the lunch template. We can create a new version of ami all the time and can update the lunch template to grab the latest version of the ami. After updating the lunch template we can do an ASG refresh (a new feature introduced by AWS to the ASGs) and with the refresh, it will take the latest version of the lunch template and do a rolling update for all the instances in the ASG.with this, without a zero downtime we can have the latest version of our AMI. And as you intend to have immutable servers, that can be preserved. You can use AWS CLI, AWS console or terrafom to do an as refresh(there is a specific module in the terraform registry)

Hashan-Rashmi-Perera commented 3 years ago

With the above mentioned approach I am gonna have unused asg launch templates.. I am still studying on this not to have too many unused lunch templates..i think there is a attribute called "default version" in a launch temple and with that we can configure not to have a new version of the launch template under any modification to the template. But I hanv't tried this is yet.

Hashan-Rashmi-Perera commented 3 years ago

There are spelling mistakes in my comments since I wrote the above comments via mobile. Sorry about that lunch= *launch temple= template

arielpeltz commented 3 years ago

Indeed refresh is probably the way to go, thanks for pointing this out. Note that I wrote this with provider version 2 where this was not available, so had to figure it out myself 😄 .

Not sure why you think I am leaving launch templates unused. I create new versions of the template.

Hashan-Rashmi-Perera commented 3 years ago

Yeah, I noticed that you wrote this in a period that this feature had not existed. But I saw your latest comments and then after I realised that still, you were not aware of these new features. I just wanted to give you a heads up Sir. Because I m still learning about these things to apply the application that I am currently working on. this blog post appeared while I was searching as a better search result and I thought it would be good to add the latest information in here since if someone refer this he would get the latest knowledge.