使用预先安装的Terraform插件,而不是使用terraform init下载它们

har*_*vmb 5 terraform

在terraform init使用Terraform时运行时,0.11.3我们收到以下错误:

初始化提供程序插件... - 在https://releases.hashicorp.com上检查可用的提供程序插件...

安装提供程序"template"时出错:获取 https://releases.hashicorp.com/terraform-provider-template/:read tcp 172.25.77.25:53742->151.101.13.183:443:read:peer reset by peer.

Terraform分析配置和状态,并自动下载所用提供商的插件.但是,尝试下载此插件时发生意外错误.

如果由于某种原因Terraform无法访问插件存储库,可能会导致这种情况.如果防火墙阻止访问,则可能无法访问存储库.

如果您的环境中无法或不希望自动安装,您可以通过下载合适的分发包并将插件的可执行文件放在以下目录中来手动安装插件:terraform.d/plugins/linux_amd64

我意识到这是因为https://releases.hashicorp.com域的连接问题.出于某些显而易见的原因,我们将不得不调整此连接问题,因为控制服务器和Hashicorp的服务器之间存在一些SSL和防火墙问题.

我们有什么方法可以通过从Hashicorp的服务器下载插件并将它们复制到控制服务器来绕过这个?或者任何其他替代方法,以避免尝试从Hashicorp的服务器下载东西?

yda*_*coR 19

您可以通过将插件放在与terraform二进制文件相同的目录中或通过设置-plugin-dir标志来使用预安装的插件.

您还可以使用该terraform-bundle工具自动构建所需的每个提供程序包.

我在Docker容器中的CI管道中运行Terraform,所以有一个类似于这样的Dockerfile:

FROM golang:alpine AS terraform-bundler-build

RUN apk --no-cache add git unzip && \
    go get -d -v github.com/hashicorp/terraform && \
    go install ./src/github.com/hashicorp/terraform/tools/terraform-bundle

COPY terraform-bundle.hcl .

RUN terraform-bundle package terraform-bundle.hcl && \
    mkdir -p terraform-bundle && \
    unzip -d terraform-bundle terraform_*.zip

####################

FROM python:alpine

RUN apk add --no-cache git make && \
    pip install awscli

COPY --from=terraform-bundler-build /go/terraform-bundle/* /usr/local/bin/
Run Code Online (Sandbox Code Playgroud)

请注意,完成的容器映像也会添加git,makeAWS CLI也因为我还需要使用此容器的CI作业中的那些工具.

该terraform-bundle.hcl则看起来像这样(取自terraform-bundle自述):

terraform {
  # Version of Terraform to include in the bundle. An exact version number
  # is required.
  version = "0.10.0"
}

# Define which provider plugins are to be included
providers {
  # Include the newest "aws" provider version in the 1.0 series.
  aws = ["~> 1.0"]

  # Include both the newest 1.0 and 2.0 versions of the "google" provider.
  # Each item in these lists allows a distinct version to be added. If the
  # two expressions match different versions then _both_ are included in
  # the bundle archive.
  google = ["~> 1.0", "~> 2.0"]

  # Include a custom plugin to the bundle. Will search for the plugin in the 
  # plugins directory, and package it with the bundle archive. Plugin must have
  # a name of the form: terraform-provider-*, and must be build with the operating
  # system and architecture that terraform enterprise is running, e.g. linux and amd64
  customplugin = ["0.1"]
}
Run Code Online (Sandbox Code Playgroud)