hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.76k stars 9.12k forks source link

[Bug]: Terraform import `aws_appautoscaling_policy` resource error with / in `resource_id` and `policy_name` #29279

Open ss-edward-tran opened 1 year ago

ss-edward-tran commented 1 year ago

Description

I'm trying to import an AWS resource of type aws_appautoscaling_policy from an old terraform state to a new one. This is the aws_appautoscaling_policy id.

ecs/service/sandbox/myservice/ecs:service:DesiredCount/ECSServiceAverageCPUUtilization:service/sandbox/myservice

its id has 4 components:

The command that I used to import the resource is:

terraform import aws_appautoscaling_policy.ecs_targettrackingcpu ecs/service/sandbox1/myservice/ecs:service:DesiredCount/ECSServiceAverageCPUUtilization:service/sandbox1/myservice

But I got this error

Error: reading Application Auto Scaling Policy (myservice): describing scaling policies: ValidationException: 1 validation error detected: Value 'sandbox' at 'scalableDimension' failed to satisfy constraint: Member must satisfy enum value set: [cassandra:table:ReadCapacityUnits, cassandra:table:WriteCapacityUnits, dynamodb:index:ReadCapacityUnits, dynamodb:index:WriteCapacityUnits, appstream:fleet:DesiredCapacity, comprehend:document-classifier-endpoint:DesiredInferenceUnits, ec2:spot-fleet-request:TargetCapacity, rds:cluster:ReadReplicaCount, elasticache:replication-group:Replicas, dynamodb:table:WriteCapacityUnits, kafka:broker-storage:VolumeSize, ecs:service:DesiredCount, sagemaker:variant:DesiredInstanceCount, comprehend:entity-recognizer-endpoint:DesiredInferenceUnits, dynamodb:table:ReadCapacityUnits, lambda:function:ProvisionedConcurrency, custom-resource:ResourceType:Property, neptune:cluster:ReadReplicaCount, elasticache:replication-group:NodeGroups, elasticmapreduce:instancegroup:InstanceCount]

I think its because in the policy name it has 2 / while it shouldn't. Thus, terraform can not parse the scalable_dimension correctly.

Note:

Anyone has encountered this kind of issue before and how to fix it? Please let me know. Thank you.

I tried to add escape character from the import command like this but it didnt seem to work:

terraform import aws_appautoscaling_policy.ecs_targettrackingcpu ecs/service\/sandbox1\/myservice/ecs:service:DesiredCount/ECSServiceAverageCPUUtilization:service\/sandbox1\/myservice

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

deryckintelematics commented 1 year ago

Looking at the code https://github.com/jdecarli/terraform-provider-aws/blob/master/aws/resource_aws_appautoscaling_policy.go#L408 In the method resourceAwsAppautoscalingPolicyImport Can see that it allows for a '/' in the resource_id but not in the policy_name

Hope that helps

singhujjwal commented 1 year ago

Is there any workaround @ss-edward-tran ?

velychkovskyi commented 1 year ago

I have the same issue - existing ECS auto-scaling policy_name contain 3-element path: service/<ecs_cluster_name/<ecs_service_name> in my case ECSServiceAverageMemoryUtilization:service/dev/mms-api Any other way to avoid slashes in policy_name and import it in terraform state?

P.S. My terraform state show aws_appautoscaling_policy.mms-api output doesn't contain exactly field named policy_name. It has 'name', 'id', 'policy_type' but not expected parameter <service-namespace>/<resource-id>/<scalable-dimension>/<policy-name>

deryckintelematics commented 1 year ago

My playing around, it seems like the autoscaling policy, if you create another with the same attributes using terraform, it does not conflict with the existing, nor does it create a duplicate. It is like it simply overwrites it. Even to the point that if it was originally in a different tfstate and you import in a new, both states still work with it on a last run wins basis.