diff --git a/aws/plugin.go b/aws/plugin.go index 1b04aa73b..340e78115 100644 --- a/aws/plugin.go +++ b/aws/plugin.go @@ -119,6 +119,7 @@ func Plugin(ctx context.Context) *plugin.Plugin { "aws_backup_selection": tableAwsBackupSelection(ctx), "aws_backup_vault": tableAwsBackupVault(ctx), "aws_backup_job": tableAwsBackupJob(ctx), + "aws_batch_queue": tableAwsBatchQueue(ctx), "aws_cloudcontrol_resource": tableAwsCloudControlResource(ctx), "aws_cloudformation_stack": tableAwsCloudFormationStack(ctx), "aws_cloudformation_stack_resource": tableAwsCloudFormationStackResource(ctx), diff --git a/aws/service.go b/aws/service.go index d3fe76386..b04d4b226 100644 --- a/aws/service.go +++ b/aws/service.go @@ -34,6 +34,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/auditmanager" "github.com/aws/aws-sdk-go-v2/service/autoscaling" "github.com/aws/aws-sdk-go-v2/service/backup" + "github.com/aws/aws-sdk-go-v2/service/batch" "github.com/aws/aws-sdk-go-v2/service/cloudcontrol" "github.com/aws/aws-sdk-go-v2/service/cloudformation" "github.com/aws/aws-sdk-go-v2/service/cloudfront" @@ -326,6 +327,20 @@ func BackupClient(ctx context.Context, d *plugin.QueryData) (*backup.Client, err return backup.NewFromConfig(*cfg), nil } +func BatchClient(ctx context.Context, d *plugin.QueryData) (*batch.Client, error) { + conf, err := getClientForQuerySupportedRegion(ctx, d, AWS_BATCH_SERVICE_ID) + if err != nil { + return nil, err + } + + if conf == nil { + return nil, nil + } + + client := batch.NewFromConfig(*conf) + return client, nil +} + func CloudControlClient(ctx context.Context, d *plugin.QueryData) (*cloudcontrol.Client, error) { // CloudControl returns GeneralServiceException in a lot of situations, which // AWS SDK treats as retryable. This is frustrating because we end up retrying diff --git a/aws/table_aws_batch_queue.go b/aws/table_aws_batch_queue.go new file mode 100644 index 000000000..175c8a13c --- /dev/null +++ b/aws/table_aws_batch_queue.go @@ -0,0 +1,175 @@ +package aws + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/batch" + "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/v5/plugin" + "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" +) + +func tableAwsBatchQueue(_ context.Context) *plugin.Table { + return &plugin.Table{ + Name: "aws_batch_queue", + Description: "AWS Batch Queue", + Get: &plugin.GetConfig{ + KeyColumns: plugin.SingleColumn("job_queue_name"), + IgnoreConfig: &plugin.IgnoreConfig{ + ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"JobQueueNotFoundException"}), + }, + Hydrate: getBatchQueue, + Tags: map[string]string{"service": "batch", "action": "DescribeJobQueues"}, + }, + List: &plugin.ListConfig{ + Hydrate: listBatchQueues, + Tags: map[string]string{"service": "batch", "action": "DescribeJobQueues"}, + }, + GetMatrixItemFunc: SupportedRegionMatrix(AWS_BATCH_SERVICE_ID), + Columns: awsRegionalColumns([]*plugin.Column{ + { + Name: "job_queue_name", + Description: "The name of the job queue", + Type: proto.ColumnType_STRING, + }, + { + Name: "arn", + Description: "The Amazon Resource Name (ARN) of the job queue", + Type: proto.ColumnType_STRING, + Transform: transform.FromField("JobQueueArn"), + }, + { + Name: "state", + Description: "The state of the job queue (ENABLED or DISABLED)", + Type: proto.ColumnType_STRING, + }, + { + Name: "status", + Description: "The status of the job queue (CREATING, UPDATING, DELETING, or DELETED)", + Type: proto.ColumnType_STRING, + }, + { + Name: "priority", + Description: "The priority of the job queue", + Type: proto.ColumnType_INT, + }, + { + Name: "compute_environment_order", + Description: "The compute environments that are attached to the job queue and the order in which job placement is preferred", + Type: proto.ColumnType_JSON, + }, + { + Name: "scheduling_policy_arn", + Description: "The ARN of the scheduling policy", + Type: proto.ColumnType_STRING, + }, + { + Name: "status_reason", + Description: "A short, human-readable string to provide additional details about the current status of the job queue", + Type: proto.ColumnType_STRING, + }, + + // Standard columns for all tables + { + Name: "tags", + Description: "The tags assigned to the job queue", + Type: proto.ColumnType_JSON, + }, + { + Name: "title", + Description: resourceInterfaceDescription("title"), + Type: proto.ColumnType_STRING, + Transform: transform.FromField("JobQueueName"), + }, + { + Name: "akas", + Description: resourceInterfaceDescription("akas"), + Type: proto.ColumnType_JSON, + Transform: transform.FromField("JobQueueArn").Transform(transform.EnsureStringArray), + }, + }), + } +} + +func listBatchQueues(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { + // Create service client + svc, err := BatchClient(ctx, d) + if err != nil { + plugin.Logger(ctx).Error("aws_batch_queue.listBatchQueues", "client_error", err) + return nil, err + } + + // Unsupported region check + if svc == nil { + return nil, nil + } + + // Limiting the results + maxLimit := int32(100) + if d.QueryContext.Limit != nil { + limit := int32(*d.QueryContext.Limit) + if limit < maxLimit { + maxLimit = limit + } + } + + input := &batch.DescribeJobQueuesInput{ + MaxResults: aws.Int32(maxLimit), + } + + paginator := batch.NewDescribeJobQueuesPaginator(svc, input) + for paginator.HasMorePages() { + output, err := paginator.NextPage(ctx) + if err != nil { + plugin.Logger(ctx).Error("aws_batch_queue.listBatchQueues", "api_error", err) + return nil, err + } + + for _, queue := range output.JobQueues { + d.StreamListItem(ctx, queue) + + // Context can be cancelled due to manual cancellation or the limit has been hit + if d.RowsRemaining(ctx) == 0 { + return nil, nil + } + } + } + + return nil, nil +} + +func getBatchQueue(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { + queueName := d.EqualsQualString("job_queue_name") + if queueName == "" { + return nil, nil + } + + // Create service client + svc, err := BatchClient(ctx, d) + if err != nil { + plugin.Logger(ctx).Error("aws_batch_queue.getBatchQueue", "client_error", err) + return nil, err + } + + // Unsupported region check + if svc == nil { + return nil, nil + } + + input := &batch.DescribeJobQueuesInput{ + JobQueues: []string{queueName}, + } + + output, err := svc.DescribeJobQueues(ctx, input) + if err != nil { + plugin.Logger(ctx).Error("aws_batch_queue.getBatchQueue", "api_error", err) + return nil, err + } + + if len(output.JobQueues) == 0 { + return nil, nil + } + + return output.JobQueues[0], nil +} diff --git a/docs/tables/aws_batch_queue.md b/docs/tables/aws_batch_queue.md new file mode 100644 index 000000000..3faa8d383 --- /dev/null +++ b/docs/tables/aws_batch_queue.md @@ -0,0 +1,200 @@ +--- +title: "Steampipe Table: aws_batch_queue - Query AWS Batch Job Queues using SQL" +description: "Allows users to query AWS Batch Job Queues for detailed information about queue configurations, statuses, and related attributes." +folder: "Batch" +--- + +# Table: aws_batch_queue + +AWS Batch Job Queues are resources in the AWS Batch service that store jobs until the AWS Batch Scheduler runs them on a compute environment. Job queues have a priority that the scheduler uses to determine which jobs in which queue should be evaluated for execution first. + +## Table Usage Guide + +The `aws_batch_queue` table provides insights into job queue configurations within AWS Batch. As a DevOps engineer, explore queue-specific details through this table, including priority settings, compute environment associations, and queue statuses. Utilize it to monitor queue configurations, verify compute environment associations, and ensure appropriate job queue priorities are set. + +## Examples + +### Basic info +Get a quick overview of all AWS Batch job queues, including their name, state, status, and priority. This is useful for inventory and monitoring purposes. + +```sql+postgres +select + job_queue_name, + state, + status, + priority +from + aws_batch_queue; +``` + +```sql+sqlite +select + job_queue_name, + state, + status, + priority +from + aws_batch_queue; +``` + +### List enabled job queues +Identify all job queues that are currently enabled. This helps you focus on active queues that can accept jobs. + +```sql+postgres +select + job_queue_name, + state, + priority +from + aws_batch_queue +where + state = 'ENABLED'; +``` + +```sql+sqlite +select + job_queue_name, + state, + priority +from + aws_batch_queue +where + state = 'ENABLED'; +``` + +### List job queues by state +See how many job queues exist in each state (ENABLED, DISABLED, etc.). This is useful for capacity planning and operational audits. + +```sql+postgres +select + state, + count(*) as queue_count +from + aws_batch_queue +group by + state +order by + queue_count desc; +``` + +```sql+sqlite +select + state, + count(*) as queue_count +from + aws_batch_queue +group by + state +order by + queue_count desc; +``` + +### Get compute environments associated with each job queue +List the compute environments attached to each job queue and their order of preference. This helps you understand job placement and compute resource allocation. + +```sql+postgres +select + job_queue_name, + state, + c->>'Order' as order_priority, + c->>'ComputeEnvironment' as compute_environment +from + aws_batch_queue, + jsonb_array_elements(compute_environment_order) as c +order by + job_queue_name, + (c->>'Order')::int; +``` + +```sql+sqlite +select + job_queue_name, + state, + json_extract(c.value, '$.Order') as order_priority, + json_extract(c.value, '$.ComputeEnvironment') as compute_environment +from + aws_batch_queue, + json_each(compute_environment_order) as c +order by + job_queue_name, + CAST(json_extract(c.value, '$.Order') as integer); +``` + +### Find job queues with high priority (lower number means higher priority) +Identify job queues with a high priority (lower number). This is useful for understanding which queues are preferred for job scheduling. + +```sql+postgres +select + job_queue_name, + state, + priority +from + aws_batch_queue +where + priority < 50 +order by + priority; +``` + +```sql+sqlite +select + job_queue_name, + state, + priority +from + aws_batch_queue +where + priority < 50 +order by + priority; +``` + +### Find job queues with scheduling policies +Find job queues that have a scheduling policy attached. This helps you identify queues with custom scheduling behavior. + +```sql+postgres +select + job_queue_name, + state, + scheduling_policy_arn +from + aws_batch_queue +where + scheduling_policy_arn is not null; +``` + +```sql+sqlite +select + job_queue_name, + state, + scheduling_policy_arn +from + aws_batch_queue +where + scheduling_policy_arn is not null; +``` + +### List job queues with their tags +Show all job queues that have tags assigned. This is useful for cost allocation, resource organization, and compliance. + +```sql+postgres +select + job_queue_name, + state, + tags +from + aws_batch_queue +where + tags is not null; +``` + +```sql+sqlite +select + job_queue_name, + state, + tags +from + aws_batch_queue +where + tags is not null; +``` diff --git a/go.mod b/go.mod index 263b26103..c36f02894 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/auditmanager v1.32.4 github.com/aws/aws-sdk-go-v2/service/autoscaling v1.40.5 github.com/aws/aws-sdk-go-v2/service/backup v1.34.2 + github.com/aws/aws-sdk-go-v2/service/batch v1.37.4 github.com/aws/aws-sdk-go-v2/service/cloudcontrol v1.18.4 github.com/aws/aws-sdk-go-v2/service/cloudformation v1.49.0 github.com/aws/aws-sdk-go-v2/service/cloudfront v1.35.4 diff --git a/go.sum b/go.sum index db1e2fbdf..63a4f40ff 100644 --- a/go.sum +++ b/go.sum @@ -269,6 +269,8 @@ github.com/aws/aws-sdk-go-v2/service/autoscaling v1.40.5 h1:vhdJymxlWS2qftzLiuCj github.com/aws/aws-sdk-go-v2/service/autoscaling v1.40.5/go.mod h1:ZErgk/bPaaZIpj+lUWGlwI1A0UFhSIscgnCPzTLnb2s= github.com/aws/aws-sdk-go-v2/service/backup v1.34.2 h1:M7OwCjc77SL2zcpvAGV/ORMik1zh9q7PjZWk6hQDOpI= github.com/aws/aws-sdk-go-v2/service/backup v1.34.2/go.mod h1:AI+UC6udX0Vo3bScHfV2LMiwecGjerEhGJZ9oFOW+2w= +github.com/aws/aws-sdk-go-v2/service/batch v1.37.4 h1:N54MVxMi3qU/s9uJKcyU+dQnGCpCx/o3+VayLG1SaKo= +github.com/aws/aws-sdk-go-v2/service/batch v1.37.4/go.mod h1:hqOLhSiZjmX2+1axOvbJ6OdBtl+WsYvolcszo2j7+NQ= github.com/aws/aws-sdk-go-v2/service/cloudcontrol v1.18.4 h1:y9xLchBUDKriRuDsA6OwwzgP9binHw67dR0uicHmOQQ= github.com/aws/aws-sdk-go-v2/service/cloudcontrol v1.18.4/go.mod h1:oOvzqGwjzl5fyWi0C7YfOalzMDS8R4yapREwUVV5gBY= github.com/aws/aws-sdk-go-v2/service/cloudformation v1.49.0 h1:XSUAzNAV7kCSWhV8duijMz+FrOdMqbLiRXXWBs6BA9A=