DEV Community

Cover image for ๐Ÿš€ Deploying Sonatype Nexus on AWS EC2 using Terraform
Himanshu Singh Tomar
Himanshu Singh Tomar

Posted on

๐Ÿš€ Deploying Sonatype Nexus on AWS EC2 using Terraform

This guide will walk you through the steps to deploy a Nexus Repository Manager on an AWS EC2 instance using a Terraform script.


๐Ÿ“ Prerequisites

Make sure you have the following installed:

  • Terraform
  • AWS CLI
  • An AWS account with credentials configured (via aws configure)
  • A valid SSH key pair created in your AWS account (needed to access EC2)

๐Ÿ“„ Step-by-Step Instructions

1. ๐Ÿ”‘ Update Your Key Pair Name

In the aws_instance block, replace:

key_name = "your-ssh-key"
Enter fullscreen mode Exit fullscreen mode

with the actual name of your AWS EC2 key pair.


2. ๐Ÿ“ฆ Save the Terraform Code

Save the entire provided Terraform code into a file named main.tf in a directory of your choice.


3. ๐Ÿ“‚ Initialize Terraform

Open a terminal, navigate to the directory containing [main.tf](https://github.com/Himanshusinghtomar/Sontype-Nexus/blob/main/sontype-nexus-aws-setup.tf), and run:

terraform init
Enter fullscreen mode Exit fullscreen mode

4. โœ… Validate the Configuration

Make sure everything is correct:

terraform validate
Enter fullscreen mode Exit fullscreen mode

5. ๐Ÿ” Preview the Deployment

Check what Terraform is going to create:

terraform plan
Enter fullscreen mode Exit fullscreen mode

6. ๐Ÿš€ Apply the Terraform Plan

Create the resources:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Type yes when prompted to proceed.


7. ๐ŸŒ Access Nexus

Once the infrastructure is deployed, Terraform will output the public IP of the EC2 instance:

nexus_public_ip = <YOUR_PUBLIC_IP>
Enter fullscreen mode Exit fullscreen mode

You can then open your browser and access Nexus:

http://<YOUR_PUBLIC_IP>:8081
Enter fullscreen mode Exit fullscreen mode

The default admin password will be available in the instance at:

sudo cat /opt/sonatype-work/nexus3/admin.password
Enter fullscreen mode Exit fullscreen mode

๐Ÿงน Optional: Destroy the Infrastructure

To delete the resources when you're done:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Happy DevOpsing! ๐ŸŽ‰

Top comments (0)