Resource: aws_launch_template

Provides an EC2 launch template resource. Can be used to create instances or auto scaling groups.

Example Usage

resource "aws_launch_template" "foo" {
  name = "foo"

  block_device_mappings {
    device_name = "/dev/sdf"

    ebs {
      volume_size = 20
    }
  }

  capacity_reservation_specification {
    capacity_reservation_preference = "open"
  }

  cpu_options {
    core_count       = 4
    threads_per_core = 2
  }

  credit_specification {
    cpu_credits = "standard"
  }

  disable_api_stop        = true
  disable_api_termination = true

  ebs_optimized = true

  elastic_gpu_specifications {
    type = "test"
  }

  elastic_inference_accelerator {
    type = "eia1.medium"
  }

  iam_instance_profile {
    name = "test"
  }

  image_id = "ami-test"

  instance_initiated_shutdown_behavior = "terminate"

  instance_market_options {
    market_type = "spot"
  }

  instance_type = "t2.micro"

  kernel_id = "test"

  key_name = "test"

  license_specification {
    license_configuration_arn = "arn:aws:license-manager:eu-west-1:123456789012:license-configuration:lic-0123456789abcdef0123456789abcdef"
  }

  metadata_options {
    http_endpoint               = "enabled"
    http_tokens                 = "required"
    http_put_response_hop_limit = 1
    instance_metadata_tags      = "enabled"
  }

  monitoring {
    enabled = true
  }

  network_interfaces {
    associate_public_ip_address = true
  }

  placement {
    availability_zone = "us-west-2a"
  }

  ram_disk_id = "test"

  vpc_security_group_ids = ["sg-12345678"]

  tag_specifications {
    resource_type = "instance"

    tags = {
      Name = "test"
    }
  }

  user_data = filebase64("${path.module}/example.sh")
}

Argument Reference

This resource supports the following arguments:

Block devices

Configure additional volumes of the instance besides specified by the AMI. It's a good idea to familiarize yourself with AWS's Block Device Mapping docs to understand the implications of using these attributes.

To find out more information for an existing AMI to override the configuration, such as device_name, you can use the AWS CLI ec2 describe-images command.

Each block_device_mappings supports the following:

The ebs block supports the following:

Capacity Reservation Specification

The capacity_reservation_specification block supports the following:

The capacity_reservation_target block supports the following:

CPU Options

The cpu_options block supports the following:

Both number of CPU cores and threads per core must be specified. Valid number of CPU cores and threads per core for the instance type can be found in the CPU Options Documentation

Credit Specification

Credit specification can be applied/modified to the EC2 Instance at any time.

The credit_specification block supports the following:

Elastic GPU

Attach an elastic GPU the instance.

The elastic_gpu_specifications block supports the following:

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.

The elastic_inference_accelerator configuration block supports the following:

Enclave Options

The enclave_options block supports the following:

For more information, see the documentation on Nitro Enclaves.

Hibernation Options

The hibernation_options block supports the following:

Instance Profile

The IAM Instance Profile to attach.

The iam_instance_profile block supports the following:

Instance Requirements

This configuration block supports the following:

    Valid names:
      * amazon-web-services
      * amd
      * nvidia
      * xilinx
    Valid names:
      * a100            - NVIDIA A100 GPUs
      * v100            - NVIDIA V100 GPUs
      * k80             - NVIDIA K80 GPUs
      * t4              - NVIDIA T4 GPUs
      * m60             - NVIDIA M60 GPUs
      * radeon-pro-v520 - AMD Radeon Pro V520 GPUs
      * vu9p            - Xilinx VU9P FPGAs
    Valid types:
      * fpga
      * gpu
      * inference
    Valid names:
      * amazon-web-services
      * amd
      * intel
    Valid names:
      * current  - Recommended for best performance.
      * previous - For existing applications optimized for older instance types.
    Value names:
      * hdd - hard disk drive
      * ssd - solid state drive

License Specification

Associate one of more license configurations.

The license_specification block supports the following:

Maintenance Options

The maintenance_options block supports the following:

Market Options

The market (purchasing) option for the instances.

The instance_market_options block supports the following:

The spot_options block supports the following:

Metadata Options

The metadata options for the instances.

The metadata_options block supports the following:

For more information, see the documentation on the Instance Metadata Service.

Monitoring

The monitoring block supports the following:

Network Interfaces

Attaches one or more Network Interfaces to the instance.

Check limitations for autoscaling group in Creating an Auto Scaling Group Using a Launch Template Guide

Each network_interfaces block supports the following:

Placement

The Placement Group of the instance.

The placement block supports the following:

Private DNS Name Options

The private_dns_name_options block supports the following:

Tag Specifications

The tags to apply to the resources during launch. You can tag instances, volumes, elastic GPUs and spot instance requests. More information can be found in the EC2 API documentation.

Each tag_specifications block supports the following:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import Launch Templates using the id. For example:

import {
  to = aws_launch_template.web
  id = "lt-12345678"
}

Using terraform import, import Launch Templates using the id. For example:

% terraform import aws_launch_template.web lt-12345678