Terraform your nexus repository and publish your monorepo with lerna

Ahmet Atasoy
3 min readJul 30, 2020

With one line of command, up and run your entire nexus repository and start publishing your packages using lerna.

With Terraform and Ansible, you will be able to create the instances and their required building blocks with a single command but also destroy with a single command. You will not have any side effects and it will always produce the same results. That is what idempotency is about.

Let’s get to the business. We need to install a couple of things on our machine.

Prerequisites:

  1. Install Terraform
  2. Install Ansible
  3. Install Docker

Create Terraform Variables

In order to access EC2 instance, we will need to create access keys. Create an IAM user, give programmatic access with `AmazonEC2FullAccess` policy attached. We will be using that with access_key and secret_key.

Save the file below as variables.tf.

variable "access_key" {}
variable "secret_key" {}
variable "region" {
default = "eu-west-2"
}

Create another file where you assign the keys — terraform.secrets.tfvars.

access_key = "XXX"
secret_key = "XXX"

Create Terraform file

The name of the file does not matter, we could call it for instance nexus.tf.

For Terraform to login, you need to add the following.

provider "aws" {
region = var.region
access_key = var.access_key
secret_key = var.secret_key
}

Let us now configure the security group in order for Ansible to access it via SSH and forward ports to access Nexus once the setup is finalized.

With the code below, we will be able to configure AWS and attach security group to it.

Once the instance is created, “local-exec” part will get into the control and execute the command. The command will create a config file for Ansible to access your AWS EC2 instance via SSH.

Create Ansible Playbook

Save the file as playbook.yml

Create instance with Terraform

Now you have the necessary files are created, execute the command below which will download the providers we required before such as “aws”, “aws_security_group” and “aws_instance”

terraform init 

Once it is finished, you should get “Terraform has been successfully initialized!”.

Now you can execute the following to create the instance.

terraform apply -var-file="terraform.secrets.tfvars"

The `var-file` is the secrets file we created earlier.

You will be asked to confirm what it is being applied, so take a look and type “yes”.

After the instance is created, you will be able to see what is created via “terraform show”. You can see the details, such as the IP address of the instance.

Make sure you destroy the instance if it is to test via:

terraform destroy -var-file="terraform.secrets.tfvars"

Now that you should have a URL you can access and setup your nexus repository.

Setup Nexus

Let us create our own npm repository and proxy ours to the public npm servers so if you are using packages other than yours, nexus will be proxying those to the npm repository.

Within Nexus, there are three repositories —

1. Private Registry. Where you keep your own packages.

2. Public Registry. Where you need to npm’s public server URL (https://registry.npmjs.org/)

3. Group. Where you add both of them and use that for your `.npmrc` but not to publish.

More on how to set it up, please follow their blog.

It is now time to configure your lerna.

Configure lerna

Finally, we will be able to use our private repository. Execute the command below:

npx lerna init

Change the contents of the “lerna.json” file with the following:

For publishing new packages, we need to update our “package.json” with the following.

"publishConfig": {
"registry": "http://<ec2>/repository/private/"
}

Login to your Nexus repository

The very last thing to do is to login to your private repository. There are many ways to handle this but you can easily execute the command below.

npm login registry=https://<ec2>/repository/group/

You can also add scope to your login and have a different repository for “@scoped” packages.

There you have it. Hope you enjoyed.

--

--