aws-ia / terraform-aws-control_tower_account_factory

AWS Control Tower Account Factory
Apache License 2.0
604 stars 386 forks source link

Pre/post-api-helper.sh: /bin/bash^M: bad interpreter: No such file or directory #406

Closed jenniupdates closed 7 months ago

jenniupdates commented 7 months ago

AFT Version: 1.10.3 Terraform Version & Provider Versions terraform version

v1.5.5

terraform providers

provider "aws" {
  region = "ap-southeast-1"
  assume_role {
    role_arn    = "arn:aws:iam::xxxxx:role/AWSAFTExecution"
  }
  default_tags {
    tags = {
      managed_by                  = "AFT"
    }
  }
}

Bug Description This is the error that I see in the global customisation CodeBuild for one of my account's CodePipeline. /codebuild/output/tmp/script.sh: /codebuild/output/src1872811563/src/api_helpers/pre-api-helpers.sh: /bin/bash^M: bad interpreter: No such file or directory Previously, when I ran the global customisations via invoke_customisation step function a month ago, I did not face this issue.

To Reproduce Steps to reproduce the behavior:

  1. Clone this repo
  2. Create account in aft-account-request and push code such that the account is created and enrolled in AWS Control Tower
  3. Create global/account-level customisations by creating the respective TF scripts in aft-account-customizations and aft-global-customizations repositories by following the templates available. Then push code.
  4. Invoking the respective step function to apply global- and account-level customisations onto the accounts that I specified in the step function invocation input.
  5. Wait for step function to invoke the correct accounts' CodePipelines and apply terraform.

Expected behavior It is suppose to succeed and echo "Executing Pre-API Helpers" or echo "Executing Post-API Helpers" according to the CodeBuild stage.

Related Logs 166 | [Container] 2023/11/20 06:20:46.038078 Phase complete: INSTALL State: SUCCEEDED 167 | [Container] 2023/11/20 06:20:46.038092 Phase context status code: Message: 168 | [Container] 2023/11/20 06:20:46.083943 Entering phase PRE_BUILD 169 | [Container] 2023/11/20 06:20:46.084875 Running command source $DEFAULT_PATH/api-helpers-venv/bin/activate 170 |   171 | [Container] 2023/11/20 06:20:46.091793 Running command export AWS_PROFILE=aft-target 172 |   173 | [Container] 2023/11/20 06:20:46.097911 Running command ls $DEFAULT_PATH 174 | README.md 175 | aft-venv 176 | api-helpers-venv 177 | api_helpers 178 | aws-aft-core-framework 179 | execution-input-sample.txt 180 | terraform 181 |   182 | [Container] 2023/11/20 06:20:46.107345 Running command ls $DEFAULT_PATH/api_helpers 183 | post-api-helpers.sh 184 | pre-api-helpers.sh 185 | python 186 |   187 | [Container] 2023/11/20 06:20:46.114707 Running command $DEFAULT_PATH/api_helpers/pre-api-helpers.sh 188 | /codebuild/output/tmp/script.sh: /codebuild/output/src1872811563/src/api_helpers/pre-api-helpers.sh: /bin/bash^M: bad interpreter: No such file or directory 189 |   190 | [Container] 2023/11/20 06:20:46.121156 Command did not exit successfully $DEFAULT_PATH/api_helpers/pre-api-helpers.sh exit status 126 191 | [Container] 2023/11/20 06:20:46.126628 Phase complete: PRE_BUILD State: FAILED_WITH_CONTINUE 192 | [Container] 2023/11/20 06:20:46.126646 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: $DEFAULT_PATH/api_helpers/pre-api-helpers.sh. Reason: exit status 126

Additional context When I invoked the step functions a month ago, I did not face this issue and it successfully ran the .sh scripts and echoed the correct statements. However, when I invoked it this week, it keeps stating that /bin/bash^M: bad interpreter: No such file or directory, even when I specifically ls to show that the directory and files exist. I read on stackoverflow that rather than missing directory/file issue, it could be the interpreter issue. Hence, I have also included the .sh script below for further elaboration:

#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
echo "Executing Pre-API Helpers"

Why am I facing this error and how do I resolve this issue such that the CodeBuild would run correctly (perhaps without using dos2unix)? Thanks alot in advance.

stumins commented 7 months ago

Hi @jenniupdates,

From the provided error logs:

188 | /codebuild/output/tmp/script.sh: /codebuild/output/src1872811563/src/api_helpers/pre-api-helpers.sh: /bin/bash^M: bad interpreter: No such file or directory

There appears to be an extra carriage return character ^M at the end of the failing interpreter path, i.e. /bin/bash^M.

Can you check that the shebang header #!/bin/bash in your pre-api-helpers script does not have this carriage return character?

jenniupdates commented 7 months ago

It does have the #!/bin/bash. What should I replace it with or should I remove it?

stumins commented 7 months ago

#!/bin/bash tells the system what interpreter to use, so keep the string itself, but ensure there is no carriage return character following it.

jenniupdates commented 7 months ago

OHH okay I realised it was using CRLF because I was running on a Windows, switched it back to LF and the CodePipeline finally works, thank you!