Dependencies and CodeArtifact
Salut! In this guide, we’ll build on our previous projects and explore AWS CodeArtifact.
New Terms to Know:
- CodeArtifact: Think of AWS CodeArtifact as a private storage locker for backing up all your web app’s dependencies.
- Domain: In AWS CodeArtifact, a domain is like a large folder that helps you organize your project materials into smaller folders.
- Public Upstream Repository: This is a publicly available repository where you can source packages for your project. AWS CodeArtifact can pull dependencies from this repository, allowing you to use open-source libraries and tools.
- Compiling: Compiling translates your project’s code into a language that computers can understand and execute.
Steps We’ll Cover:
- Set Up Your Web App and Git Repo (Projects 1 and 2 of this series)
- Create Your Domain and Repository
- Connect to the CodeArtifact Repository
- Test Your CodeArtifact Connection
- Set Up an IAM Policy for Using CodeArtifact
Step 1: Set Up Your Web App and Git Repository
We’ve already completed steps one and two. If you need help, feel free to refer to them:
- Step 1: Set Up a Web App in the Cloud
- Step 2: Connecting A github Repo with AWS
Step 2: Create Your Domain and Repository
- Search for CodeArtifact in your AWS Management Console.
- Select Domains from the menu on the left, then click Create Domain.
- Name your domain (e.g.,
my-domain) and select Create Domain. - Choose Create Repository to create a repository within this domain. Name it
my-packagesand use the description: "Packages for your web app." Selectmaven-central-storeas the public upstream repository. - Click Next.
- On the review page, take note of the Package Flow section, which visualizes the two repositories created during this process.
7. Create Repository to finish the process
Step 3: Connect to the CodeArtifact Repository
- On the next page, click View Connection Instructions. In the dialog box, select Mac & Linux as the operating system and mvn as the package manager.
- Note: Even if you’re using a Windows computer, remember that the EC2 instance running our Cloud9 IDE uses Amazon Linux 2 as its platform.
- Copy the command from Step 3. It should look similar to this:
export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain mydomain --domain-owner 390402566312 --region us-east-1 --query authorizationToken --output text`- If you encounter the error { Unable to locate credentials. You can configure credentials by running “aws configure”}, create your access key and grant it Admin Access on CodeArtifact.
4. Before running the connection settings, create a file named settings.xml to inform Maven where to find dependencies and how to connect to the right repositories.
Get Evan Selunani’s stories in your inbox
Join Medium for free to get updates from this writer.
5. Navigate to your web project and create the settings.xml file by running the following commands:
cd ~/your-web-project echo $'<settings>\n</settings>' > settings.xml6. Open the newly created settings.xml file in your VSCode file explorer on the left-hand panel.
7. Switch back to your CodeArtifact console and follow the remaining steps in the Connection Instructions dialog. Copy the code from Steps 4, 5, and 6, and add them between the <settings> tags in your settings.xml.
8. Your settings.xml file should look like this:
<settings>
<servers>
<server>
<id>mydomain-my-packages</id>
<username>aws</username>
<password>${env.CODEARTIFACT_AUTH_TOKEN}</password>
</server>
</servers>
<profiles>
<profile>
<id>mydomain-my-packages</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>mydomain-my-packages</id>
<url>https://mydomain-390402566312.d.codeartifact.us-east-1.amazonaws.com/maven/my-packages/</url>
</repository>
</repositories>
</profile>
</profiles>
<mirrors>
<mirror>
<id>mydomain-my-packages</id>
<name>mydomain-my-packages</name>
<url>https://mydomain-390402566312.d.codeartifact.us-east-1.amazonaws.com/maven/my-packages/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>Explanation of the Code Snippets:
— Servers Section: This is where you store your access details for the repositories your web app will connect with. Here, you’ve added your authentication token, which acts as an access pass to your local my-packages repository in CodeArtifact.
— Profiles Section: This defines when Maven should use a specific repository. Since we only have one package repository for this project, the profiles section is straightforward. It directs Maven to the my-packages repository to retrieve the necessary tools/packages for building your Java web app.
— Mirrors Section: This sets up backup locations for Maven to check if it can’t find the required resources in the primary local repository. In this case, we’ve designated my-packages as the backup location. The asterisk (*) denotes that all repository requests should redirect to the my-packages repository. While this might seem redundant now, mirrors are invaluable in complex scenarios and serve as a good fallback from the start.
Step 4: Test Your CodeArtifact Connection via VSCode
- Compile your application using the following command in your VSCode terminal:
mvn -s settings.xml compile2. Return to the CodeArtifact console and select your network-packages repository.
Step 5: Set up an IAM policy for using CodeArtifact
In this step, we will grant access to other DevOps services like AWS CodeBuild and AWS CodePipeline, which need permission to access the packages stored in CodeArtifact. By default, these services do not have access, but we will set up the necessary permissions for future projects in this DevOps series.
- Navigate to your IAM console.
- Click Policies in the left-hand navigation panel.
- Click Create policy.
- Select the JSON tab.
- Use the following policy code (source: Using Maven packages in CodeBuild):
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [ "codeartifact:GetAuthorizationToken",
"codeartifact:GetRepositoryEndpoint",
"codeartifact:ReadFromRepository"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "sts:GetServiceBearerToken",
"Resource": "*",
"Condition": {
"StringEquals": {
"sts:AWSServiceName": "codeartifact.amazonaws.com"
}
}
}
]
}6. Name the policy codeartifact-network-consumer-policy. For the description, add: Provides permissions to read from CodeArtifact.
7. Click Create policy.
Now, we have our working repositories that can be accessed by other services.
And that brings us to the end of our guide… See you on the next one!
A big shout out to Nextwork.org… their guides are awesome!
If you get stuck don’t hesistate to reach out!
Enjoy your weekend!










