Skip to content

Instantly share code, notes, and snippets.

@begetan
Created July 9, 2020 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save begetan/85185d42f6439ed3f39d14c70915d3b8 to your computer and use it in GitHub Desktop.
Save begetan/85185d42f6439ed3f39d14c70915d3b8 to your computer and use it in GitHub Desktop.
AWS Lambda Describe Instances Go example
func (app *App) describeInstances(id string, name string) ([]Node, error) {
svc := ec2.New(app.cfg)
input := &ec2.DescribeInstancesInput{
InstanceIds: []string{id},
Filters: []ec2.Filter{
{
Name: aws.String("tag:Name"),
Values: []string{name},
},
},
}
req := svc.DescribeInstancesRequest(input)
output, err := req.Send(app.ctx)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
err = aerr
}
return nil, err
}
}
var nodes []Node
for _, reservation := range output.Reservations {
for _, in := range reservation.Instances {
var n Node
n.ID = *in.InstanceId
n.Type = string(in.InstanceType)
n.Name = name
n.State = in.State.Name
if in.NetworkInterfaces != nil {
n.IP = *in.NetworkInterfaces[0].PrivateIpAddress
}
nodes = append(nodes, n)
}
}
return nodes, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment