AWS CodeArtifact is a fully managed artifact repository service that helps teams securely store, manage, and share software packages. It supports multiple package formats, including PyPI (Python), npm (JavaScript), Maven (Java), and NuGet (.NET). By using CodeArtifact, organizations can centralize their package management, improve security, and streamline development workflows.
In this tutorial, we will demonstrate how to set up and use AWS CodeArtifact. While CodeArtifact supports various programming languages, we will use Python as an example.
Prerequisites
Before we begin, make sure you have:
- AWS CLI installed and configured (installation covered in the last tutorial).
- AWS CodeArtifact permissions in your AWS account.
- Python and pip installed on your system.
- Twine installed for uploading Python packages: `pip install twine`
Step 1: Create a CodeArtifact Domain and Repository
A domain in CodeArtifact acts as a logical grouping for multiple repositories. A repository within the domain stores and manages software packages.
To create a domain named my-domain, run: `aws codeartifact create-domain –domain my-domain`

Then, create a repository named my-repo under this domain: `aws codeartifact create-repository –repository my-repo –domain my-domain`

Step 2: Create a Demo Python Package
Create a New Python Project
Create a new directory for the package: `mkdir my_demo_package && cd my_demo_package`

Inside this directory, create the necessary package structure: `mkdir mypackage`

Create a Python file for the package functionality: `echo ‘def hello(): return “Hello from CodeArtifact!”‘ > mypackage/__init__.py`
Create Setup Files
Generate a setup.py file for packaging:
echo ‘from setuptools import setup, find_packages
setup(
name=”my_demo_package”,
version=”0.1.0″,
packages=find_packages(),
install_requires=[],
)’ > setup.py

Before uploading, generate the package distribution files: `python setup.py sdist bdist_wheel`

Verify that the dist/ folder contains both .tar.gz and .whl files:

Step 3: Upload the Package to AWS CodeArtifact
Authenticate to AWS CodeArtifact
Retrieve an authentication token and configure pip to use CodeArtifact:
`aws codeartifact login –tool pip –repository my-repo –domain my-domain –domain-owner OWNER_ID –region us-east-1`

Set the repository index URL for pip: `pip config set global.index-url $(aws codeartifact get-repository-endpoint –domain my-domain –repository my-repo –format pypi –query repositoryEndpoint –output text)`

Upload the Package
Retrieve an authentication token and configure twine to upload the package in CodeArtifact:
`CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token –domain my-domain –query authorizationToken –output text)`

Upload the package using Twine:
`twine upload –repository-url $(aws codeartifact get-repository-endpoint –domain my-domain –repository my-repo –format pypi –query repositoryEndpoint –output text) –username “aws” –password “$CODEARTIFACT_AUTH_TOKEN” dist/*`

If you receive an Unauthorized (401) error, ensure your authentication token is valid and your IAM permissions allow publishing to CodeArtifact.
Verify that the package is correctly uploaded: `aws codeartifact list-package-versions –domain my-domain –repository my-repo –format pypi –package my_demo_package`

Step 4: Install and Use the Package from CodeArtifact
Install the Package
To install the package from CodeArtifact on another system (or after clearing the local cache), run: `pip install my-demo-package==0.1.0`

Verify Installation
Check if the package is installed correctly: `pip show my_demo_package`

If installed, you can now import and use it in your scripts: `python -c “import my_demo_package; print(‘Package loaded successfully’)”`
AWS CodeArtifact
Duplicated packages and misconfigured retention policies can drive up storage costs. Elite Cloud helps you manage your CodeArtifact repositories efficiently to avoid bloat and cost creep.
Let’s streamline your package management and reduce storage spend.
Conclusion
CodeArtifact provides a secure and scalable way to manage software dependencies. By following this tutorial, you’ve learned how to:
- Create a CodeArtifact repository
- Package and upload a Python module
- Troubleshoot common upload and installation issues
- Retrieve and install packages from CodeArtifact
This setup ensures secure and efficient package management for development teams. Whether you are managing internal dependencies or distributing software across teams, AWS CodeArtifact simplifies the process, making package management seamless and reliable.
FAQs
What is AWS CodeArtifact used for?
CodeArtifact is a managed service for storing and sharing software packages securely. It supports popular formats like PyPI, npm, Maven, and NuGet.
How do I upload a Python package to AWS CodeArtifact?
First, create a Python package with setup.py
, then use tools like Twine and pip after authenticating to upload your files to a CodeArtifact repository.
What are domains and repositories in AWS CodeArtifact?
A domain groups related repositories, while a repository is where your packages are stored and managed. Think of it as a hierarchy for organizing packages.
How do I install packages from AWS CodeArtifact?
After configuring pip with your CodeArtifact URL, run pip install
with the package name. The authentication token must be valid and active.
Is CodeArtifact secure for internal package distribution?
Yes, CodeArtifact uses IAM-based access control, token-based authentication, and integrates with AWS services to ensure your packages remain secure.