How to Automatically Generate Tokens for CodeArtifact (Maven) in Windows
Tags: aws, codeartifact, windows, maven
Published: 2022-05-18 10:43:13


If you have ever used or plan to use CodeArtifact, you will need to generate a token (**CODEARTIFACT_AUTH_TOKEN**) manually with all Maven projects daily. This can be challenging and slow down your development.

After a while, I came up with PowerShell Script that can simplify your work, creating a **User Variable**:

```
$AWS_ACCOUNT_ID='ID'
$DOMAIN='CODE_ARTIFACT_DOMAIN'
$env:CODEARTIFACT_AUTH_TOKEN = aws codeartifact get-authorization-token --domain $DOMAIN --domain-owner $AWS_USER_ACCOUNT --query authorizationToken --output text
[Environment]::SetEnvironmentVariable("CODEARTIFACT_AUTH_TOKEN",$env:CODEARTIFACT_AUTH_TOKEN,"User")
```

You only need to modify these two variables:

- **$AWS_ACCOUNT_ID**, you get it from here:

![account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0h5ki8u6cbctmj49q156.png)

- **$DOMAIN**, you get it from here:

![domain](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oasb9cn1wymko5ihauly.png) 

After this, you can save the script as a **.ps1** file and run it from **PowerShell** using this command:

```
powershell -ExecutionPolicy Bypass -File "MY_PATH\UpdateTokenNoAdmin.ps1"
```

**MY_PATH** represents the location where you saved your file.

The last part is automating its recreation daily, for this, we are going to use the Task Scheduler:

![scheduler](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41xrlywonvfm4kjhyze1.png) 

The first step, create a new task:

![task](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1kqhr426ef6g8c9h79vu.png)

Then, you give it a name:

![name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x02d64g0n4we4ynh06gn.png) 

Next, you configure a trigger (_I prefer **at log on**, but it can be daily_):

![logon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nglali7m7y85no8os4r.png)

Finally, you add a new Action based on the previous command:

```
powershell -ExecutionPolicy Bypass -File "MY_PATH\UpdateTokenNoAdmin.ps1"
```

![action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fwm7wnreruo54ece537.png)

And that's all, now, your env variable (**CODEARTIFACT_AUTH_TOKEN**) is going to be autogenerated!


### Follow me on:

|  Personal  |  LinkedIn  |YouTube|Instagram|Cyber Prophets|Sharing Your Stories|
|:----------|:----------|:------------:|:------------:|:------------:|:------------:|
|[![Personal](https://raw.githubusercontent.com/FANMixco/federiconavarrete/master/img/favicons/favicon.png)](https://federiconavarrete.com)|[![LinkedIn](https://i.stack.imgur.com/idQWu.png)](https://www.linkedin.com/in/federiconavarrete)|[![YouTube](https://i.stack.imgur.com/CFPMR.png)](https://youtube.com/c/FedericoNavarrete)|[![Instagram](https://i.stack.imgur.com/PIfqY.png)](https://www.instagram.com/federico_the_consultant)|[![RedCircle Podcast](https://i.stack.imgur.com/4XICF.png)](https://redcircle.com/shows/cyber-prophets)|[![RedCircle Podcast](https://i.stack.imgur.com/4XICF.png)](https://redcircle.com/shows/sharing-your-stories)|

[![sponsor me](https://raw.githubusercontent.com/FANMixco/Xamarin-SearchBar/master/bmc-rezr5vpd.gif)](https://www.buymeacoffee.com/fanmixco)

_Banner Credits:_

_[AWS](https://aws.amazon.com/blogs/devops/publishing-private-npm-packages-aws-codeartifact/)_