From e45375327b8b402543a3c22140df7682f1963c42 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Thu, 21 Mar 2019 01:33:20 +0900 Subject: [PATCH] resource/batch_compute_environment: Support LaunchTemplate --- aws/resource_aws_batch_compute_environment.go | 46 +++++++++++++++ ...urce_aws_batch_compute_environment_test.go | 58 +++++++++++++++++++ .../r/batch_compute_environment.html.markdown | 9 +++ 3 files changed, 113 insertions(+) diff --git a/aws/resource_aws_batch_compute_environment.go b/aws/resource_aws_batch_compute_environment.go index 3eb7b6521a59..20e63e7d659b 100644 --- a/aws/resource_aws_batch_compute_environment.go +++ b/aws/resource_aws_batch_compute_environment.go @@ -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, @@ -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) @@ -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 } diff --git a/aws/resource_aws_batch_compute_environment_test.go b/aws/resource_aws_batch_compute_environment_test.go index bf99739f70a4..c839f9500be2 100644 --- a/aws/resource_aws_batch_compute_environment_test.go +++ b/aws/resource_aws_batch_compute_environment_test.go @@ -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() @@ -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) +} diff --git a/website/docs/r/batch_compute_environment.html.markdown b/website/docs/r/batch_compute_environment.html.markdown index 48de9e95fccf..a6465f5c28f7 100644 --- a/website/docs/r/batch_compute_environment.html.markdown +++ b/website/docs/r/batch_compute_environment.html.markdown @@ -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. @@ -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.