Cannot use amazon-linux-extras in AWS CodeBuild image

Viewed 1904

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?

3 Answers

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"

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.

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/
Related