Last active
November 1, 2023 03:08
-
-
Save apacheservices68/3f72f7098d354ae0522c478c9e28ee5e to your computer and use it in GitHub Desktop.
ec2 api get info script
This file contains hidden or 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" | |
| "fmt" | |
| "io" | |
| "log" | |
| "net/http" | |
| "time" | |
| ) | |
| type Ec2Info struct { | |
| AccountId string `json:"accountId"` | |
| Architecture string `json:"architecture"` | |
| AvailabilityZone string `json:"availabilityZone"` | |
| ImageId string `json:"imageId"` | |
| InstanceId string `json:"instanceId"` | |
| InstanceType string `json:"instanceType"` | |
| PendingTime string `json:"pendingTime"` | |
| PrivateIp string `json:"privateIp"` | |
| Region string `json:"region"` | |
| } | |
| func (e Ec2Info) String() string { | |
| return fmt.Sprintf("\n[EC2] Instance Id = %v; Private IP = %d, Region = %v", e.InstanceId, e.PrivateIp, e.Region) | |
| } | |
| func main() { | |
| logs := make(chan string) | |
| go logLogs(logs) | |
| handleHello := makeHello(logs) | |
| // METHOD 2 | |
| passer := &DataPasser{logs: make(chan string)} | |
| go passer.log() | |
| http.HandleFunc("/1", handleHello) | |
| http.HandleFunc("/2", passer.handleHello) | |
| http.ListenAndServe(":8080", nil) | |
| } | |
| func Getter(url string, body chan []byte) { | |
| resp, err := http.Get(url) | |
| if err != nil { | |
| log.Printf("%+v\n", err) | |
| } | |
| b, err := io.ReadAll(resp.Body) | |
| if err != nil { | |
| log.Printf("%+v\n", err) | |
| } | |
| // close response body | |
| resp.Body.Close() | |
| // print response body | |
| log.Printf("%+v\n", string(b)) | |
| body <- b | |
| } | |
| // METHOD 1 | |
| func makeHello(logger chan string) func(http.ResponseWriter, *http.Request) { | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| body := make(chan []byte, 1) | |
| url := "http://169.254.169.254/latest/dynamic/instance-identity/document" | |
| //url := "http://localhost:8888" | |
| logger <- r.RemoteAddr | |
| go Getter(url, body) | |
| time.Sleep(time.Millisecond * 10) | |
| tmp := <-body | |
| ins := Ec2Info{} | |
| if err := json.Unmarshal(tmp, &ins); err != nil { | |
| log.Println("Parse json got an error ", err.Error()) | |
| } | |
| type_ := "application/json" | |
| w.Header().Set("Content-Type", type_) | |
| fmt.Fprintf(w, "%v", ins) | |
| //io.WriteString(w, "Hello world!") | |
| } | |
| } | |
| func logLogs(logger chan string) { | |
| for item := range logger { | |
| fmt.Println("Client IP : ", item) | |
| } | |
| } | |
| // METHOD 2 | |
| type DataPasser struct { | |
| logs chan string | |
| } | |
| func (p *DataPasser) handleHello(w http.ResponseWriter, r *http.Request) { | |
| p.logs <- r.URL.String() | |
| io.WriteString(w, "Hello world") | |
| } | |
| func (p *DataPasser) log() { | |
| for item := range p.logs { | |
| fmt.Println("2. Item", item) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GOOS=linux GOARCH=amd64 go build -o web aws6.go // listen on default port
push script to ec2 instance and start.
endpoint http://your-public-ip:8080/1