ARTIFACTORY: How to create Artifactory repositories using Terraform provider

Tatarao Vana
2023-01-22 11:07

The Artifactory provider is used to interact with the resources supported by Artifactory. The provider needs to be configured with the proper credentials before it can be used. In this article, we will discuss the creation of Artifactory Pypi repositories using the Artifactory provider for Terraform.

Creation of Pypi repositories using Artifactory Terraform provider

We can use the following steps to create any type of repository using the Artifactory Terraform providers.

1. Create a main.tf with the following content to use the Artifactory provider to create repositories. Generate an admin token and replace it in the access_token section.
$ cat main.tfterraform {
required_providers {
artifactory = {
source = "jfrog/artifactory"
version = "6.20.2"
}
}
}
# Configure the Artifactory provider
provider "artifactory" {
url = "https://jpd.local/artifactory"
access_token = "eyJXXXXXXXQ-LzVnceCxikxACO1w"
}
# Create a new repository
resource "artifactory_local_pypi_repository" "pypi-local" {
key = "pypi-local"
description = "Repo created by Terraform Provider Artifactory"
}

2. Execute the terraform init command. 
$ terraform initInitializing the backend...
Initializing provider plugins...
- Finding jfrog/artifactory versions matching "6.20.2"...
- Installing jfrog/artifactory v6.20.2...
- Installed jfrog/artifactory v6.20.2 (unauthenticated)

3. Review the execution plan.
$ terraform plan
 Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# artifactory_local_pypi_repository.test-pypi-local will be created
+ resource "artifactory_local_pypi_repository" "pypi-local" {
+ blacked_out = false
+ description = "Repo created by Terraform Provider Artifactory"
+ excludes_pattern = (known after apply)
+ id = (known after apply)
+ includes_pattern = (known after apply)
+ key = "test-pypi-local"
+ package_type = (known after apply)
+ priority_resolution = false
+ project_environments = (known after apply)
+ repo_layout_ref = "simple-default"
+ xray_index = false
}

Plan: 1 to add, 0 to change, 0 to destroy.

────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
4. Execute terraform apply command to create the repository in Artifactory.
$ terraform apply
 Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# artifactory_local_pypi_repository.test-pypi-local will be created
+ resource "artifactory_local_pypi_repository" "pypi-local" {
+ blacked_out = false
+ description = "Repo created by Terraform Provider Artifactory"
+ excludes_pattern = (known after apply)
+ id = (known after apply)
+ includes_pattern = (known after apply)
+ key = "test-pypi-local"
+ package_type = (known after apply)
+ priority_resolution = false
+ project_environments = (known after apply)
+ repo_layout_ref = "simple-default"
+ xray_index = false
}

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

artifactory_local_pypi_repository.test-pypi-local: Creating...
artifactory_local_pypi_repository.test-pypi-local: Creation complete after 3s [id=test-pypi-local]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
5. After executing the above commands, we should be able to see the PyPI local repository in the Artifactory user interface.