github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/cmd/pick-instance-type/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/gruntwork-io/go-commons/entrypoint" 7 "github.com/gruntwork-io/terratest/modules/aws" 8 "github.com/urfave/cli" 9 ) 10 11 const CustomUsageText = `Usage: pick-instance-type [OPTIONS] <REGION> <INSTANCE_TYPE> <INSTANCE_TYPE...> 12 13 This tool takes in an AWS region and a list of EC2 instance types and returns the first instance type in the list that is available in all Availability Zones (AZs) in the given region, or exits with an error if no instance type is available in all AZs. This is useful because certain instance types, such as t2.micro, are not available in some of the newer AZs, while t3.micro is not available in some of the older AZs. If you have code that needs to run on a "small" instance across all AZs in many different regions, you can use this CLI tool to automatically figure out which instance type you should use. 14 15 Arguments: 16 17 REGION The AWS region in which to look up instance availability. E.g.: us-east-1. 18 INSTANCE_TYPE One more more EC2 instance types. E.g.: t2.micro. 19 20 21 Options: 22 23 --help Show this help text and exit. 24 25 Example: 26 27 pick-instance-type ap-northeast-2 t2.micro t3.micro 28 ` 29 30 func run(cliContext *cli.Context) error { 31 region := cliContext.Args().First() 32 if region == "" { 33 return fmt.Errorf("You must specify an AWS region as the first argument") 34 } 35 36 instanceTypes := cliContext.Args().Tail() 37 if len(instanceTypes) == 0 { 38 return fmt.Errorf("You must specify at least one instance type") 39 } 40 41 // Create mock testing.T implementation so we can re-use Terratest methods 42 t := MockTestingT{MockName: "pick-instance-type"} 43 44 recommendedInstanceType, err := aws.GetRecommendedInstanceTypeE(t, region, instanceTypes) 45 if err != nil { 46 return err 47 } 48 49 // Print the recommended instance type to stdout 50 fmt.Print(recommendedInstanceType) 51 52 return nil 53 } 54 55 func main() { 56 app := entrypoint.NewApp() 57 cli.AppHelpTemplate = CustomUsageText 58 entrypoint.HelpTextLineWidth = 120 59 60 app.Name = "pick-instance-type" 61 app.Author = "Gruntwork <www.gruntwork.io>" 62 app.Description = `This tool takes in a list of EC2 instance types (e.g., "t2.micro", "t3.micro") and returns the first instance type in the list that is available in all Availability Zones (AZs) in the given AWS region, or exits with an error if no instance type is available in all AZs.` 63 app.Action = run 64 65 entrypoint.RunApp(app) 66 } 67 68 // MockTestingT is a mock implementation of testing.TestingT. All the functions are essentially no-ops. This allows us 69 // to use Terratest methods outside of a testing context (e.g., in a CLI tool). 70 type MockTestingT struct { 71 MockName string 72 } 73 74 func (t MockTestingT) Fail() {} 75 func (t MockTestingT) FailNow() {} 76 func (t MockTestingT) Fatal(args ...interface{}) {} 77 func (t MockTestingT) Fatalf(format string, args ...interface{}) {} 78 func (t MockTestingT) Error(args ...interface{}) {} 79 func (t MockTestingT) Errorf(format string, args ...interface{}) {} 80 func (t MockTestingT) Name() string { 81 return t.MockName 82 }