Configure and use Cargo with CodeArtifact
You can use Cargo to publish and download crates from CodeArtifact repositories or to fetch crates from
crates.io
Configure Cargo with CodeArtifact
To use Cargo to install and publish crates from AWS CodeArtifact, you'll first need to configure them with your CodeArtifact repository information. Follow the steps in one of the following procedure to configure Cargo with your CodeArtifact repository endpoint information and credentials.
Configure Cargo using the console instructions
You can use configuration instructions in the console to connect Cargo to your CodeArtifact repository. The console instructions provide a Cargo configuration customized for your CodeArtifact repository. You can use this custom configuration to set up Cargo without needing to find and fill in your CodeArtifact information.
-
Open the AWS CodeArtifact console at https://console.aws.amazon.com/codesuite/codeartifact/home
. -
In the navigation pane, choose Repositories, and then choose a repository to connect to Cargo.
-
Choose View connection instructions.
-
Choose your operating system.
-
Choose Cargo.
-
Follow the generated instructions to connect Cargo to your CodeArtifact repository.
Configure Cargo manually
If you cannot or do not want to use the configuration instructions from the console, you can use the following instructions to connect Cargo to your CodeArtifact repository manually.
The [registries.
section in the preceding example
defines a registry with my_registry
]
and provides my_registry
index
and credential-provider
information.
-
index
specifies the URL of the index for your registry, which is the CodeArtifact repository endpoint that ends with a/
. Thesparse+
prefix is required for registries that are not Git repositories.Note
To use a dualstack endpoint, use the
codeartifact.
endpoint.region
.on.aws -
credential-provider
specifies the credential provider for the given registry. Ifcredential-provider
isn't set, the providers inregistry.global-credential-providers
will be used. By settingcredential-provider
tocargo:token-from-stdout
, the Cargo client will fetch new authorization token automatically when publishing or downloading from your CodeArtifact repository, therefore you don't need to manually refresh the authorization token every 12 hours.
The [registry]
section defines the default registry used.
-
default
specifies the name of the registry defined in[registries.
, to use by default when publishing or downloading from your CodeArtifact repository.my_registry
]
The [source.crates-io]
section defines the default registry used when one isn't specified.
-
replace-with = "
replaces the public registry, crates.io with your CodeArtifact repository defined inmy_registry
"[registries.
. This configuration is recommended if you need to request packages from the external connection such as crates.io.my_registry
]To get all of the benefits of CodeArtifact, such as the package origin control that prevents dependency confusion attacks, it is recommended that you use source replacement. With the source replacement, CodeArtifact proxies all requests to the external connection and copies the package from the external connection to your repository. Without the source replacement, the Cargo client will directly retrieve the package based on the configuration in your
Cargo.toml
file in your project. If a dependency is not marked withregistry=
, the Cargo client will retrieve it directly from crates.io without communicating with your CodeArtifact repository.my_registry
Note
If you start using source replacement and then update your configuration file to not use source replacement, you may encounter errors. The opposite scenario may also lead to errors. Therefore, it is recommended that you avoid changing the configuration for your project.
Installing Cargo crates
Use the following procedures to install Cargo crates from a CodeArtifact repository or from crates.io
Install Cargo crates from CodeArtifact
You can use the Cargo (cargo
) CLI to quickly install a specific version of a Cargo crate from your CodeArtifact repository.
To install Cargo crates from a CodeArtifact repository with cargo
If you haven't, follow the steps in Configure and use Cargo with CodeArtifact to configure the
cargo
CLI to use your CodeArtifact repository with proper credentials.-
Use the following command to install Cargo crates from CodeArtifact:
cargo add my_cargo_package@
1.0.0
For more information, see cargo add
in The Cargo Book.
Publishing Cargo crates to CodeArtifact
Use the following procedure to publish Cargo crates to a CodeArtifact repository using the cargo
CLI.
If you haven't, follow the steps in Configure and use Cargo with CodeArtifact to configure the
cargo
CLI to use your CodeArtifact repository with proper credentials.-
Use the following command to publish Cargo crates to a CodeArtifact repository:
cargo publish
For more information, see cargo publish
in The Cargo Book.