3

My CDK stack defines a CodePipeline with the following CodeBuild project:

project_build = codebuild.Project(self, "ProjectBuild",
    project_name = "Build",
    source = github_source,
    environment = codebuild.BuildEnvironment(
        build_image = codebuild.LinuxBuildImage.AMAZON_LINUX_2_2,
        privileged = True),
    environment_variables = { … },
    build_spec = codebuild.BuildSpec.from_source_filename("cdk/buildspec/build.yml"))

I'm using an Amazon Linux 2 image, and want to install PostgreSQL on it, so I've made this simple buildspec test file:

version: 0.2

phases: 
  install:
    commands:
      - amazon-linux-extras install postgresql13
  build: 
    commands:
      - echo "foo"

And here is the error raised by the job:

[Container] 2021/10/12 16:50:32 Entering phase INSTALL
[Container] 2021/10/12 16:50:32 Running command amazon-linux-extras
/root/.pyenv/versions/3.8.10/bin/python: No module named amazon_linux_extras

[Container] 2021/10/12 16:50:34 Command did not exit successfully amazon-linux-extras exit status 1
[Container] 2021/10/12 16:50:34 Phase complete: INSTALL State: FAILED
[Container] 2021/10/12 16:50:34 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: amazon-linux-extras. Reason: exit status 1

I cannot understand why amazon-linux-extras would be run as a Python module. I've tried with the /usr/bin/amazon-linux-extras absolute path without better success.

I've never had any issue using amazon-linux-extras directly on Amazon Linux instances, but how am I supposed to use it in a CodeBuild context?

zessx
  • 68,042
  • 28
  • 135
  • 158

4 Answers4

9

It appears the amazon-linux-extras script is only working with Python 2, and is using the python executable.

I need to prefix my command like so to ensure the python2 executable is used:

version: 0.2

phases: 
  install:
    commands:
      - PYTHON=python2 amazon-linux-extras enable postgresql13
      - yum clean metadata
      - yum install postgresql
  build: 
    commands:
      - echo "foo"
zessx
  • 68,042
  • 28
  • 135
  • 158
0

Despite it being a AWS Linux instance, a lot of aws stuff isnt pre intalled on CodeBuild standard images. You often have to install it - like CDK or what not. Try yum install -y amazon-linux-extras before trying to call it.

lynkfox
  • 2,003
  • 1
  • 8
  • 16
  • I already thought of this, `yum` command ran well and upgraded the package (meaning it was installed), but the `amazon-linux-extras` command was still Pythonishy executed and failed. – zessx Oct 13 '21 at 07:28
  • how very weird. Try specifying your runtime env in the buildspec - I am assuming you are not meaning for it to be Python, like `-runtime nodejs: latest` (see https://docs.aws.amazon.com/codebuild/latest/userguide/runtime-versions.html for more info) – lynkfox Oct 13 '21 at 23:33
  • apologies: `runtime-versions: - nodejs: latest` – lynkfox Oct 13 '21 at 23:54
0

I use aws/codebuild/standard:7.0 CodeBuild image which doesn't seem to have amazon-linux-extras. I need it to install the Chromium browser. So instead I installed the package directly via apt.

version: 0.2

phases: 
  install:
    commands:
      # Have to manually install chromium to ensure all dependencies are installed.
      # https://stackoverflow.com/a/54531593/556678
      - curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
      - echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
      - apt-get -y update
      - apt-get -y install google-chrome-stable
  build: 
    commands:
      - echo "foo"
esengineer
  • 9,514
  • 7
  • 45
  • 69
-1

This error seems to be caused by the fact that Amazon Linux 2 uses Python 2.7 and the pyenv environment uses Python 3.

CodeBuild seems to have been using pyenv. So the amazon-linux-extras command will work by creating a symbolic link as follows.

ln -s /lib/python2.7/site-packages/amazon_linux_extras ~/.pyenv/versions/3.8.10/lib/python3.7/site-packages/
5hintaro
  • 107
  • 1
  • 5