amazon-web-servicesaws-cdkaws-codepipelineaws-codebuildamazon-linux

Cannot use amazon-linux-extras in AWS CodeBuild image


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?


Solution

  • 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"