sessions-with-aws-sam icon indicating copy to clipboard operation
sessions-with-aws-sam copied to clipboard

Swift Custom Runtime: CodeBuild S3 caching not working

Open mufumade opened this issue 3 years ago • 0 comments

Hey 👋, So I would like to create a pipeline that compiles my swift lambda and deploys via sam. So far it works great. Codebuild builds the swift lambdas in my project and packages it via sam package.

Unfortunately the Codebuild S3 Cache does not work with swift build. Currently my build times are around the 10 min mark. So a cache would bring that down and save cost. Currently my setup looks like the following:

Slightly modified makefile as we do not need to execute docker here:

build-SomeTestFunction: builder-bot

builder-bot:
	$(eval $@PRODUCT = $(subst build-,,$(MAKECMDGOALS)))
	$(eval $@BUILD_DIR = $(PWD)/.aws-sam/build-$($@PRODUCT))
	$(eval $@STAGE = $($@BUILD_DIR)/lambda)
	$(eval $@ARTIFACTS_DIR = $(PWD)/.aws-sam/build/$($@PRODUCT))

	# prep directories
	mkdir -p $($@BUILD_DIR)/lambda $($@ARTIFACTS_DIR)

	# Compile application
	swift build --product $($@PRODUCT) -c release --build-path $($@BUILD_DIR)

	# copy deps
	ldd '/$($@BUILD_DIR)/release/$($@PRODUCT)' | grep swift | cut -d' ' -f3 | xargs cp -Lv -t /$($@BUILD_DIR)/lambda

	# copy binary to stage
	cp $($@BUILD_DIR)/release/$($@PRODUCT) $($@BUILD_DIR)/lambda/bootstrap

	# copy app from stage to artifacts dir
	cp $($@STAGE)/* $($@ARTIFACTS_DIR)

Buildspec.yml

version: 0.2
phases:
  pre_build:
    commands:
      - mkdir -p /build-directory
      - cp -a ${CODEBUILD_SRC_DIR}/. /build-directory
      - cd /build-directory
  build:
    commands:
      - cd /build-directory
      - sam build
      - sam package -t template.yml --s3-bucket {bucketName} --output-template-file packaged.yaml

  post_build:
    commands:
      - cd /build-directory
      - cp -a /build-directory/. ${CODEBUILD_SRC_DIR}

artifacts:
  files:
    - packaged.yaml
cache:
  paths:
    - ".aws-sam/**/*"

Because sam build does not use relative paths and each codebuild could be on different machines so the paths are different each time. After some digging i found this post over on stack overflow that I should copy the ${CODEBUILD_SRC_DIR} into a temp directory. And indeed that works. I do not get any errors anymore and the compile succeeds but unfortunately no caching.

Now to the question. Did you get codepipline including caching to work for swift custom runtime ? If so what did you do or what did I do wrong.

I would really appreciate your help. Thanks!

mufumade avatar Jun 13 '21 23:06 mufumade