我在terraform中使用null_resource安装CNI。现在,如果CNI已经安装,地形脚本将失败并出现错误:exit status 254. Output: │ An error occurred (ResourceInUseException) when calling the CreateAddon │ operation: Addon already exists.
如果CNI已经安装,而不是失败,我如何让terraform继续执行。
以下是我安装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
}
}
您可以根据响应处理错误。如果命令响应包含Addon already exists
,则可以exit 0
,如果有其他内容,则返回错误,可能是aws cli
权限或错误命令。
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
}
}