Skip to content

Commit

Permalink
Merge pull request #8026 from atsushi-ishibashi/issue6454
Browse files Browse the repository at this point in the history
resource/batch_compute_environment: Support LaunchTemplate
  • Loading branch information
bflad authored Mar 29, 2019
2 parents 964e6be + e453753 commit 6c42951
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
46 changes: 46 additions & 0 deletions aws/resource_aws_batch_compute_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ func resourceAwsBatchComputeEnvironment() *schema.Resource {
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"launch_template": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"launch_template_id": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"compute_resources.0.launch_template.0.launch_template_name"},
},
"launch_template_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"compute_resources.0.launch_template.0.launch_template_id"},
},
"version": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"max_vcpus": {
Type: schema.TypeInt,
Required: true,
Expand Down Expand Up @@ -215,6 +239,20 @@ func resourceAwsBatchComputeEnvironmentCreate(d *schema.ResourceData, meta inter
if v, ok := computeResource["tags"]; ok {
input.ComputeResources.Tags = tagsFromMapGeneric(v.(map[string]interface{}))
}

if raw, ok := computeResource["launch_template"]; ok && len(raw.([]interface{})) > 0 {
input.ComputeResources.LaunchTemplate = &batch.LaunchTemplateSpecification{}
launchTemplate := raw.([]interface{})[0].(map[string]interface{})
if v, ok := launchTemplate["launch_template_id"]; ok {
input.ComputeResources.LaunchTemplate.LaunchTemplateId = aws.String(v.(string))
}
if v, ok := launchTemplate["launch_template_name"]; ok {
input.ComputeResources.LaunchTemplate.LaunchTemplateName = aws.String(v.(string))
}
if v, ok := launchTemplate["version"]; ok {
input.ComputeResources.LaunchTemplate.Version = aws.String(v.(string))
}
}
}

log.Printf("[DEBUG] Create compute environment %s.\n", input)
Expand Down Expand Up @@ -298,6 +336,14 @@ func flattenBatchComputeResources(computeResource *batch.ComputeResource) []map[
m["tags"] = tagsToMapGeneric(computeResource.Tags)
m["type"] = aws.StringValue(computeResource.Type)

if launchTemplate := computeResource.LaunchTemplate; launchTemplate != nil {
lt := make(map[string]interface{})
lt["launch_template_id"] = aws.StringValue(launchTemplate.LaunchTemplateId)
lt["launch_template_name"] = aws.StringValue(launchTemplate.LaunchTemplateName)
lt["version"] = aws.StringValue(launchTemplate.Version)
m["launch_template"] = []map[string]interface{}{lt}
}

result = append(result, m)
return result
}
Expand Down
58 changes: 58 additions & 0 deletions aws/resource_aws_batch_compute_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ func TestAccAWSBatchComputeEnvironment_createUnmanagedWithComputeResources(t *te
})
}

func TestAccAWSBatchComputeEnvironment_launchTemplate(t *testing.T) {
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSBatchComputeEnvironmentConfigLaunchTemplate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2",
"compute_resources.0.launch_template.#",
"1"),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2",
"compute_resources.0.launch_template.0.launch_template_name",
fmt.Sprintf("tf_acc_test_%d", rInt)),
),
},
},
})
}

func TestAccAWSBatchComputeEnvironment_createSpotWithoutBidPercentage(t *testing.T) {
rInt := acctest.RandInt()

Expand Down Expand Up @@ -719,3 +743,37 @@ resource "aws_batch_compute_environment" "ec2" {
}
`, rInt)
}

func testAccAWSBatchComputeEnvironmentConfigLaunchTemplate(rInt int) string {
return testAccAWSBatchComputeEnvironmentConfigBase(rInt) + fmt.Sprintf(`
resource "aws_launch_template" "foo" {
name = "tf_acc_test_%d"
}
resource "aws_batch_compute_environment" "ec2" {
compute_environment_name = "tf_acc_test_%d"
compute_resources {
instance_role = "${aws_iam_instance_profile.ecs_instance_role.arn}"
instance_type = [
"c4.large",
]
launch_template {
launch_template_name = "${aws_launch_template.foo.name}"
}
max_vcpus = 16
min_vcpus = 0
security_group_ids = [
"${aws_security_group.test_acc.id}"
]
spot_iam_fleet_role = "${aws_iam_role.aws_ec2_spot_fleet_role.arn}"
subnets = [
"${aws_subnet.test_acc.id}"
]
type = "SPOT"
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt, rInt)
}
9 changes: 9 additions & 0 deletions website/docs/r/batch_compute_environment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ resource "aws_batch_compute_environment" "sample" {
* `image_id` - (Optional) The Amazon Machine Image (AMI) ID used for instances launched in the compute environment.
* `instance_role` - (Required) The Amazon ECS instance role applied to Amazon EC2 instances in a compute environment.
* `instance_type` - (Required) A list of instance types that may be launched.
* `launch_template` - (Optional) The launch template to use for your compute resources. See details below.
* `max_vcpus` - (Required) The maximum number of EC2 vCPUs that an environment can reach.
* `min_vcpus` - (Required) The minimum number of EC2 vCPUs that an environment should maintain.
* `security_group_ids` - (Required) A list of EC2 security group that are associated with instances launched in the compute environment.
Expand All @@ -139,6 +140,14 @@ resource "aws_batch_compute_environment" "sample" {
* `tags` - (Optional) Key-value pair tags to be applied to resources that are launched in the compute environment.
* `type` - (Required) The type of compute environment. Valid items are `EC2` or `SPOT`.

### launch_template

`launch_template` supports the following:

* `launch_template_id` - (Optional) ID of the launch template. You must specify either the launch template ID or launch template name in the request, but not both.
* `launch_template_name` - (Optional) Name of the launch template.
* `version` - (Optional) The version number of the launch template. Default: The default version of the launch template.

## Attributes Reference

* `arn` - The Amazon Resource Name (ARN) of the compute environment.
Expand Down

0 comments on commit 6c42951

Please sign in to comment.