CodeBuild – Lambda Version

CodeBuild – Lambda Version

On January 6, an update to the CodeBuild build environment was announced.

Customers can now select AWS Lambda as a new compute mode for build execution. Customers will experience faster builds with AWS Lambda’s near instant start-up times. Customers will also gain cost optimization when using Lambda compute as it is billed for per-second of usage.

AWS CodeBuild now supports AWS Lambda compute

We will introduce CodeBuild using Lambda.

Environment

Diagram of CodeBuild - Lambda Version

Create a Lambda type CodeBuild project.
The Lambda image will use the ARM type, Python 3.12 version.

In this case, we will use the Python package yt-dlp to download videos from YouTube.
Place the downloaded video file in an S3 bucket.

CloudFormation template files

The above configuration is built with CloudFormation.
The CloudFormation template is placed at the following URL

GitHub
awstut-fa/149 at main · awstut-an-r/awstut-fa Contribute to awstut-an-r/awstut-fa development by creating an account on GitHub.

Explanation of key points of template files

CodeBuild

Resources:
  CodeBuildProject:
    Type: AWS::CodeBuild::Project
    Properties: 
      Artifacts:
        Type: NO_ARTIFACTS
      Cache: 
        Type: NO_CACHE
      Environment: 
        ComputeType: !Ref ProjectEnvironmentComputeType
        EnvironmentVariables:
          - Name: BUCKET_NAME
            Type: PLAINTEXT
            Value: !Ref BucketName
        Image: !Ref ProjectEnvironmentImage
        ImagePullCredentialsType: CODEBUILD
        Type: !Ref ProjectEnvironmentType
      LogsConfig: 
        CloudWatchLogs:
          GroupName: !Ref LogGroup
          Status: ENABLED
      Name: !Sub "${Prefix}-project"
      ServiceRole: !GetAtt CodeBuildRole.Arn
      Source: 
        Type: NO_SOURCE
        BuildSpec: !Sub |
          version: 0.2
          
          phases:
            install:
              commands:
                - pip3 install yt-dlp
            build:
              commands:
                - yt-dlp -o /tmp/output.mp4 'https://www.youtube.com/watch?v=TqqaSD2qTdY'
                
                - ls -al /tmp
                - aws s3 cp /tmp/output.mp4 s3://$BUCKET_NAME/
      Visibility: PRIVATE
Code language: YAML (yaml)

There are four points.

The first and second points are the ComputeType and Type properties.
In this case, we want to create a minimum build environment (1G memory) using an ARM type Lambda.
So we specify “BUILD_LAMBDA_1GB” for the former and “ARM_LAMBDA_CONTAINER” for the latter.

For details, please refer to the following page.

あわせて読みたい
Build environment compute modes and types - AWS CodeBuild In CodeBuild, you can specify the compute and runtime environment image that CodeBuild uses to run your builds. Compute refers to the computing engine (the CPU,...

The third point is the Image property.
In this case, we want to create a Python 3.12 version of the environment.
So we specify “aws/codebuild/amazonlinux-aarch64-lambda-standard:python3.12” for this property.

For details, please refer to the following page.

あわせて読みたい
Docker images provided by CodeBuild - AWS CodeBuild A supported image is the latest major version of an image available in CodeBuild and is updated with minor and patch version updates. CodeBuild optimizes the pr...

The fourth point is the Source property.
Include the buildspec.yml directly in this property.

Install yt-dlp during the install phase.
The package is installed using the Python package management tool pip.

In the build phase, we will first download a video from YouTube using yt-dlp.
The video we will download is from the official AWS channel.

The key point is where to download the video.
From the following constraint, the download destination is /tmp.

AWS Lambda doesn’t support writing to files outside /tmp. The included package managers are configured to use the /tmp directory by default for downloading and referencing packages.

Limitations of AWS Lambda compute

Upload the downloaded video file to the S3 bucket.
This operation uses the AWS CLI.
As quoted below, the Lambda version of CodeBuild supports the AWS CLI.

AWS Lambda supports the following tools: AWS CLI v2, AWS SAM CLI, git, go, Java, Node.js, Python, pip, Ruby, and .NET.

Which tools and runtimes will be included in the curated runtime environment docker images which run on AWS Lambda?

(Reference) S3 bucket

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref Prefix
      AccessControl: Private
Code language: YAML (yaml)

This is the bucket where the downloaded video files will be placed.

No special settings are required.

Architecting

Use CloudFormation to build this environment and check its actual behavior.

Create CloudFormation stacks and check the resources in the stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please see the following page.

あわせて読みたい
CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

Check the CodeBuild project from the AWS Management Console.

Detail of CodeBuild 01.

Looking at the Environment, the build environment is indeed built with an ARM type Lambda.
The image is Python 3.12 version with 1GB memory.

Detail of CodeBuild 02.

The buildspec.yml is also as specified in the CloudFormation template.

Action Check

Now that you are ready, start the CodeBuild project.
Wait a moment, the build has completed successfully.

Below is the status of each phase.

Detail of CodeBuild 03.

Indeed, all phases have been completed successfully.

Next, check the log at build time.
The log shows that the commands specified in the buildspec.yml are executed in order.

Detail of CodeBuild 04.

I install yt-dlp first.

Detail of CodeBuild 05.

The yt-dlp command is then used to download videos from YouTube.

If you look in the /tmp directory, you will see that the output.mp4 file has indeed been installed.

We then use the AWS CLI to upload the output.mp4 to the S3 bucket.

Finally, check the S3 bucket.

Detail of S3 01.

The video file was indeed saved in the bucket.

Summary

CodeBuild with Lambda was introduced.