Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linters:
- lll
- cyclop
- funlen
- maintidx
formatters:
# Enable specific formatter.
# Default: [] (uses standard Go formatting)
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ aws secretsmanager create-secret --name my-github-pat --secret-string <PAT>

## Deployment

Deploy the stack with SAM and provide the secret name and any additional runner
labels as parameters:
Deploy the stack with SAM and provide the secret name, AMI, subnet, security groups and
EC2 key pair used for the runner. You may also specify additional runner labels:

```bash
sam deploy \
--parameter-overrides GitHubPATSecretName=my-github-pat ExtraRunnerLabels="gpu"
--parameter-overrides GitHubPATSecretName=my-github-pat \
ExtraRunnerLabels="gpu" \
ImageId=ami-0123456789abcdef0 \
SubnetId=subnet-12345678 \
SecurityGroupIds=sg-12345678 \
KeyName=my-key
```

The `ExtraRunnerLabels` parameter is optional. When supplied, the labels are
added to the default runner labels.
added to the default runner labels. All other parameters are required and must
be specified for your environment.
38 changes: 34 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
extraLabels = "," + extraLabels
}

subnetID := os.Getenv("SUBNET_ID")
if subnetID == "" {
slog.Error("SUBNET_ID env var not set")

return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError}, errors.New("subnet id missing")
}

sgIDs := os.Getenv("SECURITY_GROUP_IDS")
if sgIDs == "" {
slog.Error("SECURITY_GROUP_IDS env var not set")

return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError}, errors.New("security groups missing")
}

securityGroups := strings.Split(sgIDs, ",")

keyName := os.Getenv("KEY_NAME")
if keyName == "" {
slog.Error("KEY_NAME env var not set")

return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError}, errors.New("key name missing")
}

imageID := os.Getenv("IMAGE_ID")
if imageID == "" {
slog.Error("IMAGE_ID env var not set")

return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError}, errors.New("image id missing")
}

tags := []types.Tag{
{
Key: aws.String("GitHub Workflow Job Event ID"),
Expand Down Expand Up @@ -142,19 +172,19 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
MinCount: aws.Int32(1),
MaxCount: aws.Int32(1),
EbsOptimized: aws.Bool(true),
ImageId: aws.String("ami-0c0c88099397fccb4"),
ImageId: aws.String(imageID),
InstanceInitiatedShutdownBehavior: types.ShutdownBehaviorTerminate,
InstanceType: instanceType,
NetworkInterfaces: []types.InstanceNetworkInterfaceSpecification{
{
AssociatePublicIpAddress: aws.Bool(true),
SubnetId: aws.String("subnet-0eb6da43c6f0ef528"),
SubnetId: aws.String(subnetID),
DeleteOnTermination: aws.Bool(true),
DeviceIndex: aws.Int32(0),
Groups: []string{"sg-0f185b577cb2b2802"},
Groups: securityGroups,
},
},
KeyName: aws.String("terraform-20220125192645402400000001"),
KeyName: aws.String(keyName),
Monitoring: &types.RunInstancesMonitoringEnabled{Enabled: aws.Bool(true)},
TagSpecifications: []types.TagSpecification{
{
Expand Down
16 changes: 16 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ Parameters:
Type: String
Default: ""
Description: Additional comma separated labels for the runner
ImageId:
Type: String
Description: AMI ID for the runner instances
SubnetId:
Type: String
Description: Subnet ID for the runner instances
SecurityGroupIds:
Type: String
Description: Comma separated security group IDs for the runner
KeyName:
Type: String
Description: EC2 key pair name for the runner

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Expand Down Expand Up @@ -38,6 +50,10 @@ Resources:
Variables:
GITHUB_PAT_SECRET_NAME: !Ref GitHubPATSecretName
EXTRA_RUNNER_LABELS: !Ref ExtraRunnerLabels
IMAGE_ID: !Ref ImageId
SUBNET_ID: !Ref SubnetId
SECURITY_GROUP_IDS: !Ref SecurityGroupIds
KEY_NAME: !Ref KeyName
Policies:
- Statement:
- Sid: RunInstances
Expand Down