CodeArtifact authorization tokens and upstream repositories
A package manager reaches CodeArtifact with an authorization token from the GetAuthorizationToken API. The CodeArtifact User Guide says the caller needs both codeartifact:GetAuthorizationToken and sts:GetServiceBearerToken, and the token lasts 12 hours by default. Upstream repositories let one endpoint serve packages from several repositories, searched in the order listed.
How does a build get a token?
aws codeartifact login fetches a token and configures the package manager with the token and the repository endpoint in one step. It supports dotnet, npm, nuget, pip, swift and twine. For other package managers, call aws codeartifact get-authorization-token and put the token in a configuration file or an environment variable.
The IAM user or role needs both codeartifact:GetAuthorizationToken and sts:GetServiceBearerToken. Adding sts:GetServiceBearerToken to a CodeArtifact domain resource policy has no effect, so it must be allowed in the identity's own policy. Root users cannot call GetAuthorizationToken. When the domain belongs to another account, pass --domain-owner.
How long does a token last?
A token is valid for 12 hours by default. --duration-seconds accepts 900 to 43200, which is 15 minutes to 12 hours. The lifetime starts when login or get-authorization-token is called, and a new token must be fetched when it ends.
The token lifetime is independent of the role session that fetched it. A token fetched in a 15-minute assumed-role session is still valid for the full 12 hours. The exception is --duration-seconds 0, which is valid only when assuming a role and makes the token expire with the remaining session time.
How are upstream repositories searched?
A repository can list other CodeArtifact repositories as upstreams, so a client using one endpoint can get packages held in several repositories. Associating one needs the AssociateWithDownstreamRepository permission on the upstream repository. A repository can have up to 10 upstream repositories and only one external connection.
A request looks in the repository itself first, then in its upstreams in the order they were listed in create-repository or update-repository. The search stops at the first repository that has the requested version. If an upstream has its own upstreams, those are searched before the next upstream at the same level. describe-repository shows the order.
If an upstream has an external connection to a public repository such as npm, repositories downstream of it can pull public packages through it. list-package-versions looks only in the repository named and does not list versions held in its upstreams.
Where the exam catches people
Believing codeartifact:GetAuthorizationToken alone is enough: the caller also needs sts:GetServiceBearerToken.
Believing sts:GetServiceBearerToken can be granted in the domain policy: it has no effect there and must be in the IAM identity's policy.
Believing a token dies when the role session ends: it keeps its own lifetime, 12 hours by default, unless it was created with --duration-seconds 0.
Believing the order of upstreams does not matter: the first upstream in the list that has the version wins and the search stops.
Believing a repository can have several external connections: it can have one, and up to 10 upstream repositories.