Hosting CentOS and Rocky Linux yum repositories in AWS S3

Hosting CentOS 7,8 and Rocky Linux yum repositories in AWS S3

Overview

We are utilising compute instances in different cloud environments as well as traditional data centres. On-premise virtual machines usually run RHEL 7/8, CentOS 7/8 and Rocky Linux 8.

Scope

This post explains how to create and host your own yum repositories in an S3 bucket and how to maintain secure, consistent and reliable server builds. This method also allows for a controlled package version and patch level lifecycle across environments.

The problem

Using externally hosted yum repositories or mirrors is very convenient and easy for end users installing and updating a single workstation, however it is not the best option in an enterprise environment where many new identical virtual machines could be built every day in an automated fashion.

Issues

The main problems with publicly hosted repositories are:

There are solutions available to address these issues with RHEL (Satellite), however for CentOS there are not many options.

The solution

CentOS and Rocky Linux come with tools to download or sync rpm packages to a local folder. Once this has been done, the createrepo command will build all the metadata needed to host your own yum repository via HTTP(s).

We could run a web server in front of this folder or sync the files to an S3 bucket. Each repo can be versioned (with timestamp) to promote changes across environments (dev > test > staging > production). As we are automatically rebuilding most of our servers, updating the repo URL in our source build repository is all that needs to be done. Alternatively just changing the yum repo bucket name will allow us to upgrade or downgrade packages to the desired patch level.

CentOS 7

For CentOS 7, we need to perform the following steps:

Create a new empty S3 bucket in your desired region. This bucket is used for publicly available files and may allow read access for everyone. There are options to further restrict access to the repository files but this goes beyond the scope of this blog entry.

# install necessary binaries
yum -y install yum-utils createrepo

# create repository location (ensure there is sufficient space)
mkdir /repository

# download all packages of repos we are interested in
reposync -g -l -p /repository -r base
reposync -g -l -p /repository -r extras
reposync -l -p /repository -r docker-ce-stable

# recreate repository metadata folder (this will create a repodata sub folder with repomd.xml file)
createrepo /repository

# sync whole folder to your S3 bucket
cd /repository
aws --region ap-southeast-2 s3 sync . s3://reece-yum-repository-bucket-20200420/ \
    --cache-control "max-age=3600,public" \
    --grants "read=uri=http://acs.amazonaws.com/groups/global/AllUsers"

Once the repo has been synced, simply create a repository file in /etc/yum.repos.d/myrepo.repo:

[reece]
baseurl = https://reece-yum-repository-bucket-20200420.s3-ap-southeast-2.amazonaws.com
enabled = 1
gpgcheck = 0
name = Reece
repo_gpgcheck = 0

and test the repo:

yum clean all
yum repolist
yum check-update

Some packages in this repo contain special characters (like the plus sign) in the URL. Unfortunately AWS S3 encodes these URLs, so these will not work without enabling website hosting in your bucket. The corresponding base URL will change accordingly.

In the above example, we are merging different source repositories (base, extras and docker-ce) into a single repository. This could be separated, however in our case this simplifies updates.

CentOS and Rocky Linux 8

For CentOS / Rocky Linux 8, this process is a bit more involved due to the addition of modular repositories.

As we don’t want to mix standard and modular repositories, instead of just one resulting repo we will keep the existing repository structure and names.

There are also many more packages which contain special characters, requiring the website hosting option for your S3 bucket to be enabled.

# ensure the tools are installed
yum -y install yum-utils createrepo

# sync latest rpms from origin
dnf reposync -g --delete -p /repository --repoid=baseos --download-metadata
dnf reposync -g --delete -p /repository --repoid=appstream --download-metadata
dnf reposync -g --delete -p /repository --repoid=extras --download-metadata
dnf reposync -g --delete -p /repository --repoid=powertools --download-metadata

# sync files to S3 (similar to CentOS 7)

The repository file (/etc/yum.repos.d/myrepo.repo) will include more than one repository in this case, some standard and some modular.

[baseos]
baseurl = http://yum-repo-rocky-20220229-1277.s3-website-ap-southeast-2.amazonaws.com/baseos
enabled = 1
gpgcheck = 0
name = reece-baseos
repo_gpgcheck = 0
[appstream]
baseurl = http://yum-repo-rocky-20220229-1277.s3-website-ap-southeast-2.amazonaws.com/appstream
enabled = 1
gpgcheck = 0
name = reece-appstream
repo_gpgcheck = 0
[extras]
baseurl = http://yum-repo-rocky-20220229-1277.s3-website-ap-southeast-2.amazonaws.com/extras
enabled = 1
gpgcheck = 0
name = reece-extras
repo_gpgcheck = 0
[powertools]
baseurl = http://yum-repo-rocky-20220229-1277.s3-website-ap-southeast-2.amazonaws.com/powertools
enabled = 1
gpgcheck = 0
name = reece-powertools
repo_gpgcheck = 0

Upgrades

Just replace the URL in /etc/yum.repos.d/myrepo.repo with the new path and run yum clean all.

Conclusion

Hosting your own yum repositories in S3 is a relatively easy process. The cost is minimal and the whole process can be automated in your favourite CI/CD pipeline.

More information here: