Building Terraform - linux-on-ibm-z/docs GitHub Wiki

Building Terraform

Below version of Terraform is available in respective distribution at the time of creation of these build instructions:

  • SLES (15 SP5) have 0.13.4

The instructions provided below specify the steps to build Terraform 1.8.1 on Linux on IBM Z for following distributions:

  • RHEL (7.8, 7.9, 8.6, 8.8, 8.9, 9.0, 9.2, 9.3)
  • SLES (12 SP5, 15 SP5)
  • Ubuntu (20.04, 22.04, 23.10)

General Notes:

  • When following the steps below please use a standard permission user unless otherwise specified.
  • A directory /<source_root>/ will be referred to in these instructions, this is a temporary writable directory anywhere you'd like to place it.

1) Build using script

If you want to build terraform using manual steps, go to STEP 2.

Use the following commands to build terraform using the build script. Please make sure you have wget installed.

wget -q https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Terraform/1.8.1/build_terraform.sh

# Build terraform
bash build_terraform.sh  [Provide -t option for executing build with tests]

In case of error, check logs for more details or go to STEP 2 to follow manual build steps.

2) Install dependencies

export SOURCE_ROOT=/<source_root>/
  • RHEL (7.8, 7.9)

    sudo yum install -y git wget tar gcc diffutils rh-git227-git.s390x zip unzip
    
    #Enable git 2.27.0
    source /opt/rh/rh-git227/enable
    
  • RHEL (8.6, 8.8, 8.9, 9.0, 9.2, 9.3)

    sudo yum install -y git wget tar gcc diffutils zip unzip
    
  • SLES (12 SP5, 15 SP5)

    sudo zypper install -y git-core wget tar gcc gzip zip unzip
    
  • Ubuntu (20.04, 22.04, 23.10)

    sudo apt-get update
    sudo apt-get install -y git wget tar gcc zip unzip
    
  • Install Go version 1.22.0

    cd $SOURCE_ROOT
    export GO_VERSION="1.22.0"
    wget -q https://storage.googleapis.com/golang/go"$GO_VERSION".linux-s390x.tar.gz
    chmod ugo+r go"$GO_VERSION".linux-s390x.tar.gz
    sudo tar -C /usr/local -xzf go"$GO_VERSION".linux-s390x.tar.gz
    sudo ln -sf /usr/local/go/bin/go /usr/bin/
    sudo ln -sf /usr/local/go/bin/gofmt /usr/bin/
    sudo ln -sf /usr/bin/gcc /usr/bin/s390x-linux-gnu-gcc # (Only on RHEL and SLES)
    go version
    

3) Create Terraform binary

  #Download and install terraform
  export GOPATH=$SOURCE_ROOT
  export PATH=$GOPATH/bin:$PATH
  mkdir -p $GOPATH/src/github.com/hashicorp
  cd $GOPATH/src/github.com/hashicorp
  git clone -b v1.8.1 https://github.com/hashicorp/terraform.git
  cd terraform
  go install .

The terraform binary will be created in folder $GOPATH/bin/

  • Copying binary to /usr/bin and verify installation

    sudo cp $GOPATH/bin/terraform /usr/bin/
    terraform -version
    terraform -help
    

4) Execute Test Cases(Optional)

4.1) Run unit tests

cd $GOPATH/src/github.com/hashicorp/terraform
go test -v ./...

4.2) Run race tests

cd $GOPATH/src/github.com/hashicorp/terraform
go test -race ./internal/terraform ./internal/command ./internal/states

