简体   繁体   中英

How can I configure an AWS autoscaling group/launch configuration with Terraform to only run a cron job on one EC2 instance?

I am building a Terraform configuration for an AWS system that requires exactly one instance in the autoscaling group to run a cronjob on a daily cadence.

Is it possible to express this in HCL with the AWS provider? What is the "best practice" advice from the community? I would prefer not to use Batch compute, as that requires ECS.

Many thanks in advance.

If you want to use cron on an instance, you can create a single-instance autoscaling by setting max_size, min_size, and desired_capacity all to 1 like so:

resource "aws_autoscaling_group" "foo" {
  name             = "foo"
  max_size         = 1
  min_size         = 1
  desired_capacity = 1
# Other params...
}

If you ever want to replace the instance, you can set it's status to 'Unhealthy' and the ASG will replace it. If you need more protection from running multiple copies of your code at once, you will need to do something more complicated at the application level (acquiring a distributed lock, for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM