Add your Ruby gems to AWS CodeArtifact
Applications often use numerous packages to speed up development by providing reusable code for common tasks like network access, cryptography, or data manipulation… These packages may come from within your organization or from third parties like open source projects… Managing packages and dep…
Ruby developers can now use AWS CodeArtifact to securely store and retrieve their gems. CodeArtifact integrates with standard developer tools like gem
and bundler
.
Applications often use numerous packages to speed up development by providing reusable code for common tasks like network access, cryptography, or data manipulation. Developers also embed SDKs–such as the AWS SDKs–to access remote services. These packages may come from within your organization or from third parties like open source projects. Managing packages and dependencies is integral to software development. Languages like Java, C#, JavaScript, Swift, and Python have tools for downloading and resolving dependencies, and Ruby developers typically use gem
and bundler
.
However, using third-party packages presents legal and security challenges. Organizations must ensure package licenses are compatible with their projects and don’t violate intellectual property. They must also verify that the included code is safe and doesn’t introduce vulnerabilities, a tactic known as a supply chain attack. To address these challenges, organizations typically use private package servers. Developers can only use packages vetted by security and legal teams made available through private repositories.
CodeArtifact is a managed service that allows the safe distribution of packages to internal developer teams without managing the underlying infrastructure. CodeArtifact now supports Ruby gems in addition to npm, PyPI, Maven, NuGet, SwiftPM, and generic formats.
You can publish and download Ruby gem dependencies from your CodeArtifact repository in the AWS Cloud, working with existing tools such as gem
and bundler
. After storing packages in CodeArtifact, you can reference them in your Gemfile
. Your build system will then download approved packages from the CodeArtifact repository during the build process.
How to get started
Imagine I’m working on a package to be shared with other development teams in my organization.
In this demo, I show you how I prepare my environment, upload the package to the repository, and use this specific package build as a dependency for my project. I focus on the steps specific to Ruby packages. You can read the tutorial written by my colleague Steven to get started with CodeArtifact.
I use an AWS account that has a package repository (MyGemsRepo
) and domain (stormacq-test
) already configured.
To let the Ruby tools acess my CodeArtifact repository, I start by collecting an authentication token from CodeArtifact.
export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token
--domain stormacq-test
--domain-owner 012345678912
--query authorizationToken
--output text`
export GEM_HOST_API_KEY="Bearer $CODEARTIFACT_AUTH_TOKEN"
Note that the authentication token expires after 12 hours. I must repeat this command after 12 hours to obtain a fresh token.
Then, I request the repository endpoint. I pass the domain
name and domain owner
(the AWS account ID). Notice the --format ruby
option.
export RUBYGEMS_HOST=`aws codeartifact get-repository-endpoint
--domain stormacq-test
--domain-owner 012345678912
--format ruby
--repository MyGemsRepo
--query repositoryEndpoint
--output text`
Now that I have the repository endpoint and an authentication token, gem
will use these environment variable values to connect to my private package repository.
I create a very simple project, build it, and send it to the package repository.
$ gem build hola.gemspec
Successfully built RubyGem
Name: hola-codeartifact
Version: 0.0.0
File: hola-codeartifact-0.0.0.gem
$ gem push hola-codeartifact-0.0.0.gem
Pushing gem to https://stormacq-test-486652066693.d.codeartifact.us-west-2.amazonaws.com/ruby/MyGemsRepo...
I verify in the console that the package is available.
Now that the package is available, I can use it in my projects as usual. This involves configuring the local ~/.gemrc
file on my machine. I follow the instructions provided by the console, and I make sure I replace ${CODEARTIFACT_AUTH_TOKEN}
with its actual value.
Once ~/.gemrc
is correctly configured, I can install gems as usual. They will be downloaded from my private gem repository.
$ gem install hola-codeartifact
Fetching hola-codeartifact-0.0.0.gem
Successfully installed hola-codeartifact-0.0.0
Parsing documentation for hola-codeartifact-0.0.0
Installing ri documentation for hola-codeartifact-0.0.0
Done installing documentation for hola-codeartifact after 0 seconds
1 gem installed
Install from upstream
I can also associate my repository with an upstream source. It will automatically fetch gems from upstream when I request one.
To associate the repository with rubygems.org, I use the console, or I type
aws codeartifact associate-external-connection
--domain stormacq-test
--repository MyGemsRepo
--external-connection public:ruby-gems-org
{
"repository": {
"name": "MyGemsRepo",
"administratorAccount": "012345678912",
"domainName": "stormacq-test",
"domainOwner": "012345678912",
"arn": "arn:aws:codeartifact:us-west-2:012345678912:repository/stormacq-test/MyGemsRepo",
"upstreams": [],
"externalConnections": [
{
"externalConnectionName": "public:ruby-gems-org",
"packageFormat": "ruby",
"status": "AVAILABLE"
}
],
"createdTime": "2024-04-12T12:58:44.101000+02:00"
}
}
Once associated, I can pull any gems through CodeArtifact. It will automatically fetch packages from upstream when not locally available.
$ gem install rake
Fetching rake-13.2.1.gem
Successfully installed rake-13.2.1
Parsing documentation for rake-13.2.1
Installing ri documentation for rake-13.2.1
Done installing documentation for rake after 0 seconds
1 gem installed
I use the console to verify the rake
package is now available in my repo.
Things to know
There are some things to keep in mind before uploading your first Ruby packages.
- Be sure to update to the latest version of the AWS CLI before trying any command shown in the preceding instructions. Only the latest version of the CLI knows about Ruby repositories in CodeArtifact.
- The authentication token expires after 12 hours. We suggest writing a script to automate its renewal or using a scheduled AWS Lambda function and securely storing the token in AWS Secrets Manager (for example).
Pricing and availability
CodeArtifact costs for Ruby packages are the same as for the other package formats already supported. CodeArtifact billing depends on three metrics: the storage (measured in GB per month), the number of requests, and the data transfer out to the internet or to other AWS Regions. Data transfer to AWS services in the same Region is not charged, meaning you can run your continuous integration and delivery (CI/CD) jobs on Amazon Elastic Compute Cloud (Amazon EC2) or AWS CodeBuild, for example, without incurring a charge for the CodeArtifact data transfer. As usual, the pricing page has the details.
CodeArtifact for Ruby packages is available in all 13 Regions where CodeArtifact is available.
Now, go build your Ruby applications and upload your private packages to CodeArtifact!
— sebAuthor: Sébastien Stormacq