- リージョンを指定しないとだめだった(awsのdefaultのcredentialにセットされていても)
- DescribeInstancesは
err
にあたる部分のエラーハンドリング用の引数を指定しないと怒られた(そういうものなんですか)./describe.go:32:33: multiple-value svc.DescribeInstances() in single-value context
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 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"fmt"</span> <span class="s">"github.com/aws/aws-sdk-go/aws"</span> <span class="s">"github.com/aws/aws-sdk-go/aws/awserr"</span> <span class="s">"github.com/aws/aws-sdk-go/aws/session"</span> <span class="s">"github.com/aws/aws-sdk-go/service/ec2"</span> <span class="p">)</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">svc</span> <span class="o">:=</span> <span class="n">ec2</span><span class="o">.</span><span class="n">New</span><span class="p">(</span> <span class="n">session</span><span class="o">.</span><span class="n">New</span><span class="p">(),</span> <span class="o">&</span><span class="n">aws</span><span class="o">.</span><span class="n">Config</span><span class="p">{</span> <span class="n">Region</span><span class="o">:</span> <span class="n">aws</span><span class="o">.</span><span class="n">String</span><span class="p">(</span><span class="s">"ap-northeast-1"</span><span class="p">),</span> <span class="p">})</span> <span class="n">input</span> <span class="o">:=</span> <span class="o">&</span><span class="n">ec2</span><span class="o">.</span><span class="n">DescribeInstancesInput</span><span class="p">{</span> <span class="n">InstanceIds</span><span class="o">:</span> <span class="p">[]</span><span class="o">*</span><span class="kt">string</span><span class="p">{</span> <span class="n">aws</span><span class="o">.</span><span class="n">String</span><span class="p">(</span><span class="s">"i-xxxxxxxxxxxxxxxx"</span><span class="p">),</span> <span class="p">},</span> <span class="p">}</span> <span class="n">result</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">svc</span><span class="o">.</span><span class="n">DescribeInstances</span><span class="p">(</span><span class="n">input</span><span class="p">)</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="k">if</span> <span class="n">aerr</span><span class="p">,</span> <span class="n">ok</span> <span class="o">:=</span> <span class="n">err</span><span class="o">.</span><span class="p">(</span><span class="n">awserr</span><span class="o">.</span><span class="n">Error</span><span class="p">);</span> <span class="n">ok</span> <span class="p">{</span> <span class="k">switch</span> <span class="n">aerr</span><span class="o">.</span><span class="n">Code</span><span class="p">()</span> <span class="p">{</span> <span class="k">default</span><span class="o">:</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">aerr</span><span class="o">.</span><span class="n">Error</span><span class="p">())</span> <span class="p">}</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="c">// Print the error, cast err to awserr.Error to get the Code and</span> <span class="c">// Message from an error.</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">err</span><span class="o">.</span><span class="n">Error</span><span class="p">())</span> <span class="p">}</span> <span class="k">return</span> <span class="p">}</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="p">}</span> |