@@ -62,7 +62,34 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
6262 return events.APIGatewayProxyResponse {StatusCode : http .StatusOK }, nil
6363 }
6464
65- cfg , err := config .LoadDefaultConfig (context .TODO (), config .WithRegion ("us-east-2" ))
65+ // parse valid regions from env var
66+ validRegions := strings .Split (os .Getenv ("VALID_REGIONS" ), "," )
67+ region := "us-east-2"
68+ imageID := ""
69+ instanceType := types .InstanceTypeC7aLarge
70+ instanceTypes := instanceType .Values ()
71+
72+ for _ , label := range event .GetWorkflowJob ().Labels {
73+ // check AMI
74+ if strings .HasPrefix (label , "ami-" ) {
75+ imageID = label
76+ }
77+ // check region
78+ for _ , r := range validRegions {
79+ if label == r {
80+ region = label
81+ break
82+ }
83+ }
84+ // check instance type
85+ for i := range instanceTypes {
86+ if label == string (instanceTypes [i ]) {
87+ instanceType = instanceTypes [i ]
88+ }
89+ }
90+ }
91+
92+ cfg , err := config .LoadDefaultConfig (context .TODO (), config .WithRegion (region ))
6693 if err != nil {
6794 return events.APIGatewayProxyResponse {StatusCode : http .StatusInternalServerError }, err
6895 }
@@ -91,21 +118,58 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
91118 extraLabels = "," + extraLabels
92119 }
93120
94- subnetID := os .Getenv ("SUBNET_ID" )
95- if subnetID == "" {
96- slog .Error ("SUBNET_ID env var not set" )
121+ // discover subnets by tag
122+ subnetTags := strings .Split (os .Getenv ("SUBNET_TAGS" ), "," )
123+
124+ filters := []types.Filter {}
125+ for _ , tag := range subnetTags {
126+ parts := strings .SplitN (tag , "=" , 2 )
127+ if len (parts ) != 2 {
128+ continue
129+ }
130+ filters = append (filters , types.Filter {
131+ Name : aws .String ("tag:" + parts [0 ]),
132+ Values : []string {parts [1 ]},
133+ })
134+ }
135+
136+ subnetResult , err := svc .DescribeSubnets (context .TODO (), & ec2.DescribeSubnetsInput {
137+ Filters : filters ,
138+ })
139+ if err != nil {
140+ return events.APIGatewayProxyResponse {StatusCode : http .StatusInternalServerError }, err
141+ }
97142
98- return events.APIGatewayProxyResponse {StatusCode : http .StatusInternalServerError }, errors .New ("subnet id missing" )
143+ if len (subnetResult .Subnets ) == 0 {
144+ return events.APIGatewayProxyResponse {StatusCode : http .StatusInternalServerError },
145+ fmt .Errorf ("no subnets found with tags %s in region %s" , os .Getenv ("SUBNET_TAGS" ), region )
99146 }
100147
101- sgIDs := os .Getenv ("SECURITY_GROUP_IDS" )
102- if sgIDs == "" {
103- slog .Error ("SECURITY_GROUP_IDS env var not set" )
148+ subnetID := * subnetResult .Subnets [0 ].SubnetId
149+
150+ // discover security groups by tag
151+ sgTags := strings .Split (os .Getenv ("SECURITY_GROUP_TAGS" ), "," )
104152
105- return events.APIGatewayProxyResponse {StatusCode : http .StatusInternalServerError }, errors .New ("security groups missing" )
153+ filters = []types.Filter {}
154+ for _ , tag := range sgTags {
155+ parts := strings .SplitN (tag , "=" , 2 )
156+ if len (parts ) != 2 {
157+ continue
158+ }
159+ filters = append (filters , types.Filter {
160+ Name : aws .String ("tag:" + parts [0 ]),
161+ Values : []string {parts [1 ]},
162+ })
106163 }
107164
108- securityGroups := strings .Split (sgIDs , "," )
165+ sgResult , err := svc .DescribeSecurityGroups (context .TODO (), & ec2.DescribeSecurityGroupsInput {
166+ Filters : filters ,
167+ })
168+
169+ securityGroups := []string {}
170+ for _ , sg := range sgResult .SecurityGroups {
171+ securityGroups = append (securityGroups , * sg .GroupId )
172+ }
109173
110174 keyName := os .Getenv ("KEY_NAME" )
111175 if keyName == "" {
@@ -121,13 +185,6 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
121185 return events.APIGatewayProxyResponse {StatusCode : http .StatusInternalServerError }, errors .New ("instance profile arn missing" )
122186 }
123187
124- imageID := os .Getenv ("IMAGE_ID" )
125- if imageID == "" {
126- slog .Error ("IMAGE_ID env var not set" )
127-
128- return events.APIGatewayProxyResponse {StatusCode : http .StatusInternalServerError }, errors .New ("image id missing" )
129- }
130-
131188 tags := []types.Tag {
132189 {
133190 Key : aws .String ("GitHub Workflow Job Event ID" ),
@@ -146,19 +203,6 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
146203 return events.APIGatewayProxyResponse {StatusCode : http .StatusOK }, nil
147204 }
148205
149- instanceType := types .InstanceTypeC7aLarge
150-
151- instanceTypes := instanceType .Values ()
152- for _ , label := range event .GetWorkflowJob ().Labels {
153- for i := range instanceTypes {
154- if label == string (instanceTypes [i ]) {
155- instanceType = instanceTypes [i ]
156-
157- break
158- }
159- }
160- }
161-
162206 slog .Info ("creating instance" , "instanceType" , instanceType )
163207
164208 tpl , err := template .New ("userdata" ).Parse (userData )
0 commit comments