Skip to content

Instantly share code, notes, and snippets.

@amazingandyyy
Last active February 22, 2023 04:30
Show Gist options
  • Save amazingandyyy/60edaede96fef0bc5524c01cb5a7f92b to your computer and use it in GitHub Desktop.
Save amazingandyyy/60edaede96fef0bc5524c01cb5a7f92b to your computer and use it in GitHub Desktop.
Some useful commmands you will need to build your goloang binary and deploy to aws-ec2

Some useful commmands you will need to build your goloang binary and deploy to aws-ec2

build golang

$ GOOS=linux GOARCH=amd64 go build -o [binary-name]
  • type go env to check your environment variables
  • GOOS stands for Go OS, depends on what ec2 machine you rent, you might have different os setting
  • GOARCH stands for Go Archetecture
  • -o only allowed when compiling a single package, define the name of output file

securely copy binary to ssh machine

$ scp -i /path/to/[your].pem [binary-name] ubuntu@[public-DNS]:/home/ubuntu/[binary-name]
  • scp stands for secure copy
  • -i stands for identity
  • : for root folder or :/home/ubuntu/[binary-name] for more complex routes

setup services for the machin

ssh login

$ ssh -i /path/to/[your].pem ubuntu@[public-DNS]

Change mode of file to 700

$ sudo chmod 700 [name-of-binary-file]

create service

$ sudo nano /etc/systemd/system/[filename].service

as following

  [Unit]
  Description=Go Server

  [Service]
  ExecStart=/home/<username>/<path-to-exe>/<exe>
  WorkingDirectory=/home/<username>/<exe-working-dir>
  User=root
  Restart=always

  [Install]
  WantedBy=multi-user.target

control the service

  • Add the service to systemd.
    • sudo systemctl enable [filename].service
  • Activate the service.
    • sudo systemctl start [filename].service
  • Check if systemd started it.
    • sudo systemctl status [filename].service
  • Stop systemd if so desired.
    • sudo systemctl stop [filename].service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment