Publishing package to NuGet repository on AWS CodeArtifact

Vaibhav Srivastava
4 min readJan 22, 2023

CodeArtifact requires users to authenticate with the service in order to publish or consume package versions. You must authenticate to the CodeArtifact service by creating an authorization token using your AWS credentials.

Authenticate to a CodeArtifact repository

First, install the AWS CLI and configure AWS credentials for an IAM user or role that has the appropriate permission to access CodeArtifact. You can then use the CLI to call the CodeArtifact GetAuthorizationToken API. This API vends auth tokens, that can be included in the HTTP Authorization header in requests made by package managers and build tools. The CLI provides the login command that calls GetAuthorizationToken and automatically configures a package manager to use this token for all requests. This is similar to the get-login command provided by Amazon ECR, so developers who have interacted with ECR using the docker CLI will be familiar with this pattern.

Create a class library project

  1. In Visual Studio, choose File > New > Project, select the Visual C# node, select the “Class Library (.NET Framework)” template.

2. Name the project SampleLib4CodeArtifact, and click Create

Configure project

From the Solution Explorer,> Right Click on resulting project and select Properties

Refer to this document for setting up AWS Profile
https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/setup-credentials.html

Copying the NuGet source endpoint

After you set up your profile, you can see your provisioned repositories.

  1. In the AWS Explorer pane, navigate to the repository you want to connect
  2. Choose your repository (right-click).

Copy NuGet Source Endpoint.

Setting up Package source

Now that you have your source endpoint, you can set up the NuGet package source.

  1. In Visual Studio, under Tools, choose Options.
  2. Choose NuGet Package Manager.
  3. Under Options, choose the + icon to add a package source.
  4. For Name, enter AWS Nuget Repo.
  5. For Source, enter the source endpoint you copied from the previous step.

Configure project

From the Solution Explorer,> Right Click on the resulting project and select Properties

Update the Package configuration based on your project requirements and save it

Build and Publish the package

Let's build the project library by running this command “dotnet build

Once the build is successful, let's package the library and make it ready to publish by running this command “dotnet pack

To push a package version to a CodeArtifact repository, run the following command with the full path to your .nupkg file and the source name for your CodeArtifact repository in your NuGet configuration file.

dotnet nuget push <package-location> --source "https://xxxxxxxxxx.d.codeartifact.us-east-1.amazonaws.com/nuget/nuget-repository/v3/index.json"

This will publish the package

A review published package in CodeArtifact

Packages are stored in a repository you pull from, or referenced via the upstream connection. Because we’re pulling packages from nuget.org through an external connection, you can see cached copies of those packages in your repository. To view the packages, navigate to your repository on the CodeArtifact console.

Consuming package

Once you have configured NuGet with CodeArtifact, you can consume NuGet packages stored in your CodeArtifact repository or upstream repositories.

To consume a package version from a CodeArtifact repository or one of its upstream repositories with nuget or dotnet, run the following command replacing packageName with the name of the package you want to consume and packageSourceName with the source name for your CodeArtifact repository in your NuGet configuration file.

--

--

Vaibhav Srivastava

Solutions Architect | AWS & Azure Certified | Hybrid & Multi-Cloud Exp. | Technophile