Aakash
Aakash

Reputation: 3171

CodeDeploy AfterInstall hook does not exit after starting Go server

I have a simple Go server. Code below. server.go

package main

import (
    "fmt"
    "log"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

I have an EC2 server with CodeDeploy agent and everything in place. All I am trying to do is, once I push and merge new code with master branch of my Github repo, CodeDeploy should deploy the revisions to my EC2 server In-Place and below is my appspec.yml (which is at the root of my repo)

appspec.yml

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu/go/myproject
hooks:
  AfterInstall:
  - location: after-install.sh
    runas: ubuntu

after-install.sh script below. Pretty simple, after the revision is deployed to myproject folder in EC2, cd into the folder and start the go server.

#!/bin/bash

cd $HOME/go/myproject
/usr/local/go/bin/go run server.go &

Problem: With the above, revisions are perfectly deployed and the server is started and I can hit the public dns on port 8080 from my browser and see the changes. However, CodeDeploy keeps running and never shows that deployment is completed.

Is there anything I should do for CodeDeploy to gracefully complete after starting the Go server?

Upvotes: 1

Views: 739

Answers (1)

Chris Williams
Chris Williams

Reputation: 35146

According to the documentation you should not do this.

You cannot simply add an ampersand (&) in after-install.sh to run sleep.sh in the background.

If the below was your hook file

#!/bin/bash
# Do not do this.
/tmp/sleep.sh &

You would need to add > /dev/null 2> /dev/null < /dev/null before the &, like below

#!/bin/bash
/tmp/sleep.sh > /dev/null 2> /dev/null < /dev/null &

For your file it should be update to look like the below

#!/bin/bash

cd $HOME/go/myproject
/usr/local/go/bin/go run server.go > /dev/null 2> /dev/null < /dev/null &

Upvotes: 2

Related Questions