forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_console_output.go
50 lines (40 loc) · 1.15 KB
/
get_console_output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"encoding/base64"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
func decodeOutputb64(encoded string) string {
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
fmt.Println("Error", err)
return ""
}
return string(decoded)
}
func main() {
// Create new EC2 service
ec2Svc := ec2.New(session.Must(session.NewSession(&aws.Config{
Region: aws.String(endpoints.UsWest2RegionID),
})))
if len(os.Args) == 1 {
fmt.Printf("Usage %s <instance-id>\n", os.Args[0])
os.Exit(1)
}
instanceId := os.Args[1]
// Call EC2 GetConsoleOutput API on the given instance according
// https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#EC2.GetConsoleOutput
input := ec2.GetConsoleOutputInput{InstanceId: aws.String(instanceId)}
json, err := ec2Svc.GetConsoleOutput(&input)
if err != nil {
fmt.Println("Error", err)
} else {
fmt.Println(decodeOutputb64(*json.Output))
}
}