Open chrisbecke opened 1 month ago
Thinking about it some more:
resource "aws_iam_role" "this" {
name = "my-role"
assume_role_policy = ""
}
resource "aws_iam_role_policy_attachment" "this" {
count = 1
policy_arn = "arn:aws:iam::aws:policy/AutoScalingFullAccess"
role = aws_iam_role.this.name
}
resource "aws_iam_instance_profile" "this" {
name = "my-instance-profile"
role = aws_iam_role.this.name
}
resource "aws_launch_template" "this" {
iam_instance_profile {
name = aws_iam_instance_profile.this.name
}
// This is needed to ensure the roles associated with the instance profile are attached before template instances need them.
depends_on = [ aws_iam_role_policy_attachment.this ]
}
resource "aws_autoscaling_group" "this" {
name = "my-asg"
max_size = 1
min_size = 1
launch_template {
id = aws_launch_template.this.id
}
}
Without the depends_on, there is a race condition - instances cannot depend on the policy attachments existing when the infrastructure is being created or destroyed.
This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days
Description
Given an autoscaling group that defines lifecycle hooks and attaches a role policy of AutoScalingFullAccess...
If the terraform is destroyed, or if an update triggers a create-before-destroy on the autoscaling group, terraform will destroy the
aws_iam_role_policy_attachment
s before the aws_autoscaling_group has been destroyed.The aws_autoscaling_group will be waiting for the EC2_INSTANCE_TERMINATING lifecycle hook to finish on each instance to proceed, and if the instances process their own lifecycle hooks to shut down, they will need an attached instance roke policy with "autoscaling:CompleteLifecycleAction".
This introduces a race condition where Instances have their instance role policies stripped before the instances can finish any kind of cleanup.
Versions
Module version [Required]: v5.50.0
Terraform version:
provider registry.opentofu.org/hashicorp/aws v5.50.0
provider registry.opentofu.org/hashicorp/template v2.2.0
Reproduction Code [Required]
Steps to reproduce the behavior:
Expected behavior
The Instances can react to the lifecycle hook (A bash script for example) and respond using the aws cli to CONTINUE the lifecyclehook
Actual behavior
The instances cannot CONTINUE the lifecycle hook with a role-or-policy does not exist error.