terraform-provider-aws: aws_appstream_fleet_stack_association not working correctly

Community Note

  • Please vote on this issue by adding a šŸ‘ reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave ā€œ+1ā€ or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform CLI and Terraform AWS Provider Version

Affected Resource(s)

  • aws_appstream_fleet_stack_association

Terraform Configuration Files

The example provided here does not seem to create an aws_appstream_fleet_stack_association in the state, yet the association itself seems to be created in AWS.

resource "aws_appstream_stack" "stack" {
  count = local.appstream.create_as ? 1 : 0

  name         = "Stack"

  tags = local.tags
}

resource "aws_appstream_fleet" "fleet" {
  count = local.appstream.create_as ? 1 : 0

  name                               = "Fleet"
  image_name                         = "Firefox-Image"
  instance_type                      = "stream.standard.small"
  idle_disconnect_timeout_in_seconds = 3600
  enable_default_internet_access     = false
  fleet_type                         = "ALWAYS_ON"

  compute_capacity {
    desired_instances = 2
  }

  vpc_config {
    subnet_ids         = module.vpc.private_subnets
  }

  tags = local.tags
}

resource "aws_appstream_fleet_stack_association" "stack_fleet" {
  count = local.appstream.create_as ? 1 : 0

  fleet_name = aws_appstream_fleet.fleet.0.name
  stack_name = aws_appstream_stack.stack.0.name
}

Expected Behavior

The association should be created in the state

Actual Behavior

Terraform believes that the association was not created in a subsequent run, and then tries to create it - but finds that it already exists. You then have to import the resource manually.

If you then update or taint a stack, there appears to be a missing dependency to re-create the association.

Steps to Reproduce

  1. Create the resources in the example or the above code.
  2. Update the stack definition or taint it.
  3. Try to apply it

Output (new resource)

Error: error reading AppStream Fleet Stack Association (Stack/Fleet): No stack "Fleet" associated with fleet "Stack"

Output (updating stack resource)

aws_appstream_stack.stack[0]: Destroying... [id=Stack]

Error: error deleting Appstream Stack (Stack): ResourceInUseException: Cannot delete stack Stack. This stack has fleets associated with it. Only stacks with no fleets associated with it can be deleted.

Looking at the creating a resource error output, it looks like ā€œfleet_nameā€ and ā€œstack_nameā€ are somehow inverted somewhere in the logic. I have double checked my references and they are correct - which leads me to believe they might be swapped in the logic of the provider somehwere…

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 28
  • Comments: 19

Commits related to this issue

Most upvoted comments

A temporary workaround (until this bug is fixed), that I’m testing is:

resource "null_resource" "temp_associate_fleet_stack" {
  # Requires the fleet and stack to be provisioned first. This also ensures
  # that the disassociation occurs before the fleet and stack are destroyed.
  depends_on = [aws_appstream_fleet.fleet,  aws_appstream_stack.stack]

  triggers = {
    appstream_fleet = aws_appstream_fleet.fleet.id
    appstream_stack = aws_appstream_stack.stack.id
  }

  provisioner "local-exec" {
    command = "aws appstream associate-fleet --fleet-name ${self.triggers.appstream_fleet} --stack-name ${self.triggers.appstream_stack}"
  }

  provisioner "local-exec" {
    when = destroy
    command = "aws appstream disassociate-fleet --fleet-name ${self.triggers.appstream_fleet} --stack-name ${self.triggers.appstream_stack}"
  }
}

Edits: - From testing, this works some times, but it’s still flakey with some inconsistent terraform state errors. - Modified each provisioner to get stack/fleet names from triggers.

This worked for me… But we really need a permanent fix. It’s only one line of code šŸ˜‰

I have the same issue using: hashicorp/aws v4.6.0

resource "aws_appstream_fleet_stack_association" "example" {
   fleet_name = aws_appstream_fleet.this.name
   stack_name = aws_appstream_stack.stack.name
}

Error: error reading AppStream Fleet Stack Association (AppStreamStack/wav-dev-asf): No stack "wav-dev-asf" associated with fleet "AppStreamStack"

Any update on this, please?

I have encountered this issue as well. Hoping for a fix

FYI: I sent an email with details to one of the contributors of the commit that caused this issue. Hopefully they will respond. I will create a PR if needed…

Moved my comment from the duplicate issue.

The bug seems to be caused by this line: https://github.com/hashicorp/terraform-provider-aws/blob/60bd245a93893c6eefae9a84f72f3a22ed7c2305/internal/service/appstream/fleet_stack_association.go#L69

Where the arguments to EncodeStackFleetID are not in the right order.

The function for reference:

func EncodeStackFleetID(fleetName, stackName string) string {
	return fmt.Sprintf("%s/%s", fleetName, stackName)
}

A temporary workaround (until this bug is fixed), that I’m testing is:

resource "null_resource" "anno_associate_fleet_stack" {
  # Requires the fleet and stack to be provisioned first. This also ensures
  # that the disassociation occurs before the fleet and stack are destroyed.
  depends_on = [aws_appstream_fleet.fleet,
                aws_appstream_stack.stack]

  triggers = {
    appstream_fleet = aws_appstream_fleet.fleet.id
    appstream_stack = aws_appstream_stack.stack.id
  }

  provisioner "local-exec" {
    command = "aws appstream associate-fleet --fleet-name Fleet --stack-name Stack"
  }

  provisioner "local-exec" {
    when = destroy
    command = "aws appstream disassociate-fleet --fleet-name Fleet --stack-name Stack"
  }
}

EDIT: from testing, this works some times, but it’s still flaky with some inconsistent terraform state errors.

I am facing the exact issue! any solution to resolve this issue?

This functionality has been released in v4.21.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

facing the same error,any fix for this…