Skip to content

Commit 8d513bc

Browse files
digocorbelliniRodrigo Okamoto
andauthored
updated readme (#136)
* updated example code in readme * updated help printout in readme * added simple output to choices for output format * added a drop down for v2.3.1 example code * fixed v2.3.1 example code * copied v2.3.1 example code from old readme Co-authored-by: Rodrigo Okamoto <rodocp@amazon.com>
1 parent fd78895 commit 8d513bc

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

README.md

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ $ ec2-instance-selector --help
153153
```
154154

155155
```bash#help
156-
ec2-instance-selector is a CLI tool to filter EC2 instance types based on resource criteria.
156+
ec2-instance-selector is a CLI tool to filter EC2 instance types based on resource criteria.
157157
Filtering allows you to select all the instance types that match your application requirements.
158158
Full docs can be found at github.com/aws/amazon-ec2-instance-selector
159159
@@ -245,7 +245,7 @@ Global Flags:
245245
--cache-ttl int Cache TTLs in hours for pricing and instance type caches. Setting the cache to 0 will turn off caching and cleanup any on-disk caches. (default 168)
246246
-h, --help Help
247247
--max-results int The maximum number of instance types that match your criteria to return (default 20)
248-
-o, --output string Specify the output format (table, table-wide, one-line)
248+
-o, --output string Specify the output format (table, table-wide, one-line, simple) (default "simple")
249249
--profile string AWS CLI profile to use for credentials and config
250250
-r, --region string AWS Region to use for API requests (NOTE: if not passed in, uses AWS SDK default precedence)
251251
-v, --verbose Verbose - will print out full instance specs
@@ -257,8 +257,11 @@ Global Flags:
257257

258258
This is a minimal example of using the instance selector go package directly:
259259

260-
**cmd/examples/example1.go**
261-
```go#cmd/examples/example1.go
260+
**NOTE:** The example below is intended for `v3+`. For versions `v2.3.1` and earlier, refer to the following dropdown:
261+
<details>
262+
<summary>Example for v2.3.1</summary>
263+
264+
```go
262265
package main
263266

264267
import (
@@ -315,6 +318,81 @@ func main() {
315318
}
316319
// Print the returned instance types slice
317320
fmt.Println(instanceTypesSlice)
321+
}
322+
```
323+
</details>
324+
325+
**cmd/examples/example1.go**
326+
```go#cmd/examples/example1.go
327+
package main
328+
329+
import (
330+
"fmt"
331+
332+
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/bytequantity"
333+
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/selector"
334+
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/selector/outputs"
335+
"github.com/aws/aws-sdk-go/aws"
336+
"github.com/aws/aws-sdk-go/aws/session"
337+
)
338+
339+
func main() {
340+
// Load an AWS session by looking at shared credentials or environment variables
341+
// https://docs.aws.amazon.com/sdk-for-go/api/aws/session/
342+
sess, err := session.NewSession(&aws.Config{
343+
Region: aws.String("us-east-2"),
344+
})
345+
if err != nil {
346+
fmt.Printf("Oh no, AWS session credentials cannot be found: %v", err)
347+
return
348+
}
349+
350+
// Instantiate a new instance of a selector with the AWS session
351+
instanceSelector := selector.New(sess)
352+
353+
// Instantiate an int range filter to specify min and max vcpus
354+
vcpusRange := selector.IntRangeFilter{
355+
LowerBound: 2,
356+
UpperBound: 4,
357+
}
358+
// Instantiate a byte quantity range filter to specify min and max memory in GiB
359+
memoryRange := selector.ByteQuantityRangeFilter{
360+
LowerBound: bytequantity.FromGiB(2),
361+
UpperBound: bytequantity.FromGiB(4),
362+
}
363+
// Create a string for the CPU Architecture so that it can be passed as a pointer
364+
// when creating the Filter struct
365+
cpuArch := "x86_64"
366+
367+
// Create a Filter struct with criteria you would like to filter
368+
// The full struct definition can be found here for all of the supported filters:
369+
// https://github.com/aws/amazon-ec2-instance-selector/blob/main/pkg/selector/types.go
370+
filters := selector.Filters{
371+
VCpusRange: &vcpusRange,
372+
MemoryRange: &memoryRange,
373+
CPUArchitecture: &cpuArch,
374+
}
375+
376+
// Pass the Filter struct to the FilteredInstanceTypes function of your
377+
// selector instance to get a list of filtered instance types and their details
378+
instanceTypesSlice, err := instanceSelector.FilterInstanceTypes(filters)
379+
if err != nil {
380+
fmt.Printf("Oh no, there was an error getting instance types: %v", err)
381+
return
382+
}
383+
384+
// Pass in your list of instance type details to the appropriate output function
385+
// in order to format the instance types as printable strings.
386+
maxResults := 100
387+
instanceTypesSlice, _, err = outputs.TruncateResults(&maxResults, instanceTypesSlice)
388+
if err != nil {
389+
fmt.Printf("Oh no, there was an error truncating instnace types: %v", err)
390+
return
391+
}
392+
instanceTypes := outputs.SimpleInstanceTypeOutput(instanceTypesSlice)
393+
394+
// Print the returned instance types slice
395+
fmt.Println(instanceTypes)
318396
}
319397
```
320398

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Full docs can be found at github.com/aws/amazon-` + binName
144144
tableOutput,
145145
tableWideOutput,
146146
oneLineOutput,
147+
simpleOutput,
147148
}
148149

149150
// Registers flags with specific input types from the cli pkg

0 commit comments

Comments
 (0)