Skip to content

Commit

Permalink
Merge pull request #8247 from terraform-providers/f-aws_launch_templa…
Browse files Browse the repository at this point in the history
…te-ElasticInference

resource/aws_launch_template: Add elastic_inference_accelerator configuration block
  • Loading branch information
bflad authored Apr 9, 2019
2 parents 27c8a8b + e67d0a9 commit 97d35d6
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
64 changes: 64 additions & 0 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ func resourceAwsLaunchTemplate() *schema.Resource {
},
},

"elastic_inference_accelerator": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},

"iam_instance_profile": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -639,6 +653,10 @@ func resourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) err
return err
}

if err := d.Set("elastic_inference_accelerator", flattenEc2LaunchTemplateElasticInferenceAcceleratorResponse(ltData.ElasticInferenceAccelerators)); err != nil {
return fmt.Errorf("error setting elastic_inference_accelerator: %s", err)
}

if err := d.Set("iam_instance_profile", getIamInstanceProfile(ltData.IamInstanceProfile)); err != nil {
return err
}
Expand Down Expand Up @@ -810,6 +828,24 @@ func getElasticGpuSpecifications(e []*ec2.ElasticGpuSpecificationResponse) []int
return s
}

func flattenEc2LaunchTemplateElasticInferenceAcceleratorResponse(accelerators []*ec2.LaunchTemplateElasticInferenceAcceleratorResponse) []interface{} {
l := []interface{}{}

for _, accelerator := range accelerators {
if accelerator == nil {
continue
}

m := map[string]interface{}{
"type": aws.StringValue(accelerator.Type),
}

l = append(l, m)
}

return l
}

func getIamInstanceProfile(i *ec2.LaunchTemplateIamInstanceProfileSpecification) []interface{} {
s := []interface{}{}
if i != nil {
Expand Down Expand Up @@ -1056,6 +1092,10 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
opts.ElasticGpuSpecifications = elasticGpuSpecifications
}

if v, ok := d.GetOk("elastic_inference_accelerator"); ok {
opts.ElasticInferenceAccelerators = expandEc2LaunchTemplateElasticInferenceAccelerators(v.([]interface{}))
}

if v, ok := d.GetOk("iam_instance_profile"); ok {
iip := v.([]interface{})

Expand Down Expand Up @@ -1346,6 +1386,30 @@ func readElasticGpuSpecificationsFromConfig(egs map[string]interface{}) *ec2.Ela
return elasticGpuSpecification
}

func expandEc2LaunchTemplateElasticInferenceAccelerators(l []interface{}) []*ec2.LaunchTemplateElasticInferenceAccelerator {
if len(l) == 0 {
return nil
}

var accelerators []*ec2.LaunchTemplateElasticInferenceAccelerator

for _, lRaw := range l {
m, ok := lRaw.(map[string]interface{})

if !ok {
continue
}

accelerator := &ec2.LaunchTemplateElasticInferenceAccelerator{
Type: aws.String(m["type"].(string)),
}

accelerators = append(accelerators, accelerator)
}

return accelerators
}

func readInstanceMarketOptionsFromConfig(imo map[string]interface{}) (*ec2.LaunchTemplateInstanceMarketOptionsRequest, error) {
instanceMarketOptions := &ec2.LaunchTemplateInstanceMarketOptionsRequest{}
spotOptions := &ec2.LaunchTemplateSpotMarketOptionsRequest{}
Expand Down
48 changes: 48 additions & 0 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestAccAWSLaunchTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resName, "latest_version", "1"),
resource.TestCheckResourceAttrSet(resName, "arn"),
resource.TestCheckResourceAttr(resName, "ebs_optimized", ""),
resource.TestCheckResourceAttr(resName, "elastic_inference_accelerator.#", "0"),
),
},
},
Expand Down Expand Up @@ -225,6 +226,41 @@ func TestAccAWSLaunchTemplate_EbsOptimized(t *testing.T) {
})
}

func TestAccAWSLaunchTemplate_ElasticInferenceAccelerator(t *testing.T) {
var template1 ec2.LaunchTemplate
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_launch_template.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateConfigElasticInferenceAccelerator(rName, "eia1.medium"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &template1),
resource.TestCheckResourceAttr(resourceName, "elastic_inference_accelerator.#", "1"),
resource.TestCheckResourceAttr(resourceName, "elastic_inference_accelerator.0.type", "eia1.medium"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSLaunchTemplateConfigElasticInferenceAccelerator(rName, "eia1.large"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &template1),
resource.TestCheckResourceAttr(resourceName, "elastic_inference_accelerator.#", "1"),
resource.TestCheckResourceAttr(resourceName, "elastic_inference_accelerator.0.type", "eia1.large"),
),
},
},
})
}

func TestAccAWSLaunchTemplate_data(t *testing.T) {
var template ec2.LaunchTemplate
resName := "aws_launch_template.foo"
Expand Down Expand Up @@ -810,6 +846,18 @@ resource "aws_launch_template" "test" {
`, ebsOptimized, rName)
}

func testAccAWSLaunchTemplateConfigElasticInferenceAccelerator(rName, elasticInferenceAcceleratorType string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
name = %[1]q
elastic_inference_accelerator {
type = %[2]q
}
}
`, rName, elasticInferenceAcceleratorType)
}

func testAccAWSLaunchTemplateConfig_data(rInt int) string {
return fmt.Sprintf(`
resource "aws_launch_template" "foo" {
Expand Down
13 changes: 13 additions & 0 deletions website/docs/r/launch_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ resource "aws_launch_template" "foo" {
type = "test"
}
elastic_interence_accelerator {
type = "eia1.medium"
}
iam_instance_profile {
name = "test"
}
Expand Down Expand Up @@ -107,6 +111,7 @@ The following arguments are supported:
* `ebs_optimized` - If `true`, the launched EC2 instance will be EBS-optimized.
* `elastic_gpu_specifications` - The elastic GPU to attach to the instance. See [Elastic GPU](#elastic-gpu)
below for more details.
* `elastic_inference_accelerator` - (Optional) Configuration block containing an Elastic Inference Accelerator to attach to the instance. See [Elastic Inference Accelerator](#elastic-inference-accelerator) below for more details.
* `iam_instance_profile` - The IAM Instance Profile to launch the instance with. See [Instance Profile](#instance-profile)
below for more details.
* `image_id` - The AMI from which to launch the instance.
Expand Down Expand Up @@ -188,6 +193,14 @@ The `elastic_gpu_specifications` block supports the following:

* `type` - The [Elastic GPU Type](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/elastic-gpus.html#elastic-gpus-basics)

### Elastic Inference Accelerator

Attach an Elastic Inference Accelerator to the instance. Additional information about Elastic Inference in EC2 can be found in the [EC2 User Guide](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-inference.html).

The `elastic_inference_accelerator` configuration block supports the following:

* `type` - (Required) Accelerator type.

### Instance Profile

The [IAM Instance Profile](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html)
Expand Down

0 comments on commit 97d35d6

Please sign in to comment.