You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "The ultimate step-by-step guide to cost-effective, build-time-efficient, and easy managable EC2 deployments using GitHub Actions, Terraform, Docker, and a self-hosted registry."
@@ -11,90 +11,42 @@ image: "/ogs/ga-tf-aws.jpg"
11
11

12
12
13
13
14
-
This guide shows how to deploy own Docker apps (with AdminForth as example) to Amazon EC2 instance with Docker and Terraform involving Docker self-hosted registry.
15
-
16
-
Needed resources:
17
-
- GitHub actions Free plan which includes 2000 minutes per month (1000 of 2-minute builds per month - more then enough for many projects, if you are not running tests). Extra builds would cost `0.008$` per minute.
18
-
- AWS account where we will auto-spawn EC2 instance. We will use `t3a.small` instance (2 vCPUs, 2GB RAM) which costs `~14$` per month in `us-east-1` region (cheapest region). Also it will take `$2` per month for EBS gp2 storage (20GB) for EC2 instance
19
-
20
-
Registry will be auto-spawned on EC2 instance, so no extra costs for it. GitHub storage is not used as well, so no costs for it as well.
21
-
22
-
The setup shape:
23
-
- Build is done using IaaC approach with HashiCorp Terraform, so almoast no manual actions are needed from you. Every resource including EC2 server instance is described in code which is commited to repo.
24
-
- Docker build process is done on GitHub actions server, so EC2 server is not overloaded with builds
25
-
- Changes in infrastructure including changing server type, adding S3 Bucket, changing size of sever disk is also can be done by commiting code to repo.
26
-
- Docker images and cache are stored on EC2 server, so no extra costs for Docker registry are needed.
27
-
- Total build time for average commit to AdminForth app (with Vite rebuilds) is around 2 minutes.
14
+
This guide is a hackers extended addition of [Deploying AdminForth to EC2 with Amazon ECR](/blog/compose-aws-ec2-ecr-terraform-github-actions/). The key difference in this post that we will not use Amazon ECR but self-host registry on EC2 itself. Automatically from terraform. And will see whether we will win something in terms of build time.
28
15
29
16
<!-- truncate -->
30
17
31
18
32
-
# Building on CI versus building on EC2?
33
-
34
-
Previously we had a blog post about [deploying AdminForth to EC2 with Terraform without registry](/blog/compose-ec2-deployment-github-actions/). That method might work well but has a significant disadvantage - build process happens on EC2 itself and uses EC2 RAM and CPU. This can be a problem if your EC2 instance is well-loaded without extra free resources. Moreover, low-end EC2 instances have a small amount of RAM and CPU, so build process which involves vite/tsc/etc can be slow or even fail.
35
-
36
-
So obviously to solve this problem we need to move the build process to CI, however it introduces new chellenges and we will solve them in this post.
19
+
## Costs for Amazon ECR vs consts for self-hosted registry on EC2
37
20
38
-
Quick difference between approaches from previous post and current post:
21
+
Most of AWS services are formed from EC2 prices plus some extra overhead for own cost. In same way, Amazon ECR pricing is pretty same.
39
22
40
-
| Feature |Without Registry|With Registry|
23
+
| Feature |Amazon ECR|Self-hosted registry on EC2|
41
24
| --- | --- | --- |
42
-
| How and where docker build happens | Source code is rsync-ed from CI to EC2 and docker build is done there | Docker build is done on CI and docker image is pushed to registry (in this post we run registry automatically on EC2) |
43
-
| How Docker build layers are cached | Cache is stored on EC2 | GitHub actions has no own Docker cache out of the box, so it should be stored in dedicated place (we use self-hosted registry on the EC2 as it is free) |
44
-
| Advantages | Simpler setup with less code (we don't need code to run and secure registry, and don't need extra cache setup as is naturally persisted on EC2). | Build is done on CI, so EC2 server is not overloaded. For most cases CI builds are faster than on EC2. Plus time is saved because we don't need to rsync source code to EC2 |
45
-
| Disadvantages | Build on EC2 requires additional server RAM / overloads CPU | More terraform code is needed. Registry cache might require small extra space on EC2. Complexities to make it run from both local machine and CI |
<sub>\* All tests done from local machine (Intel(R) Core(TM) Ultra 9 185H, Docker Desktop/WSL2 64 GB RAM, 300Mbps up/down) up to working state</sub>
50
-
51
-
52
-
## Chellenges when you build on CI
25
+
| Storage | $0.10 per GB/month | $0.10 per GB/month for gp2 EBS volume |
26
+
| Data transfer for egress | $0.09 per GB | $0.09 per GB |
53
27
54
-
A little bit of theory.
28
+
So as you can see there is still no difference in terms of cost. However the approach in this system allows to replace Amazon EC2 with any other cloud provider which does not charge for egress traffic.
55
29
56
-
When you move build process to CI you have to solve next chellenges:
57
-
1) We need to deliver built docker images to EC2 somehow (and only we)
58
-
2) We need to persist cache between builds
59
30
60
-
### Delivering images
31
+
#Bechnmarking build time
61
32
62
-
#### Exporing images to tar files
33
+
When I implmented this solution, I was curious whether it will be faster to build images on EC2 or on CI. So I did a little bit of testing.
34
+
First I used results form [deploying AdminForth to EC2 with Terraform without registry](/blog/compose-ec2-deployment-github-actions/) where we built images on EC2. Then I did the same test but with self-hosted registry on EC2 and compared to [deploying AdminForth to EC2 with Amazon ECR](/blog/compose-aws-ec2-ecr-terraform-github-actions/) where we built images on CI and pushed to Amazon ECR.
63
35
64
-
Simplest option which you can find is save docker images to tar files and deliver them to EC2. We can easily do it in terraform (using `docker save -o ...` command on CI and `docker load ...` command on EC2). However this option has a significant disadvantage - it is slow. Docker images are big (always include all layers, without any options), so it takes infinity to do save/load and another infinity to transfer them to EC2 (via relatively slow rsync/SSH and relatively slow GitHub actions outbound connection).
65
-
66
-
#### Docker registry
67
-
68
-
Faster, right option which we will use here - involve Docker registry. Registry is a repository which stores docker images. It does it in a smart way - it saves each image as several layers, so if you will update last layer, then only last layer will be pushed to registry and then only last will be pulled to EC2.
69
-
To give you row compare - whole-layers image might take `1GB`, but last layer created by `npm run build` command might take `50MB`. And most builds you will do only last layer changes, so it will be 20 times faster to push/pull last layer than whole image.
70
-
And this is not all, registry uses TLS HTTP protocol so it is faster then SSH/rsync encrypted connection.
71
-
72
-
Of course you have to care about a way of registry authentication (so only you and your CI/EC2 can push/pull images).
73
-
74
-
What docker registry can you use? Pretty known options:
75
-
1) Docker Hub - most famous. It is free for public images, so literally every opensource project uses it. However it is not free for private images, and you have to pay for it. In this post we are considering you might do development for commercial project with tight budget, so we will not use it.
76
-
2) GHCR - Registry from GitHub. Has free plan but allows to store only 500MB and allows to transfer 1GB of traffic per month. Then you pay for every extra GB in storage (`$0.0008` per GB/day or `$0.24` per GB/month) and for every extra GB in traffic ($0.09 per GB). Probably small images will fit in free plan, but generally even alpine-based docker images are bigger than 500MB, so it is non-free option.
77
-
3) Amazon ECR - Same as GHCR but from Amazon. Price is `$0.10` per GB of storage per month and `$0.09` per GB of data transfer. So it is cheaper than GHCR but still not free. But is good option.
78
-
4) Self-hosted registry web system. In our software development company, we use Harbor. It is a powerful free open-source registry that can be installed to own server. It allows pushing and pulling without limit. Also, it has internal life-cycle rules that cleanup unnecessary images and layers. The main drawbacks of it are that it is not so fast to install and configure, plus you have to get a domain and another powerfull server to run it. So unless you are a software development company, it is not worth using it.
79
-
5) Self-hosted minimal CNCF Distribution [registry](https://distribution.github.io/distribution/) on EC2 itself. So since we already have EC2, we can run registry on it directly. The `registry` container is pretty light-weight and easy to setup and it will not consume a lot of extra CPU/RAM on server. Plus images will be stored close to application so pull will be fast.
80
-
81
-
In the post we will use last (5th way). Our terraform will deploy registry automatically, so you don't have to do anything special.
82
-
83
-
### Persisting cache
36
+
| Feature | Without Registry (build directly on EC2) | With self-hosted registry | With Amazon ECR |
Docker builds without layer cache persistence are possible but very slow. Most builds only change a couple of layers, and having no ability to cache them will cause the Docker builder to regenerate all layers from scratch. This can, for example, increase the Docker build time from a minute to ten minutes or even more.
41
+
<sub>\* All tests done from local machine (Intel(R) Core(TM) Ultra 9 185H, Docker Desktop/WSL2 64 GB RAM, 300Mbps up/down) up to working state</sub>
86
42
87
-
Out of the box, GitHub Actions can't save Docker layers between builds, so you have to use external storage.
43
+
So it indeed own self-hosted registry is faster then ECR and overall build time of pure AdminForth is faster then building on EC2. When ECR is slower then self-hosted registry, it is because of network speed.
88
44
89
-
> Though some CI systems can persist docker build cache, e.g. open-source self-hosted Woodpecker CI allows it out of the box. However GitHub actions which is pretty popular, reasonably can't allow such free storage to anyone
90
45
91
-
So when build-in Docker cache can't be used, there is one alternative - Docker BuildKit external cache.
92
-
So BuildKit allows you to connect external storage. There are several options, but most sweet for us is using Docker registry as cache storage (not only as images storage to deliver them to application server).
46
+
# Chellenges when you build on CI
93
47
94
-
> *BuildKit cache in Compose issue*
95
-
> Previously we used docker compose to build & run our app, it can be used to both build, push and pull images, but has [issues with external cache connection](https://github.com/docker/compose/issues/11072#issuecomment-1848974315). While they are not solved we have to use `docker buildx bake` command to build images. It is not so bad, but is another point of configuration which we will cover in this post.
96
48
97
-
###Registry authorization and traffic encryption
49
+
# Registry authorization and traffic encryption
98
50
99
51
Hosting custom CNCF registry, from other hand is a security responsibility.
100
52
@@ -104,7 +56,7 @@ First of all we need to set some authorization to our registry so everyone who w
104
56
105
57
But this is not enough. Basic auth is not encrypted, so someone can perform MITM attack and get your credentials. So we need to encrypt traffic between CI and registry. We can do it by using TLS certificates. So we will generate self-signed TLS certificates, and attach them to our registry.
106
58
107
-
59
+
Though the challenge is that we need to provide CA certificate to every daemon which will work with our registry. So we need to provide CA certificate to buildx daemon on CI, also if we want to do it from local machine, we need to provide CA certificate to local docker daemon.
@@ -997,7 +867,7 @@ Check your public IP in Terraform output and add
997
867
> Bad news is that instance public IP will be known only after first run, so some steps would fail because there will be no hosts mapping. However since EC2 provisioning takes some time it is even possible to copy IP from terminal and inser it to hosts file from first run 🤪
998
868
999
869
1000
-
### 3. Using local build from multiple projects
870
+
#### 3. Using local build from multiple projects
1001
871
1002
872
The easiest way would be probably to rename `appserver.local` to unique name for each project.
0 commit comments