Terraform local-exec 为什么需要很长时间才能完成

huangapple go评论62阅读模式
英文:

why is terraform local-exec is taking forever to finish

问题

以下是您提供的Terraform配置文件的翻译部分:

资源 "null_resource" "example" {
  provisioner "local-exec" {
    command = <<EOT
      docker build -t imagename -f devops/Dockerfile .
      aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin xxxxx.dkr.ecr.us-east-2.amazonaws.com
      docker tag imagename:latest xxxxx.dkr.ecr.us-east-2.amazonaws.com/imagename:latest
      docker push xxxxx.dkr.ecr.us-east-2.amazonaws.com/imagename:latest
    EOT
    working_dir = "${path.module}/.."
  }
  depends_on = [
    aws_ecr_repository.example
  ]
}

资源 "aws_instance" "example" {
  # ... 实例配置 ...
  
  depends_on = [
    null_resource.example
  ]
}

这是您的Terraform配置文件的一部分,用于构建Docker镜像并将其推送到ECR。AWS实例将在null_resource完成后创建。

如果您移除null_resource,它将运行得很完美,创建EC2实例和ECR仓库。

英文:
resource &quot;null_resource&quot; &quot;example&quot; {
  provisioner &quot;local-exec&quot; {
    command = &lt;&lt;EOT
      docker build -t imagename -f devops/Dockerfile .
      aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin xxxxx.dkr.ecr.us-east-2.amazonaws.com
      docker tag imagename:latest xxxxx.dkr.ecr.us-east-2.amazonaws.com/imagename:latest
      docker push xxxxx.dkr.ecr.us-east-2.amazonaws.com/imagename:latest
    EOT
    working_dir = &quot;${path.module}/..&quot;
  }
  depends_on = [
    aws_ecr_repository.example
  ]
}

resource &quot;aws_instance&quot; &quot;example&quot; {
  # ... instance configuration ...
  
  depends_on = [
    null_resource.example
  ]
}

This is a fraction of my terraform. So basically I need to run docker build then docker push to ecr.
The aws_instance will be created after null_resource is done.

it has been running for 17mins

null_resource.example: Still creating... [30s elapsed]
null_resource.example: Still creating... [40s elapsed]
null_resource.example: Still creating... [50s elapsed]
null_resource.example: Still creating... [1m50s elapsed]
null_resource.example: Still creating... [2m50s elapsed]
null_resource.example: Still creating... [3m0s elapsed]
null_resource.example: Still creating... [4m0s elapsed]
null_resource.example: Still creating... [5m0s elapsed]
null_resource.example: Still creating... [6m0s elapsed]
null_resource.example: Still creating... [16m51s elapsed]
null_resource.example: Still creating... [17m1s elapsed]
null_resource.example: Still creating... [17m11s elapsed]

I run the commands in my cmder, in windows10. It works perfect. The size of image is 2.85GB it took only 3mins to build the image.
My null_resource is also depending on ecr, so when ecr is done creating the repo, its possible to docker push

if i remove null_resource, it runs perfectly, it creates ec2 and ecr.

答案1

得分: 1

去掉EOT后面的空格对我有效。检查以下更新后的资源块:

resource "null_resource" "example" {
  provisioner "local-exec" {
    command = <<EOT
      docker build -t imagename -f devops/Dockerfile .
      aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin xxxxx.dkr.ecr.us-east-2.amazonaws.com
      docker tag imagename:latest xxxxx.dkr.ecr.us-east-2.amazonaws.com/imagename:latest
      docker push xxxxx.dkr.ecr.us-east-2.amazonaws.com/imagename:latest
EOT
    working_dir = "${path.module}/.."
  }
  depends_on = [
    aws_ecr_repository.example
  ]
}
英文:

Remove the white space in front of the closing EOT works for me.
Check the updated resource block as below:

resource &quot;null_resource&quot; &quot;example&quot; {
  provisioner &quot;local-exec&quot; {
    command = &lt;&lt;EOT
      docker build -t imagename -f devops/Dockerfile .
      aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin xxxxx.dkr.ecr.us-east-2.amazonaws.com
      docker tag imagename:latest xxxxx.dkr.ecr.us-east-2.amazonaws.com/imagename:latest
      docker push xxxxx.dkr.ecr.us-east-2.amazonaws.com/imagename:latest
EOT
    working_dir = &quot;${path.module}/..&quot;
  }
  depends_on = [
    aws_ecr_repository.example
  ]
}

huangapple
  • 本文由 发表于 2023年4月11日 15:42:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定