Sunday, February 17, 2019

CodeBuild 2019 or "How I built perl for less than 3 cents"

This is my second (or third) dalliance with AWS CodeBuild.  I think I'm starting to like it now though.  I recently wrote a bash script to compile a version of perl to create Lambdas as part of my Perl Lambda project.  My so-called `make-a-perl` script will instantiate an EC2, download the perl source code, compile it, zip it up and copy it to an S3 bucket.

I suspected I could use AWS CodeBuild to do all of the aforementioned operations, but did not want to descend down the CodeBuild rabbit hole once more.  But alas, the temptation was just too much!

To cut to the chase, if you want to see the result of a Saturday afternoon down the CodeBuild rabbit hole visit the project here - https://github.com/rlauer6/codebuild-project-maker.

version: 0.2
phases:
  install:
    commands:
      - yum install -y gcc patch wget aws-cli tar gzip make zip tree
  pre_build:
    commands:
      - wget http://www.cpan.org/src/5.0/perl-$PERL_VERSION.tar.gz -O perl-$PERL_VERSION.tar.gz
  build:
    commands:
      - tar xfvz perl-$PERL_VERSION.tar.gz
      - cd perl-$PERL_VERSION && ./Configure -des -Dprefix=/opt/perl-$PERL_VERSION -Dman1dir=none -Dman3dir=none                                                                                       
      - make && make install DESTDIR=/tmp
artifacts:
  base-directory: '/tmp'
  files:
    - '**/*'
  name: perl-$PERL_VERSION.zip

The project explains all of the details, but to summarize, all I wanted to do was run the same commands found in my `make-a-perl` as part of a CodeBuild project.  The resulting ZIP file should contain a version of perl that is binary compatible with the the Amazon Linux version that runs in the Lambda environment.  Luckily there is a Docker image (amazonlinux) that you can use that fits this bill.  After some fiddling and experimenting, I was able to create a buildspec that accomplished the mission.  It's now an example you can find in the project.



Some tips and notes regarding this winter's afternoon project


CodeBuild is cheap - $0.005/build minute (i.e. half a US penny per minute) when using their smallest build instance (build.general1.small).  You get 100 free build minutes as part of the "free-tier", so if you're careful, CodeBuild can be free to use.

"The AWS CodeBuild free tier includes 100 build minutes of build.general1.small per month. The CodeBuild free tier does not expire automatically at the end of your 12-month AWS Free Tier term. It is available to new and existing AWS customers."

If you are interested, it takes about 5 minutes to actually build perl 5.28.1.  So if you've already exceeded your free tier minutes that's 5 build minutes * $0.005 or 2.5 cents.

Don't get confused as I did regarding how to actually get CodeBuild to bundle your artifacts.  The script I wrote in the codebuild-project-maker project now specifies a ZIP file as the artifact.  At one point I mistakenly specified that the entire perl source and installed build be written as the artifacts to my S3 bucket.  CodeBuild is SLOW writing 60+MB of individual files to S3!  Turns out I spent a whole 20 cents on that build! The script now correctly specifies that the artifacts be written as a ZIP file.  You should provide a name in the buildspec file in the artifacts section that represents the ZIP file to create.

Use the base-directory value in the buildspec to specify the location of your artifacts and use the files value to specify the individual files or paths.  Another mistake I made was to assume that this:

base-directory: '/tmp'
files:
  - 'opt/*'
name: perl-$PERL_VERSION.zip

would create a ZIP file containing the contents of the /tmp/opt/perl-5.28.1 subdirectory.  The proper syntax to include all files in all sub-directories is:

  - '**/*'

Conclusion

CodeBuild is a good thing.  It's low cost and integration into the rest of the AWS ecosystem make it a hammer just waiting for some nails.  I'm going to start looking for more nails. ;-)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.