DEV Community

Kalsoom ahmed
Kalsoom ahmed

Posted on

Secure Packages with CodeArtifact

KEY CONCEPTS

  • AWS CodeArtifact
  • Amazon EC2
  • GitHub

Get ready to:

🗂️ Set up CodeArtifact as a repository for your project's dependencies.
🛡️ Use IAM roles and policies to give your web app access to CodeArtifact.
✅ Verify your web app's connection to CodeArtifact!

💎 Become a package uploader - create and add your own packages to your CodeArtifact repository!

Image description

Launch your EC2 Instance

Let's kick things off by launching an EC2 instance! This instance will be our virtual server in the cloud where we'll develop our Java web app.

This is important because Amazon EC2 provides the compute resources we need to host our application and build our CI/CD pipeline.

In this step, you're going to:

  • Launch a new EC2 instance

nstall Maven and Java on EC2 on terminal

copy past following commands into terminal
wget https://archive.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

sudo tar -xzf apache-maven-3.5.2-bin.tar.gz -C /opt

echo "export PATH=/opt/apache-maven-3.5.2/bin:$PATH" >> ~/.bashrc

source ~/.bashrc

Now we're going to install Java 8, or more specifically, Amazon Correto 8:

sudo dnf install -y java-1.8.0-amazon-corretto-devel

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto.x86_64

export PATH=/usr/lib/jvm/java-1.8.0-amazon-corretto.x86_64/jre/bin/:$PATH

  • To make it easier to work with our web app files on the EC2 instance, we'll use the VS Code Remote - SSH extension.

Image description

  • Use the extension to SSH connect to your EC2 instance.

Image description

Set Up Your Web App

In your EC2 instance terminal, run the following Maven command to generate a basic Java web app structure. Copy and paste the entire command and press Enter:
mvn archetype:generate \
-DgroupId=com.nextwork.app \
-DartifactId=nextwork-web-project \
-DarchetypeArtifactId=maven-archetype-webapp \
-DinteractiveMode=false

Image description

  • After running the command, you should see a BUILD SUCCESS message in your terminal output. This tells us that Maven has successfully generated the web app!

Image description

Connect Your Web App to GitHub

Let's connect our local web app to a remote repository on GitHub. This will let us track changes to our code and collaborate with others!
In this step, you'll:

  • Install Git on your EC2 instance.
  • Set up a GitHub repository.
  • Connect your local web app to the GitHub repository.

  • install Git

To start using Git, we need to install it on your EC2 instance
sudo dnf update -y

sudo dnf install git -y

Image description

  • To check that Git was installed correctly, run the following command in the terminal: git --version

Set Up AWS CodeArtifact Repository

Now, let's set up AWS CodeArtifact, a fully managed artifact repository service. We'll use it to store and manage our project's dependencies, ensuring secure and reliable access to Java packages.

This is important because CodeArtifact provides a centralized, secure, and scalable way to manage dependencies for our Java projects, improving build consistency and security.
In this step, you're going to:

  • Create a CodeArtifact repository.

Create an IAM Policy for CodeArtifact Access

For Maven to start working with CodeArtifact, we need to create an IAM role that grants our EC2 instance the permission it needs to access CodeArtifact.

Otherwise, Maven can try all it wants to command your EC2 instance to store and retrieve packages from CodeArtifact, but your EC2 instance simple wouldn't be able to do anything! And going another layer deeper, IAM roles are made of policies; so we need to create policies first before setting up the role
In this step, you're going to:

  • Try connecting Maven with CodeArtifact (error!)
  • Create a new IAM policy.
  • Set up the policy to grant an EC2 instance access to CodeArtifact.

Attach IAM Policy and Verify CodeArtifact Connection

Now that we've created the IAM policy for CodeArtifact access, let's attach it to an IAM role and then associate that role with our EC2 instance. This will grant our EC2 instance the permissions it needs to securely access CodeArtifact. Finally, we'll verify the connection to CodeArtifact from our EC2 instance.

This is important because attaching the IAM role to our EC2 instance is what actually grants the instance the permissions defined in the policy, enabling secure access to CodeArtifact.

In this step, you're going to:

  • Create a new IAM role for EC2 that has your new policy attached.
  • Attach the IAM role to your EC2 instance.
  • Re-run the export token command, this time seeing a successful response

Login in to github account:

Image description

  • Set up a new repository called nextwork-web-project.

-Back in your terminal, set up a new local Git repository:
git init

Image description

Add remote origin

Now let's connect your local project folder with your Github repo!

Head back to your terminal in VS Code.
Add the remote repository as the origin with the following command, replacing with your repository's URL.
git remote add origin

  • To verify that the remote origin has been set up correctly, run the command git remote -v

Add, commit, and push your code to GitHub

Now, let's add all the files in your project to the Git repository. To do this, there are three commands you need to run...
git add .
git commit -m "Updated index.jsp with new content"
git push -u origin master

Image description

  • You should now see all your web app files listed in your GitHub repository. SO good!

Image description

See Packages in CodeArtifact!

Let's make sure everything is set up correctly by verifying the connection to our CodeArtifact repository from our EC2 instance. We'll configure Maven to use CodeArtifact and then try to compile our web app, which should now download dependencies from CodeArtifact.

In this step, you're going to:

  • Finish setting up the connection between Maven and CodeArtifact.
  • Compile your Maven project using the settings.xml file.
  • See your CodeArtifact repository automatically store your project's dependencies!

Delete your resources

Now that we've successfully verified our CodeArtifact connection, it's time to clean up the AWS resources we created to avoid incurring any unnecessary costs.

Resources to delete:

  • EC2 instance
  • IAM role and policy
  • CodeArtifact repository and domain
  • Key pair file

Top comments (0)