Last active
March 14, 2023 23:40
-
-
Save euank/2d5a7bc74dc56f502c30 to your computer and use it in GitHub Desktop.
ECS / EC2 Metadata entrypoint example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"family": "test-hostenv-entrypoint", | |
"containerDefinitions": [{ | |
"name": "test", | |
"image": "euank/play:2015-08-10", | |
"memory": 50, | |
"portMappings": [{ | |
"containerPort": 8080, | |
"hostPort": 8080 | |
}] | |
}] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.