» 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/sda1"

    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_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
  }

  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

The following arguments are supported:

» 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:

  • core_count - The number of CPU cores for the instance.
  • threads_per_core - The number of threads per CPU core. To disable Intel Hyper-Threading Technology for the instance, specify a value of 1. Otherwise, specify the default value of 2.

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:

  • cpu_credits - The credit option for CPU usage. Can be "standard" or "unlimited". T3 instances are launched as unlimited by default. T2 instances are launched as standard by default.

» 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:

  • type - (Required) Accelerator type.

» Instance Profile

The IAM Instance Profile to attach.

The iam_instance_profile block supports the following:

  • arn - The Amazon Resource Name (ARN) of the instance profile.
  • name - The name of the instance profile.

» License Specification

Associate one of more license configurations.

The license_specification 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:

  • block_duration_minutes - The required duration in minutes. This value must be a multiple of 60.
  • instance_interruption_behavior - The behavior when a Spot Instance is interrupted. Can be hibernate, stop, or terminate. (Default: terminate).
  • max_price - The maximum hourly price you're willing to pay for the Spot Instances.
  • spot_instance_type - The Spot Instance request type. Can be one-time, or persistent.
  • valid_until - The end date of the request.

» Metadata Options

The metadata options for the instances.

The metadata_options block supports the following:

  • http_endpoint - (Optional) Whether the metadata service is available. Can be "enabled" or "disabled". (Default: "enabled").
  • http_tokens - (Optional) Whether or not the metadata service requires session tokens, also referred to as Instance Metadata Service Version 2. Can be "optional" or "required". (Default: "optional").
  • http_put_response_hop_limit - (Optional) The desired HTTP PUT response hop limit for instance metadata requests. The larger the number, the further instance metadata requests can travel. Can be an integer from 1 to 64. (Default: 1).

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

» Monitoring

The monitoring block supports the following:

  • enabled - If true, the launched EC2 instance will have detailed monitoring enabled.

» 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:

  • associate_public_ip_address - Associate a public ip address with the network interface. Boolean value.
  • delete_on_termination - Whether the network interface should be destroyed on instance termination.
  • description - Description of the network interface.
  • device_index - The integer index of the network interface attachment.
  • ipv6_addresses - One or more specific IPv6 addresses from the IPv6 CIDR block range of your subnet. Conflicts with ipv6_address_count
  • ipv6_address_count - The number of IPv6 addresses to assign to a network interface. Conflicts with ipv6_addresses
  • network_interface_id - The ID of the network interface to attach.
  • private_ip_address - The primary private IPv4 address.
  • ipv4_address_count - The number of secondary private IPv4 addresses to assign to a network interface. Conflicts with ipv4_addresses
  • ipv4_addresses - One or more private IPv4 addresses to associate. Conflicts with ipv4_address_count
  • security_groups - A list of security group IDs to associate.
  • subnet_id - The VPC Subnet ID to associate.

» Placement

The Placement Group of the instance.

The placement block supports the following:

  • affinity - The affinity setting for an instance on a Dedicated Host.
  • availability_zone - The Availability Zone for the instance.
  • group_name - The name of the placement group for the instance.
  • host_id - The ID of the Dedicated Host for the instance.
  • spread_domain - Reserved for future use.
  • tenancy - The tenancy of the instance (if the instance is running in a VPC). Can be default, dedicated, or host.

» Hibernation Options

The hibernation_options block supports the following:

  • configured - If set to true, the launched EC2 instance will hibernation enabled.

» Tag Specifications

The tags to apply to the resources during launch. You can tag instances and volumes. More information can be found in the EC2 API documentation.

Each tag_specifications block supports the following:

  • resource_type - The type of resource to tag. Valid values are instance and volume.
  • tags - A mapping of tags to assign to the resource.

» Attributes Reference

The following attributes are exported along with all argument references:

  • arn - Amazon Resource Name (ARN) of the launch template.
  • id - The ID of the launch template.
  • default_version - The default version of the launch template.
  • latest_version - The latest version of the launch template.

» Import

Launch Templates can be imported using the id, e.g.

$ terraform import aws_launch_template.web lt-12345678