Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

sourcegraph/deploy-sourcegraph-aws

Repository files navigation

🚨 Deprecation Notice 🚨

This repository is no longer a supported Sourcegraph deployment method. If you'd like to deploy Sourcegraph, please see our Deployment Documentation to learn about our supported deployment methods.


Deploying Sourcegraph on EC2

This Terraform plan deploys Sourcegraph to an EC2 instance with SSL using a self-signed certificate.

This plan will create:

  • An EC2 instance
  • Security group with inbound ports 22, 80, and 443
  • IAM role and and IAM instance profile
  • Depending upon configuration, a key pair (see plan configuration)

See how it works by watching this screencast on Vimeo.com.

Sourcegraph on AWS using Terraform demo screencast

Prerequisites

  • Make (installed if you're macOS and Linux)
  • Terraform
  • mkcert (optional but required for self-signed cert validation)

NOTE: A basic level of knowledge and experience using AWS IAM and Terraform is required.

Terraform AWS authentication

You can authenticate with AWS using one of the methods described in the Terraform AWS authentication docs.

If you haven't done this before, using environment vars is recommended.

Terraform plan configuration

The existence of a terraform.tfvars file is required. To create it, copy the contents of terraform.tfvars.sample to a new terraform.tfvars file and review to see which variables you'd like to set.

You'll likely set key_name (if you already have a key pair), or public_key and key_name which if both specified, will create the key . If neither are these are set, you can still deploy Sourcegraph, but you'll be unable to SSH to the instance.

NOTE: If the region does not have a default VPC, vpc_id variable must be set in the terraform.tfvars file.

Commands

The Makefile has commands to cover the most common use-cases. The easiest way to create your Droplet is to run:

make deploy

This will create all resources and and poll the server to let you know when Sourcegraph is ready.

Other commands include:

  • make init: Download the required Terraform provider packages
  • make plan: Anything to add, remove or change?
  • make apply: Create the EC2 instance and other required resources
  • make sourcegraph: Wait for Sourcegraph to be ready to accept connections
  • make output: Display the same output as when make apply completes
  • make destroy: Removes all created from resources

WARNING: make destroy will destroy the Droplet so back-up the /etc/sourcegraph and /var/opt/sourcegraph directories first.

Upgrading Sourcegraph

To upgrade Sourcegraph:

  1. SSH into the EC2 instance
  2. Run ./sourcegraph-upgrade

The newer Docker image will be pulled and Sourcegraph will be restarted.

Troubleshooting

Default VPC error

If you get:

Error: Error refreshing state: 1 error(s) occurred:

* data.aws_vpc.default: 1 error(s) occurred:

* data.aws_vpc.default: data.aws_vpc.default: no matching VPC found

It means you don't have a default VPC in the currently specified region. Either choose a different region that has a default VPC or provide a vpc_id value in terraform.tfvars.


No public IP or hostname assigned

If the instance has no public IP address, get the subnet id the instance belongs to, then in the AWS console:

  1. VPC
  2. Subnets
  3. Select the subnet
  4. Actions: Modify auto-assign IP settings
  5. check "Auto-assign IPv4" and save
  6. Run make destroy to remove the current instance
  7. Run make deploy again to get a public hostname and IP