Use AWS CodeArtifact with Angular app builds
Tags: aws, angular, devops
Published: 2023-09-27 18:20:39

**Introduction**
This is a short post describing how you can use AWS CodeArtifact with your application builds. I am using generic packages but there are other options. I will provide example for npm build but the approach can be used for other types of builds.

> Note: I previously posted detailed walkthrough on how to host Angular up in S3 ( [check out the link] (https://dev.to/aws-builders/angular-app-in-aws-s3-and-cloudfront-2jp0)). This post will describe how we can now store the builds.

**Understanding the Need for Artifact Repositories**
Why Store Builds? Why can't we just deploy our applications immediately after building them? It's essential to understand the rationale behind storing builds in the first place.

1. Consistent Deployments: The "build once and deploy everywhere" mantra ensures that the same build is deployed across various environments, be it staging, QA, or production. This consistency eliminates discrepancies between environments and the notorious "it works on my machine" dilemma.

2. Versioning and Rollbacks: Storing builds provides a clear version history. If a new deployment encounters issues, teams can swiftly revert to a previous, stable build, ensuring minimal service disruption.

3. Streamlined Testing: With builds stored and versioned, testing becomes more efficient. Testers can pull specific builds for different testing stages, ensuring that the application undergoes thorough scrutiny before hitting production.

4. Audit and Compliance: A repository provides a clear audit trail of application iterations. This is invaluable for sectors with stringent regulatory requirements, as it offers transparency and traceability.

**Setting Up an AWS CodeArtifact Repository**

```
data "aws_kms_key" "codeartifact_key" {
  key_id = "alias/aws/codeartifact"
}

resource "aws_codeartifact_domain" "example" {
  domain         = "nbekenov"
  encryption_key = data.aws_kms_key.codeartifact_key.arn
}

resource "aws_codeartifact_repository" "example" {
  repository = "my-example-repo"
  domain     = aws_codeartifact_domain.example.domain
}
```

**Build**

```
npm run build:prod
```
This creates dist/ folder

**Publish to CodeArtifact**

```
export DOMAIN_NAME="nbekenov"
export REPOSITORY_NAME="my-example-repo"
export PACKAGE_NAME="example-package"
export PACKAGE_VERSION="1.0.0"

tar -cvzf asset.tar.gz dist/

export ASSET_SHA256=$(sha256sum asset.tar.gz | awk '{print $1;}')

aws codeartifact publish-package-version --domain $DOMAIN_NAME \
    --repository $REPOSITORY_NAME --format generic \
    --namespace $PACKAGE_NAME \
    --package $PACKAGE_NAME --package-version $PACKAGE_VERSION \
    --asset-content asset.tar.gz \
    --asset-name asset.tar.gz \
    --asset-sha256 $ASSET_SHA256
```

**Get package from CodeArtifact**

```
#!/bin/bash
export DOMAIN_NAME="nbekenov"
export REPOSITORY_NAME="my-example-repo"
export PACKAGE_NAME="example-package"

PACKAGE_VERSION=$1

aws codeartifact get-package-version-asset --domain $DOMAIN_NAME \
    --repository $REPOSITORY_NAME  --format generic \
    --namespace $PACKAGE_NAME \
    --package $PACKAGE_NAME \
    --package-version $PACKAGE_VERSION --asset asset.tar.gz asset.tar.gz

tar -xzvf asset.tar.gz

```

And that's it! Now you have packages based on the build number (package version) you provided and can deploy it.