1// Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT. 2 3package batch_test 4 5import ( 6 "fmt" 7 "strings" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/aws/session" 13 "github.com/aws/aws-sdk-go/service/batch" 14) 15 16var _ time.Duration 17var _ strings.Reader 18var _ aws.Config 19 20func parseTime(layout, value string) *time.Time { 21 t, err := time.Parse(layout, value) 22 if err != nil { 23 panic(err) 24 } 25 return &t 26} 27 28// To cancel a job 29// 30// This example cancels a job with the specified job ID. 31func ExampleBatch_CancelJob_shared00() { 32 svc := batch.New(session.New()) 33 input := &batch.CancelJobInput{ 34 JobId: aws.String("1d828f65-7a4d-42e8-996d-3b900ed59dc4"), 35 Reason: aws.String("Cancelling job."), 36 } 37 38 result, err := svc.CancelJob(input) 39 if err != nil { 40 if aerr, ok := err.(awserr.Error); ok { 41 switch aerr.Code() { 42 case batch.ErrCodeClientException: 43 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 44 case batch.ErrCodeServerException: 45 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 46 default: 47 fmt.Println(aerr.Error()) 48 } 49 } else { 50 // Print the error, cast err to awserr.Error to get the Code and 51 // Message from an error. 52 fmt.Println(err.Error()) 53 } 54 return 55 } 56 57 fmt.Println(result) 58} 59 60// To create a managed EC2 compute environment 61// 62// This example creates a managed compute environment with specific C4 instance types 63// that are launched on demand. The compute environment is called C4OnDemand. 64func ExampleBatch_CreateComputeEnvironment_shared00() { 65 svc := batch.New(session.New()) 66 input := &batch.CreateComputeEnvironmentInput{ 67 ComputeEnvironmentName: aws.String("C4OnDemand"), 68 ComputeResources: &batch.ComputeResource{ 69 DesiredvCpus: aws.Int64(48), 70 Ec2KeyPair: aws.String("id_rsa"), 71 InstanceRole: aws.String("ecsInstanceRole"), 72 InstanceTypes: []*string{ 73 aws.String("c4.large"), 74 aws.String("c4.xlarge"), 75 aws.String("c4.2xlarge"), 76 aws.String("c4.4xlarge"), 77 aws.String("c4.8xlarge"), 78 }, 79 MaxvCpus: aws.Int64(128), 80 MinvCpus: aws.Int64(0), 81 SecurityGroupIds: []*string{ 82 aws.String("sg-cf5093b2"), 83 }, 84 Subnets: []*string{ 85 aws.String("subnet-220c0e0a"), 86 aws.String("subnet-1a95556d"), 87 aws.String("subnet-978f6dce"), 88 }, 89 Tags: map[string]*string{ 90 "Name": aws.String("Batch Instance - C4OnDemand"), 91 }, 92 Type: aws.String("EC2"), 93 }, 94 ServiceRole: aws.String("arn:aws:iam::012345678910:role/AWSBatchServiceRole"), 95 State: aws.String("ENABLED"), 96 Type: aws.String("MANAGED"), 97 } 98 99 result, err := svc.CreateComputeEnvironment(input) 100 if err != nil { 101 if aerr, ok := err.(awserr.Error); ok { 102 switch aerr.Code() { 103 case batch.ErrCodeClientException: 104 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 105 case batch.ErrCodeServerException: 106 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 107 default: 108 fmt.Println(aerr.Error()) 109 } 110 } else { 111 // Print the error, cast err to awserr.Error to get the Code and 112 // Message from an error. 113 fmt.Println(err.Error()) 114 } 115 return 116 } 117 118 fmt.Println(result) 119} 120 121// To create a managed EC2 Spot compute environment 122// 123// This example creates a managed compute environment with the M4 instance type that 124// is launched when the Spot bid price is at or below 20% of the On-Demand price for 125// the instance type. The compute environment is called M4Spot. 126func ExampleBatch_CreateComputeEnvironment_shared01() { 127 svc := batch.New(session.New()) 128 input := &batch.CreateComputeEnvironmentInput{ 129 ComputeEnvironmentName: aws.String("M4Spot"), 130 ComputeResources: &batch.ComputeResource{ 131 BidPercentage: aws.Int64(20), 132 DesiredvCpus: aws.Int64(4), 133 Ec2KeyPair: aws.String("id_rsa"), 134 InstanceRole: aws.String("ecsInstanceRole"), 135 InstanceTypes: []*string{ 136 aws.String("m4"), 137 }, 138 MaxvCpus: aws.Int64(128), 139 MinvCpus: aws.Int64(0), 140 SecurityGroupIds: []*string{ 141 aws.String("sg-cf5093b2"), 142 }, 143 SpotIamFleetRole: aws.String("arn:aws:iam::012345678910:role/aws-ec2-spot-fleet-role"), 144 Subnets: []*string{ 145 aws.String("subnet-220c0e0a"), 146 aws.String("subnet-1a95556d"), 147 aws.String("subnet-978f6dce"), 148 }, 149 Tags: map[string]*string{ 150 "Name": aws.String("Batch Instance - M4Spot"), 151 }, 152 Type: aws.String("SPOT"), 153 }, 154 ServiceRole: aws.String("arn:aws:iam::012345678910:role/AWSBatchServiceRole"), 155 State: aws.String("ENABLED"), 156 Type: aws.String("MANAGED"), 157 } 158 159 result, err := svc.CreateComputeEnvironment(input) 160 if err != nil { 161 if aerr, ok := err.(awserr.Error); ok { 162 switch aerr.Code() { 163 case batch.ErrCodeClientException: 164 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 165 case batch.ErrCodeServerException: 166 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 167 default: 168 fmt.Println(aerr.Error()) 169 } 170 } else { 171 // Print the error, cast err to awserr.Error to get the Code and 172 // Message from an error. 173 fmt.Println(err.Error()) 174 } 175 return 176 } 177 178 fmt.Println(result) 179} 180 181// To create a job queue with a single compute environment 182// 183// This example creates a job queue called LowPriority that uses the M4Spot compute 184// environment. 185func ExampleBatch_CreateJobQueue_shared00() { 186 svc := batch.New(session.New()) 187 input := &batch.CreateJobQueueInput{ 188 ComputeEnvironmentOrder: []*batch.ComputeEnvironmentOrder{ 189 { 190 ComputeEnvironment: aws.String("M4Spot"), 191 Order: aws.Int64(1), 192 }, 193 }, 194 JobQueueName: aws.String("LowPriority"), 195 Priority: aws.Int64(1), 196 State: aws.String("ENABLED"), 197 } 198 199 result, err := svc.CreateJobQueue(input) 200 if err != nil { 201 if aerr, ok := err.(awserr.Error); ok { 202 switch aerr.Code() { 203 case batch.ErrCodeClientException: 204 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 205 case batch.ErrCodeServerException: 206 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 207 default: 208 fmt.Println(aerr.Error()) 209 } 210 } else { 211 // Print the error, cast err to awserr.Error to get the Code and 212 // Message from an error. 213 fmt.Println(err.Error()) 214 } 215 return 216 } 217 218 fmt.Println(result) 219} 220 221// To create a job queue with multiple compute environments 222// 223// This example creates a job queue called HighPriority that uses the C4OnDemand compute 224// environment with an order of 1 and the M4Spot compute environment with an order of 225// 2. 226func ExampleBatch_CreateJobQueue_shared01() { 227 svc := batch.New(session.New()) 228 input := &batch.CreateJobQueueInput{ 229 ComputeEnvironmentOrder: []*batch.ComputeEnvironmentOrder{ 230 { 231 ComputeEnvironment: aws.String("C4OnDemand"), 232 Order: aws.Int64(1), 233 }, 234 { 235 ComputeEnvironment: aws.String("M4Spot"), 236 Order: aws.Int64(2), 237 }, 238 }, 239 JobQueueName: aws.String("HighPriority"), 240 Priority: aws.Int64(10), 241 State: aws.String("ENABLED"), 242 } 243 244 result, err := svc.CreateJobQueue(input) 245 if err != nil { 246 if aerr, ok := err.(awserr.Error); ok { 247 switch aerr.Code() { 248 case batch.ErrCodeClientException: 249 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 250 case batch.ErrCodeServerException: 251 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 252 default: 253 fmt.Println(aerr.Error()) 254 } 255 } else { 256 // Print the error, cast err to awserr.Error to get the Code and 257 // Message from an error. 258 fmt.Println(err.Error()) 259 } 260 return 261 } 262 263 fmt.Println(result) 264} 265 266// To delete a compute environment 267// 268// This example deletes the P2OnDemand compute environment. 269func ExampleBatch_DeleteComputeEnvironment_shared00() { 270 svc := batch.New(session.New()) 271 input := &batch.DeleteComputeEnvironmentInput{ 272 ComputeEnvironment: aws.String("P2OnDemand"), 273 } 274 275 result, err := svc.DeleteComputeEnvironment(input) 276 if err != nil { 277 if aerr, ok := err.(awserr.Error); ok { 278 switch aerr.Code() { 279 case batch.ErrCodeClientException: 280 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 281 case batch.ErrCodeServerException: 282 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 283 default: 284 fmt.Println(aerr.Error()) 285 } 286 } else { 287 // Print the error, cast err to awserr.Error to get the Code and 288 // Message from an error. 289 fmt.Println(err.Error()) 290 } 291 return 292 } 293 294 fmt.Println(result) 295} 296 297// To delete a job queue 298// 299// This example deletes the GPGPU job queue. 300func ExampleBatch_DeleteJobQueue_shared00() { 301 svc := batch.New(session.New()) 302 input := &batch.DeleteJobQueueInput{ 303 JobQueue: aws.String("GPGPU"), 304 } 305 306 result, err := svc.DeleteJobQueue(input) 307 if err != nil { 308 if aerr, ok := err.(awserr.Error); ok { 309 switch aerr.Code() { 310 case batch.ErrCodeClientException: 311 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 312 case batch.ErrCodeServerException: 313 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 314 default: 315 fmt.Println(aerr.Error()) 316 } 317 } else { 318 // Print the error, cast err to awserr.Error to get the Code and 319 // Message from an error. 320 fmt.Println(err.Error()) 321 } 322 return 323 } 324 325 fmt.Println(result) 326} 327 328// To deregister a job definition 329// 330// This example deregisters a job definition called sleep10. 331func ExampleBatch_DeregisterJobDefinition_shared00() { 332 svc := batch.New(session.New()) 333 input := &batch.DeregisterJobDefinitionInput{ 334 JobDefinition: aws.String("sleep10"), 335 } 336 337 result, err := svc.DeregisterJobDefinition(input) 338 if err != nil { 339 if aerr, ok := err.(awserr.Error); ok { 340 switch aerr.Code() { 341 case batch.ErrCodeClientException: 342 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 343 case batch.ErrCodeServerException: 344 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 345 default: 346 fmt.Println(aerr.Error()) 347 } 348 } else { 349 // Print the error, cast err to awserr.Error to get the Code and 350 // Message from an error. 351 fmt.Println(err.Error()) 352 } 353 return 354 } 355 356 fmt.Println(result) 357} 358 359// To describe a compute environment 360// 361// This example describes the P2OnDemand compute environment. 362func ExampleBatch_DescribeComputeEnvironments_shared00() { 363 svc := batch.New(session.New()) 364 input := &batch.DescribeComputeEnvironmentsInput{ 365 ComputeEnvironments: []*string{ 366 aws.String("P2OnDemand"), 367 }, 368 } 369 370 result, err := svc.DescribeComputeEnvironments(input) 371 if err != nil { 372 if aerr, ok := err.(awserr.Error); ok { 373 switch aerr.Code() { 374 case batch.ErrCodeClientException: 375 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 376 case batch.ErrCodeServerException: 377 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 378 default: 379 fmt.Println(aerr.Error()) 380 } 381 } else { 382 // Print the error, cast err to awserr.Error to get the Code and 383 // Message from an error. 384 fmt.Println(err.Error()) 385 } 386 return 387 } 388 389 fmt.Println(result) 390} 391 392// To describe active job definitions 393// 394// This example describes all of your active job definitions. 395func ExampleBatch_DescribeJobDefinitions_shared00() { 396 svc := batch.New(session.New()) 397 input := &batch.DescribeJobDefinitionsInput{ 398 Status: aws.String("ACTIVE"), 399 } 400 401 result, err := svc.DescribeJobDefinitions(input) 402 if err != nil { 403 if aerr, ok := err.(awserr.Error); ok { 404 switch aerr.Code() { 405 case batch.ErrCodeClientException: 406 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 407 case batch.ErrCodeServerException: 408 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 409 default: 410 fmt.Println(aerr.Error()) 411 } 412 } else { 413 // Print the error, cast err to awserr.Error to get the Code and 414 // Message from an error. 415 fmt.Println(err.Error()) 416 } 417 return 418 } 419 420 fmt.Println(result) 421} 422 423// To describe a job queue 424// 425// This example describes the HighPriority job queue. 426func ExampleBatch_DescribeJobQueues_shared00() { 427 svc := batch.New(session.New()) 428 input := &batch.DescribeJobQueuesInput{ 429 JobQueues: []*string{ 430 aws.String("HighPriority"), 431 }, 432 } 433 434 result, err := svc.DescribeJobQueues(input) 435 if err != nil { 436 if aerr, ok := err.(awserr.Error); ok { 437 switch aerr.Code() { 438 case batch.ErrCodeClientException: 439 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 440 case batch.ErrCodeServerException: 441 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 442 default: 443 fmt.Println(aerr.Error()) 444 } 445 } else { 446 // Print the error, cast err to awserr.Error to get the Code and 447 // Message from an error. 448 fmt.Println(err.Error()) 449 } 450 return 451 } 452 453 fmt.Println(result) 454} 455 456// To describe a specific job 457// 458// This example describes a job with the specified job ID. 459func ExampleBatch_DescribeJobs_shared00() { 460 svc := batch.New(session.New()) 461 input := &batch.DescribeJobsInput{ 462 Jobs: []*string{ 463 aws.String("24fa2d7a-64c4-49d2-8b47-f8da4fbde8e9"), 464 }, 465 } 466 467 result, err := svc.DescribeJobs(input) 468 if err != nil { 469 if aerr, ok := err.(awserr.Error); ok { 470 switch aerr.Code() { 471 case batch.ErrCodeClientException: 472 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 473 case batch.ErrCodeServerException: 474 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 475 default: 476 fmt.Println(aerr.Error()) 477 } 478 } else { 479 // Print the error, cast err to awserr.Error to get the Code and 480 // Message from an error. 481 fmt.Println(err.Error()) 482 } 483 return 484 } 485 486 fmt.Println(result) 487} 488 489// To list running jobs 490// 491// This example lists the running jobs in the HighPriority job queue. 492func ExampleBatch_ListJobs_shared00() { 493 svc := batch.New(session.New()) 494 input := &batch.ListJobsInput{ 495 JobQueue: aws.String("HighPriority"), 496 } 497 498 result, err := svc.ListJobs(input) 499 if err != nil { 500 if aerr, ok := err.(awserr.Error); ok { 501 switch aerr.Code() { 502 case batch.ErrCodeClientException: 503 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 504 case batch.ErrCodeServerException: 505 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 506 default: 507 fmt.Println(aerr.Error()) 508 } 509 } else { 510 // Print the error, cast err to awserr.Error to get the Code and 511 // Message from an error. 512 fmt.Println(err.Error()) 513 } 514 return 515 } 516 517 fmt.Println(result) 518} 519 520// To list submitted jobs 521// 522// This example lists jobs in the HighPriority job queue that are in the SUBMITTED job 523// status. 524func ExampleBatch_ListJobs_shared01() { 525 svc := batch.New(session.New()) 526 input := &batch.ListJobsInput{ 527 JobQueue: aws.String("HighPriority"), 528 JobStatus: aws.String("SUBMITTED"), 529 } 530 531 result, err := svc.ListJobs(input) 532 if err != nil { 533 if aerr, ok := err.(awserr.Error); ok { 534 switch aerr.Code() { 535 case batch.ErrCodeClientException: 536 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 537 case batch.ErrCodeServerException: 538 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 539 default: 540 fmt.Println(aerr.Error()) 541 } 542 } else { 543 // Print the error, cast err to awserr.Error to get the Code and 544 // Message from an error. 545 fmt.Println(err.Error()) 546 } 547 return 548 } 549 550 fmt.Println(result) 551} 552 553// To register a job definition 554// 555// This example registers a job definition for a simple container job. 556func ExampleBatch_RegisterJobDefinition_shared00() { 557 svc := batch.New(session.New()) 558 input := &batch.RegisterJobDefinitionInput{ 559 ContainerProperties: &batch.ContainerProperties{ 560 Command: []*string{ 561 aws.String("sleep"), 562 aws.String("10"), 563 }, 564 Image: aws.String("busybox"), 565 Memory: aws.Int64(128), 566 Vcpus: aws.Int64(1), 567 }, 568 JobDefinitionName: aws.String("sleep10"), 569 Type: aws.String("container"), 570 } 571 572 result, err := svc.RegisterJobDefinition(input) 573 if err != nil { 574 if aerr, ok := err.(awserr.Error); ok { 575 switch aerr.Code() { 576 case batch.ErrCodeClientException: 577 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 578 case batch.ErrCodeServerException: 579 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 580 default: 581 fmt.Println(aerr.Error()) 582 } 583 } else { 584 // Print the error, cast err to awserr.Error to get the Code and 585 // Message from an error. 586 fmt.Println(err.Error()) 587 } 588 return 589 } 590 591 fmt.Println(result) 592} 593 594// To submit a job to a queue 595// 596// This example submits a simple container job called example to the HighPriority job 597// queue. 598func ExampleBatch_SubmitJob_shared00() { 599 svc := batch.New(session.New()) 600 input := &batch.SubmitJobInput{ 601 JobDefinition: aws.String("sleep60"), 602 JobName: aws.String("example"), 603 JobQueue: aws.String("HighPriority"), 604 } 605 606 result, err := svc.SubmitJob(input) 607 if err != nil { 608 if aerr, ok := err.(awserr.Error); ok { 609 switch aerr.Code() { 610 case batch.ErrCodeClientException: 611 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 612 case batch.ErrCodeServerException: 613 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 614 default: 615 fmt.Println(aerr.Error()) 616 } 617 } else { 618 // Print the error, cast err to awserr.Error to get the Code and 619 // Message from an error. 620 fmt.Println(err.Error()) 621 } 622 return 623 } 624 625 fmt.Println(result) 626} 627 628// To terminate a job 629// 630// This example terminates a job with the specified job ID. 631func ExampleBatch_TerminateJob_shared00() { 632 svc := batch.New(session.New()) 633 input := &batch.TerminateJobInput{ 634 JobId: aws.String("61e743ed-35e4-48da-b2de-5c8333821c84"), 635 Reason: aws.String("Terminating job."), 636 } 637 638 result, err := svc.TerminateJob(input) 639 if err != nil { 640 if aerr, ok := err.(awserr.Error); ok { 641 switch aerr.Code() { 642 case batch.ErrCodeClientException: 643 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 644 case batch.ErrCodeServerException: 645 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 646 default: 647 fmt.Println(aerr.Error()) 648 } 649 } else { 650 // Print the error, cast err to awserr.Error to get the Code and 651 // Message from an error. 652 fmt.Println(err.Error()) 653 } 654 return 655 } 656 657 fmt.Println(result) 658} 659 660// To update a compute environment 661// 662// This example disables the P2OnDemand compute environment so it can be deleted. 663func ExampleBatch_UpdateComputeEnvironment_shared00() { 664 svc := batch.New(session.New()) 665 input := &batch.UpdateComputeEnvironmentInput{ 666 ComputeEnvironment: aws.String("P2OnDemand"), 667 State: aws.String("DISABLED"), 668 } 669 670 result, err := svc.UpdateComputeEnvironment(input) 671 if err != nil { 672 if aerr, ok := err.(awserr.Error); ok { 673 switch aerr.Code() { 674 case batch.ErrCodeClientException: 675 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 676 case batch.ErrCodeServerException: 677 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 678 default: 679 fmt.Println(aerr.Error()) 680 } 681 } else { 682 // Print the error, cast err to awserr.Error to get the Code and 683 // Message from an error. 684 fmt.Println(err.Error()) 685 } 686 return 687 } 688 689 fmt.Println(result) 690} 691 692// To update a job queue 693// 694// This example disables a job queue so that it can be deleted. 695func ExampleBatch_UpdateJobQueue_shared00() { 696 svc := batch.New(session.New()) 697 input := &batch.UpdateJobQueueInput{ 698 JobQueue: aws.String("GPGPU"), 699 State: aws.String("DISABLED"), 700 } 701 702 result, err := svc.UpdateJobQueue(input) 703 if err != nil { 704 if aerr, ok := err.(awserr.Error); ok { 705 switch aerr.Code() { 706 case batch.ErrCodeClientException: 707 fmt.Println(batch.ErrCodeClientException, aerr.Error()) 708 case batch.ErrCodeServerException: 709 fmt.Println(batch.ErrCodeServerException, aerr.Error()) 710 default: 711 fmt.Println(aerr.Error()) 712 } 713 } else { 714 // Print the error, cast err to awserr.Error to get the Code and 715 // Message from an error. 716 fmt.Println(err.Error()) 717 } 718 return 719 } 720 721 fmt.Println(result) 722} 723