Resource: aws_lightsail_distribution

Terraform resource for managing an AWS Lightsail Distribution.

Example Usage

Basic Usage

Below is a basic example with a bucket as an origin.

resource "aws_lightsail_bucket" "test" {
  name      = "test-bucket"
  bundle_id = "small_1_0"
}
resource "aws_lightsail_distribution" "test" {
  name      = "test-distribution"
  bundle_id = "small_1_0"
  origin {
    name        = aws_lightsail_bucket.test.name
    region_name = aws_lightsail_bucket.test.region
  }
  default_cache_behavior {
    behavior = "cache"
  }
  cache_behavior_settings {
    allowed_http_methods = "GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE"
    cached_http_methods  = "GET,HEAD"
    default_ttl          = 86400
    maximum_ttl          = 31536000
    minimum_ttl          = 0
    forwarded_cookies {
      option = "none"
    }
    forwarded_headers {
      option = "default"
    }
    forwarded_query_strings {
      option = false
    }
  }
}

instance origin example

Below is an example of an instance as the origin.

data "aws_availability_zones" "available" {
  state = "available"

  filter {
    name   = "opt-in-status"
    values = ["opt-in-not-required"]
  }
}

resource "aws_lightsail_static_ip_attachment" "test" {
  static_ip_name = aws_lightsail_static_ip.test.name
  instance_name  = aws_lightsail_instance.test.name
}

resource "aws_lightsail_static_ip" "test" {
  name = "test-static-ip"
}

resource "aws_lightsail_instance" "test" {
  name              = "test-instance"
  availability_zone = data.aws_availability_zones.available.names[0]
  blueprint_id      = "amazon_linux_2"
  bundle_id         = "micro_1_0"
}

resource "aws_lightsail_distribution" "test" {
  name       = "test-distribution"
  depends_on = [aws_lightsail_static_ip_attachment.test]
  bundle_id  = "small_1_0"
  origin {
    name        = aws_lightsail_instance.test.name
    region_name = data.aws_availability_zones.available.id
  }
  default_cache_behavior {
    behavior = "cache"
  }
}

lb origin example

Below is an example with a load balancer as an origin

data "aws_availability_zones" "available" {
  state = "available"

  filter {
    name   = "opt-in-status"
    values = ["opt-in-not-required"]
  }
}

resource "aws_lightsail_lb" "test" {
  name              = "test-load-balancer"
  health_check_path = "/"
  instance_port     = "80"
  tags = {
    foo = "bar"
  }
}

resource "aws_lightsail_instance" "test" {
  name              = "test-instance"
  availability_zone = data.aws_availability_zones.available.names[0]
  blueprint_id      = "amazon_linux_2"
  bundle_id         = "nano_3_0"
}

resource "aws_lightsail_lb_attachment" "test" {
  lb_name       = aws_lightsail_lb.test.name
  instance_name = aws_lightsail_instance.test.name
}

resource "aws_lightsail_distribution" "test" {
  name       = "test-distribution"
  depends_on = [aws_lightsail_lb_attachment.test]
  bundle_id  = "small_1_0"
  origin {
    name        = aws_lightsail_lb.test.name
    region_name = data.aws_availability_zones.available.id
  }
  default_cache_behavior {
    behavior = "cache"
  }
}

Argument Reference

The following arguments are required:

The following arguments are optional:

default_cache_behavior

origin

cache_behavior_settings

forwarded_cookies

forwarded_headers

forwarded_query_strings

cache_behavior

Attribute Reference

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

location

Timeouts

Configuration options:

Import

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

import {
  to = aws_lightsail_distribution.example
  id = "rft-8012925589"
}

Using terraform import, import Lightsail Distribution using the id. For example:

% terraform import aws_lightsail_distribution.example rft-8012925589