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

  credit_specification {
    cpu_credits = "standard"
  }

  disable_api_termination = true

  ebs_optimized = true

  elastic_gpu_specifications {
    type = "test"
  }

  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"

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

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

  • delete_on_termination - Whether the volume should be destroyed on instance termination (Default: true).
  • encrypted - Enables EBS encryption on the volume (Default: false). Cannot be used with snapshot_id.
  • iops - The amount of provisioned IOPS. This must be set with a volume_type of "io1".
  • kms_key_id - AWS Key Management Service (AWS KMS) customer master key (CMK) to use when creating the encrypted volume. encrypted must be set to true when this is set.
  • snapshot_id - The Snapshot ID to mount.
  • volume_size - The size of the volume in gigabytes.
  • volume_type - The type of volume. Can be "standard", "gp2", or "io1". (Default: "standard").

» 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". (Default: "standard").

» Elastic GPU

Attach an elastic GPU the instance.

The elastic_gpu_specifications block supports the following:

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

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

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

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

» Tags

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:

  • 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