I am installing CNI using null_resource in terraform. Now if the CNI is already installed the terraform script fails with error:
exit status 254. Output: │ An error occurred (ResourceInUseException) when calling the CreateAddon │ operation: Addon already exists.
How can I make terraform continue with execution if the CNI is already installed, rather than failing.
Below is my Configuration for installing CNI:
### Installing CNI Addon ###
resource "null_resource" "install-CNI" {
provisioner "local-exec" {
when = create
interpreter = ["bash", "-c"]
command = <<EOT
aws eks create-addon \
--cluster-name ${data.aws_eks_cluster.Custom_Dev-cluster-deploy.name} \
--addon-name vpc-cni \
--addon-version v1.11.2-eksbuild.1 \
--service-account-role-arn ${aws_iam_role.Custom_Dev-cluster.arn} \
--resolve-conflicts OVERWRITE
EOT
}
triggers = {
"before" = null_resource.eks-config-file.id
}
}
you can handle the error base on the response. if the command response contains Addon already exists
you can exit 0
and return an error if something else, it can be aws cli
permission or wrong command.
resource "null_resource" "install-CNI" {
provisioner "local-exec" {
when = create
interpreter = ["bash", "-c"]
command = <<EOT
RESULT=$(aws eks create-addon --cluster-name ${data.aws_eks_cluster.Custom_Dev-cluster-deploy.name} --addon-name vpc-cni --addon-version v1.11.2-eksbuild.1 --service-account-role-arn ${aws_iam_role.Custom_Dev-cluster.arn} --resolve-conflicts OVERWRITE 2>&1)
if [ $? -eq 0 ]
then
echo "Addon installed successfully $RESULT"
exit 0
elif [[ "$RESULT" =~ .*"Addon already exists".* ]]
then
echo "Plugin already exists $RESULT" >&2
exit 0
else
echo "Encounter error $RESULT" >&2
exit 1
fi
EOT
}
triggers = {
"before" = null_resource.eks-config-file.id
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With