Skip to content

Instantly share code, notes, and snippets.

@euank
Last active March 14, 2023 23:40
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save euank/2d5a7bc74dc56f502c30 to your computer and use it in GitHub Desktop.
Save euank/2d5a7bc74dc56f502c30 to your computer and use it in GitHub Desktop.
ECS / EC2 Metadata entrypoint example
FROM golang:1.4
COPY entrypoint.sh /entrypoint.sh
COPY main.go main.go
RUN go build -o main main.go
RUN chmod +x /entrypoint.sh
EXPOSE 8080
ENTRYPOINT ["/entrypoint.sh"]
CMD ["./main"]
#!/bin/sh
export HOST=$(curl --retry 5 --connect-timeout 3 -s 169.254.169.254/latest/meta-data/local-hostname)
export LOCAL_IP=$(curl --retry 5 --connect-timeout 3 -s 169.254.169.254/latest/meta-data/local-ipv4)
exec "$@"
package main
import (
"encoding/json"
"log"
"net/http"
"os"
)
func main() {
log.Fatal(http.ListenAndServe(":8080", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("Content-Type", "application/json")
body, err := json.Marshal(map[string]string{"hostname": os.Getenv("HOST"), "localIp": os.Getenv("LOCAL_IP")})
if err != nil {
panic(err)
}
resp.Write(body)
})))
}
{
"family": "test-hostenv-entrypoint",
"containerDefinitions": [{
"name": "test",
"image": "euank/play:2015-08-10",
"memory": 50,
"portMappings": [{
"containerPort": 8080,
"hostPort": 8080
}]
}]
}
@ccit-spence
Copy link

Thank for this, I will try later today. The issue I have had in my attempts is the session the export of the variables is placed. I am using a Spring/Java jar file. Needing to get the env variables into the properties files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment