forked from awsdocs/aws-doc-sdk-examples
/
get_console_output.go
61 lines (48 loc) · 1.74 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
51
52
53
54
55
56
57
58
59
60
61
/*
Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
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))
}
}