How to do it…

  1. Create http-server.go, where we will create a simple HTTP server that will render Hello World! browsing http://ec2-instance-public-dns:80 or executing curl -X GET http://ec2-instance-public-dns:80 from the command line, as follows:
package main
import
(
"fmt"
"log"
"net/http"
)
const
(
CONN_PORT = "80"
)
func helloWorld(w http.ResponseWriter, r *http.Request)
{
fmt.Fprintf(w, "Hello World!")
}
func main()
{
http.HandleFunc("/", helloWorld)
err := http.ListenAndServe(":"+CONN_PORT, nil)
if err != nil
{
log.Fatal("error starting http server : ", err)
return
}
}

With everything in place, the directory structure should look like the following:

  1. Copy http-server.go from the local machine directory to an EC2 user home (/home/ec2-user) directory using the secure copy or scp command, as follows:
$ scp -i my-first-ec2-instance.pem http-server.go ec2-user@ec2-172-31-34-99.compute-1.amazonaws.com:/home/ec2-user
  1. Login into an EC2 instance using a private key file and a public DNS name, as follows:
$ ssh -i my-first-ec2-instance.pem ec2-user@ec2-172-31-34-99.compute-1.amazonaws.com
  1. Run http-server.go in the background, executing the no hang-up or nohup command, as follows:
[ec2-user@ip-172-31-34-99 ~] $ nohup go run http-server.go &
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156