I am trying to import some resources ( created by EKS outside terraform) specifically cluster creator access entry.
The documentation provides this usage to create new access entry
locals {
access_entries = {
iam_identity_center_admin_role = {
kubernetes_groups = []
principal_arn = data.external.get_sso_admin_role.result.Arn
policy_associations = {
iam_identity_center_admin_role = {
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy"
access_scope = {
type = "cluster"
}
}
}
}
}
}
According to terraform import command docs
Before you run terraform import you must manually write a resource configuration block for the resource. The resource block describes where Terraform should map the imported object.
Based on the above understanding I tried to use the following import command which throws error
terraform import module.base.module.eks.aws_eks_access_entry.iam_identity_center_admin_role mycluster:principal arn
Before importing this resource, please create its configuration in module.base.module.eks. For example:
resource "aws_eks_access_entry" "iam_identity_center_admin_role" {
# (resource arguments)
}
The same works if I add the terror resource for eks access entry. Ofcourse when I use resource definition I dont use the locals of access entries map. So, its not correct resource problem
resource "aws_eks_access_entry" "imported_cluster_creator_eks_access_entry" {
cluster_name = local.eks_name
principal_arn = data.external.get_sso_admin_role.result.Arn
}
But if I use the following import command with locals and not explicit resource definition having this
it works . I want to know why the usage of this works? why this is used? To my understanding it is implicity used
terraform import 'module.base.module.eks.aws_eks_access_entry.this["iam_identity_center_admin_role"]' my-cluster:pricipal arn
2
Answers
The eks module defines the
aws_eks_access_entry
resource with a name calledthis
. It then uses afor_each
meta to loop over all the entries it needs to createAs mentioned in the comments,
this
doesnt do anything special, its just a name for theaws_eks_access_entry
resource. Usingthis
is just general convention for a general purpose resource in a module.you can read more on naming which says
Based on the terraform module code, the
merged_access_entries
local variable is used with theaws_eks_access_entry
resource. Since the resource you have created manually needs to be imported into the module, you have to follow the convention specified by the said module. In this case, the resource you want to import is using the logical name ofthis
:As you can see, the EKS module is also using
for_each
to create the resourceaws_eks_access_entry
, hence the need for the key when specifying theimport
command as the key-value pairs are decided from themerged_access_entries
local variable. Based on everything outlined here and in your question, you are calling the EKS module from thebase
module.When using modules, you need to know which resource and resource’s logical name are used in the module you want to import to in order to successfully import it. Since the resource in question is
"aws_eks_access_entry" "this"
, the entire import command needs to be:because one of the keys in the local
access_entries
variable which you have defined isiam_identity_center_admin
.