On-demand CI/CD infrastructure with GitLab and AWS Fargate - How to reduce costs and scale GitLab Runner down to zero

In his new article, Daniel Miranda shows how we can use AWS Lambda functions to stop the Runner manager hosted on AWS Fargate when there are no CI/CD jobs to process and start it when a new pipeline is triggered. This configuration can significantly reduce the costs when we have considerable idle times between builds.

https://medium.com/ci-t/on-demand-ci-cd-infrastructure-with-gitlab-and-aws-fargate-376edc7afcda


Related discussions:

Why do something bespoke like this when you can just use webhooks and CodeBuild/Pipeline?

https://aws.amazon.com/quickstart/architecture/git-to-s3-using-webhooks/

Unfortunately this method has limitations that make it almost impossible to use from my POV. Those limitations being:

  1. A fixed base image. You will no longer be able to use a different base image per project

  2. Docker-in-Docker not available. Makes building images impossible

Look into kaniko for dind solution, that’s what we did so we can run our stuff on k8s without risk

We are already running our runners on k8s. I’m just saying that due to this limitation i don’t see many people being able to switch to Fargate

It’s always a judgement call but if your team likes GitLab CI there’s an argument for not setting up a duplicate service, especially if you support multiple clouds or on-premise deployments.

As always, the most important thing is to keep an idea of how much you’re spending on your current system and whether limitations are affecting your schedule. Build tools are overhead so you really just want to make sure that whatever you pick isn’t soaking up expensive developer time.

But you run dind on k8s? That’s fairly risky, what I meant is kaniko allows you to not need that. (Fixed image is a blocker though for this thing)

Yeah, but we only run dind a on dedicated cluster. But I’ll look into Kaniko, thanks for the heads-up

Docker-in-docker is a known limitation of Fargate. We got close to successfully use Kaniko with the Fargate driver, but didn’t succeed. Looks like it breaks the container. More details here: https://gitlab.com/gitlab-org/ci-cd/custom-executor-drivers/fargate/-/merge_requests/34.

And a follow-up in this issue: https://gitlab.com/gitlab-org/ci-cd/custom-executor-drivers/fargate/-/issues/16

You have downtimes when builds aren’t running? I think there’s a little time around 3-6am where we don’t have one build running.

Can you explain more your concerns on running dind on k8s? Just want to know. We are building our CI CD on k8s and are evaluating alternatives

Great to see others using the “start / stop” approach. I had to build a custom setup for our flagship apps that are not in .NET Core yet. It uses Github Actions, on check-in it will start a EC2 VM that is configured with the Github Actions Self Hosted Agent, our build tools and critically previously download Windows Server base images (11gb). Then after the build, Actions will stop the server to save money.

lovin’ kaniko on a gitlab runner - it’s the biz!

It allows you to fairly trivially break out of the runner and gain root on the under lying host. Privileged mode is basically root access, with a wee bit of research. Actually I need to double check with a colleague but we don’t bother setting up ssh access to a host for k8s, just execute a privileged container and control the host like that.

If you let other teams run jobs on your cluster and you have other software/stuff running there, then anyone can own your cluster through .gitlab-ci.yaml basically

Not everyone is 100% cloud native and AWS doesn’t have all the same code quality and security native tooling that 3rd party tools still provide.

New blog post on using AWS CodeBuild with the GitLab Runner as a workaround for the Docker-inDocker limitation on Fargate. Let us know if this is at all helpful.

https://about.gitlab.com/blog/2020/07/31/aws-fargate-codebuild-build-containers-gitlab-runner/