Can't Specify Runtime Version in CodeBuild

Viewed 32

I thought this would be straightforward but so far it isn't. I'm trying to specify the runtime version in a buildspec (using CDK in python). In the below snippet, if I specify the nodejs version without quotes then python complains that nodejs is an undefined variable. If I put it in quotes then it doesn't update the runtime version.

How do I specify the version?

        codebuild.Project(
            self,
            "client-frontend-react-codebuild",
            project_name="frontend-react",
            build_spec=codebuild.BuildSpec.from_object(

                {
                    "version": "0.2",
                    "phases": {
                        "install": {
                            "runtime-versions": {nodejs: "16.x"}
                        },  # this doesn't work
                        "build": {
                            "commands": [
                                "echo Hello, World!",
                            ]
                        },
                    },
                }
)
1 Answers

This is because by default, the build image used in the project is aws/codebuild/standard:1.0, which doesn't support runtime-versions.

From the docs on runtime-versions:

A runtime version is supported with the Ubuntu standard image 2.0 or later and the Amazon Linux 2 standard image 1.0 or later.

From the docs on the Project construct, specifically the environment prop:

default: BuildEnvironment.LinuxBuildImage.STANDARD_1_0

To enable support for runtime-versions, specify a build image that supports it. For example, here's Standard 6.0, which is Ubuntu 22.04:

codebuild.Project(
    self,
    "client-frontend-react-codebuild",
    project_name="frontend-react",
    build_spec=codebuild.BuildSpec.from_object(
    {
        "version": "0.2",
        "phases": {
            "install": {
                "runtime-versions": {"nodejs": "16.x"}
            },  # this doesn't work
            "build": {
                "commands": [
                    "echo Hello, World!",
                ]
            },
        },
    }
    ),
    environment=codebuild.BuildEnvironment(
        build_image=codebuild.LinuxBuildImage.STANDARD_6_0
    )
)
Related