4.3) Run end-to-end tests

  • Download and build necessary terraform provider plugins locally

    PROVIDER_PLUGIN_LOCAL_MIRROR_PATH="$HOME/.terraform.d/plugins"
    mkdir -p $PROVIDER_PLUGIN_LOCAL_MIRROR_PATH
    
    cd $GOPATH/src/github.com/hashicorp
    git clone https://github.com/hashicorp/terraform-provider-null.git
    cd terraform-provider-null
    git checkout v3.2.2
    go build
    BIN_PATH="${PROVIDER_PLUGIN_LOCAL_MIRROR_PATH}/registry.terraform.io/hashicorp/null/3.2.2/linux_s390x"
    mkdir -p $BIN_PATH
    mv terraform-provider-null $BIN_PATH/terraform-provider-null_v3.2.2
    git checkout v3.1.0
    go build
    mv terraform-provider-null terraform-provider-null_v3.1.0_x5
    zip terraform-provider-null_3.1.0_linux_s390x.zip terraform-provider-null_v3.1.0_x5
    mv terraform-provider-null_3.1.0_linux_s390x.zip $PROVIDER_PLUGIN_LOCAL_MIRROR_PATH/registry.terraform.io/hashicorp/null/
    git checkout v2.1.0
    go build
    BIN_PATH="${PROVIDER_PLUGIN_LOCAL_MIRROR_PATH}/registry.terraform.io/hashicorp/null/2.1.0/linux_s390x"
    mkdir -p $BIN_PATH
    mv terraform-provider-null $BIN_PATH/terraform-provider-null_v2.1.0_x4
    BIN_PATH="${PROVIDER_PLUGIN_LOCAL_MIRROR_PATH}/registry.terraform.io/hashicorp/null/1.0.0+local/linux_s390x"
    mkdir -p $BIN_PATH
    cp $GOPATH/src/github.com/hashicorp/terraform/internal/command/e2etest/testdata/vendored-provider/terraform.d/plugins/registry.terraform.io/hashicorp/null/1.0.0+local/os_arch/terraform-provider-null_v1.0.0 $BIN_PATH/
    
    cd $GOPATH/src/github.com/hashicorp
    git clone https://github.com/hashicorp/terraform-provider-aws.git
    cd terraform-provider-aws
    git checkout v5.33.0
    go build
    BIN_PATH="${PROVIDER_PLUGIN_LOCAL_MIRROR_PATH}/registry.terraform.io/hashicorp/aws/5.33.0/linux_s390x"
    mkdir -p $BIN_PATH
    mv terraform-provider-aws $BIN_PATH/terraform-provider-aws_v5.33.0
    
    cd $GOPATH/src/github.com/hashicorp
    git clone https://github.com/hashicorp/terraform-provider-template.git
    cd terraform-provider-template
    git checkout v2.2.0
    go build
    BIN_PATH="${PROVIDER_PLUGIN_LOCAL_MIRROR_PATH}/registry.terraform.io/hashicorp/template/2.2.0/linux_s390x"
    mkdir -p $BIN_PATH
    mv terraform-provider-template $BIN_PATH/terraform-provider-template_v2.2.0
    BIN_PATH="${PROVIDER_PLUGIN_LOCAL_MIRROR_PATH}/registry.terraform.io/hashicorp/template/2.1.0/linux_s390x"
    mkdir -p $BIN_PATH
    mv $GOPATH/src/github.com/hashicorp/terraform/internal/command/e2etest/testdata/plugin-cache/cache/registry.terraform.io/hashicorp/template/2.1.0/os_arch/terraform-provider-template_v2.1.0_x4 $BIN_PATH/
    
    BIN_PATH="${PROVIDER_PLUGIN_LOCAL_MIRROR_PATH}/example.com/awesomecorp/happycloud/1.2.0/linux_s390x"
    mkdir -p $BIN_PATH
    mv $GOPATH/src/github.com/hashicorp/terraform/internal/command/e2etest/testdata/local-only-provider/terraform.d/plugins/example.com/awesomecorp/happycloud/1.2.0/os_arch/terraform-provider-happycloud_v1.2.0 $BIN_PATH/
    
  • Change CLI configurations temporarily to use locally built provider plugins

    cd $HOME
    wget https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Terraform/1.8.1/patch/.terraformrc
    sed -i "s#HOME__PATH#$HOME#g" .terraformrc
    
  • Run e2e tests

    cd $GOPATH/src/github.com/hashicorp/terraform
    TF_ACC=1 go test -v ./internal/command/e2etest
    
  • Revert CLI configurations back to default

    cd $HOME
    mv .terraformrc .terraformrc.e2etest
    

Note: In case of unexpected test failures, please try re-running the test individually. Use below command to execute the test individually:

go test -v <package_name> -run <failed_test_name>

5) Verification(Optional)

Please follow instructions here to verify the built Terraform binary.

Note: The docker provider (kreuzwerker/docker) used in this tutorial should be built on s390x with steps similar to those in Section 4.3 before running the verification.

References