AWS CodeBuild /codebuild/output/tmp/script.sh: terraform: Exec format error

Viewed 181

I am using AWS Codebuild to build infrastructure through terraform. I created this buildspec.yaml file.

version: 0.2

phases: 
  install:
    commands:
      - "apt install unzip -y"
      - "wget https://releases.hashicorp.com/terraform/1.1.8/terraform_1.1.8_linux_arm64.zip"
      - "unzip terraform_1.1.8_linux_arm64.zip"
      - "mv terraform /usr/local/bin/"

  pre_build:
    commands:
      - terraform init

  build:
    commands:
      - terraform plan
      - terraform apply -auto-approve
  post_build:
    commands:
      - echo "terraform apply completed on `date`"

I am able to install terraform successfully but not able to run terraform init and initialize the code. I am getting this error:

[Container] 2022/06/23 10:25:29 Phase complete: INSTALL State: SUCCEEDED
[Container] 2022/06/23 10:25:29 Phase context status code:  Message: 
[Container] 2022/06/23 10:25:29 Entering phase PRE_BUILD
[Container] 2022/06/23 10:25:29 Running command terraform init
/codebuild/output/tmp/script.sh: 4: /codebuild/output/tmp/script.sh: terraform: Exec format error

This is my terraform setup.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  backend "s3" { }
}

I tried different things such changing aws provider version. I turned on and off "privileged" flag on codebuild project. Can someone please suggest what I am doing wrong?

1 Answers

You are using TF binary for arm64 architectures. Probably you should be using amd64 instead:

      - "wget https://releases.hashicorp.com/terraform/1.1.8/terraform_1.1.8_linux_amd64.zip"
      - "unzip terraform_1.1.8_linux_amd64.zip"
Related