How do I host Nexus on AWS EC2?
In this guide, we will demonstrate how to host Nexus on an AWS EC2 instance using Pulumi. We will create an EC2 instance, configure security groups, and install Nexus on the instance.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Define a VPC
const vpc = new aws.ec2.Vpc("vpc", {
cidrBlock: "10.0.0.0/16",
tags: {
Name: "nexus-vpc",
},
});
// Define an Internet Gateway
const igw = new aws.ec2.InternetGateway("igw", {
vpcId: vpc.id,
tags: {
Name: "nexus-igw",
},
});
// Define a Subnet
const subnet = new aws.ec2.Subnet("subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
tags: {
Name: "nexus-subnet",
},
});
// Define a Route Table
const routeTable = new aws.ec2.RouteTable("routeTable", {
vpcId: vpc.id,
routes: [
{
cidrBlock: "0.0.0.0/0",
gatewayId: igw.id,
},
],
tags: {
Name: "nexus-route-table",
},
});
// Associate the Route Table with the Subnet
new aws.ec2.RouteTableAssociation("routeTableAssociation", {
subnetId: subnet.id,
routeTableId: routeTable.id,
});
// Define a Security Group
const securityGroup = new aws.ec2.SecurityGroup("securityGroup", {
vpcId: vpc.id,
description: "Allow HTTP and SSH",
ingress: [
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, // SSH
{ protocol: "tcp", fromPort: 8081, toPort: 8081, cidrBlocks: ["0.0.0.0/0"] }, // Nexus
],
egress: [
{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
],
tags: {
Name: "nexus-security-group",
},
});
// Define a Key Pair
const keyPair = new aws.ec2.KeyPair("keyPair", {
publicKey: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA...",
});
// Define an EC2 Instance
const instance = new aws.ec2.Instance("instance", {
instanceType: "t2.micro",
ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
keyName: keyPair.keyName,
subnetId: subnet.id,
securityGroups: [securityGroup.name],
userData: `#!/bin/bash
sudo yum update -y
sudo yum install -y java-1.8.0-openjdk
cd /opt
sudo wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz
sudo tar -zxvf latest-unix.tar.gz
sudo mv nexus-3* nexus
sudo adduser nexus
sudo chown -R nexus:nexus /opt/nexus
sudo chown -R nexus:nexus /opt/sonatype-work
sudo ln -s /opt/nexus/bin/nexus /etc/init.d/nexus
sudo chkconfig --add nexus
sudo chkconfig --levels 345 nexus on
sudo service nexus start
`,
tags: {
Name: "nexus-instance",
},
});
// Export the public IP of the instance
export const publicIp = instance.publicIp;
export const publicDns = instance.publicDns;
Key Points
- We created a VPC, subnet, and associated route table to ensure our EC2 instance can communicate with the internet.
- A security group was defined to allow SSH and Nexus traffic.
- An EC2 instance was created, and Nexus was installed and started using user data.
Summary
In this guide, we demonstrated how to host Nexus on an AWS EC2 instance using Pulumi. We set up the necessary networking components, configured security groups, and installed Nexus on the EC2 instance.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.