Skip to content

Instantly share code, notes, and snippets.

@maskinoshita
Created October 27, 2016 04:17
Show Gist options
  • Save maskinoshita/a7c439bfe9f2c519d45cc46946d9f19f to your computer and use it in GitHub Desktop.
Save maskinoshita/a7c439bfe9f2c519d45cc46946d9f19f to your computer and use it in GitHub Desktop.
Reproduction codes: terraform - aws_spot_instance_request with associate_public_ip_address=true never assign public ip. #9575
provider "aws" {
region = "${var.region}"
}
resource "aws_vpc" "test_vpc" {
cidr_block = "192.168.10.0/24"
enable_dns_support = true
enable_dns_hostnames = true
}
resource "aws_subnet" "test_subnet" {
vpc_id = "${aws_vpc.test_vpc.id}"
availability_zone = "${var.az}"
cidr_block = "192.168.10.128/25"
// change true/false
map_public_ip_on_launch = false
}
resource "aws_security_group" "test_access_policy" {
name = "test_access_policy"
description = "Allow ssh:22 from all"
vpc_id = "${aws_vpc.test_vpc.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_spot_instance_request" "test_spot" {
ami = "${lookup(var.ami, var.region)}"
instance_type = "m3.medium"
key_name = "${var.key-pair}"
subnet_id = "${aws_subnet.test_subnet.id}"
vpc_security_group_ids = [
"${aws_security_group.test_access_policy.id}"
]
spot_price = "0.5"
wait_for_fulfillment = true
// following code doesn't work when set map_public_ip_on_launch = false
associate_public_ip_address = true
}
output "spot_pub_ip" {
value = "${aws_spot_instance_request.test_spot.public_ip}"
}
output "spot_pub_dns" {
value = "${aws_spot_instance_request.test_spot.public_dns}"
}
resource "aws_instance" "test_ondemand" {
ami = "${lookup(var.ami, var.region)}"
instance_type = "m3.medium"
key_name = "${var.key-pair}"
subnet_id = "${aws_subnet.test_subnet.id}"
vpc_security_group_ids = [
"${aws_security_group.test_access_policy.id}"
]
// following code always works
associate_public_ip_address = true
}
output "ondemand_pub_ip" {
value = "${aws_instance.test_ondemand.public_ip}"
}
output "ondemand_pub_dns" {
value = "${aws_instance.test_ondemand.public_dns}"
}

Summary

  • subnet's map_public_ip_on_launch = true
    • both ondemand and spot instances are assigned public ips
  • subnet's map_public_ip_on_launch = false
    • ondemand instance is assigned a public ip, but spot instance is not assigned a public ip.

aws_subnet: map_public_ip_on_launch = true

terraform apply outputs:

aws_vpc.test_vpc: Creating...
  cidr_block:                "" => "192.168.10.0/24"
  default_network_acl_id:    "" => "<computed>"
  default_security_group_id: "" => "<computed>"
  dhcp_options_id:           "" => "<computed>"
  enable_classiclink:        "" => "<computed>"
  enable_dns_hostnames:      "" => "true"
  enable_dns_support:        "" => "true"
  instance_tenancy:          "" => "<computed>"
  main_route_table_id:       "" => "<computed>"
aws_vpc.test_vpc: Creation complete
aws_subnet.test_subnet: Creating...
  availability_zone:       "" => "ap-northeast-1c"
  cidr_block:              "" => "192.168.10.128/25"
  map_public_ip_on_launch: "" => "true"
  vpc_id:                  "" => "vpc-d41ce6b0"
aws_security_group.test_access_policy: Creating...
  description:                          "" => "Allow ssh:22 from all"
  egress.#:                             "" => "1"
  egress.482069346.cidr_blocks.#:       "" => "1"
  egress.482069346.cidr_blocks.0:       "" => "0.0.0.0/0"
  egress.482069346.from_port:           "" => "0"
  egress.482069346.prefix_list_ids.#:   "" => "0"
  egress.482069346.protocol:            "" => "-1"
  egress.482069346.security_groups.#:   "" => "0"
  egress.482069346.self:                "" => "false"
  egress.482069346.to_port:             "" => "0"
  ingress.#:                            "" => "1"
  ingress.2541437006.cidr_blocks.#:     "" => "1"
  ingress.2541437006.cidr_blocks.0:     "" => "0.0.0.0/0"
  ingress.2541437006.from_port:         "" => "22"
  ingress.2541437006.protocol:          "" => "tcp"
  ingress.2541437006.security_groups.#: "" => "0"
  ingress.2541437006.self:              "" => "false"
  ingress.2541437006.to_port:           "" => "22"
  name:                                 "" => "test_access_policy"
  owner_id:                             "" => "<computed>"
  vpc_id:                               "" => "vpc-d41ce6b0"
aws_subnet.test_subnet: Creation complete
aws_security_group.test_access_policy: Creation complete
aws_spot_instance_request.test_spot: Creating...
  ami:                               "" => "ami-3ec2743e"
  associate_public_ip_address:       "" => "true"
  availability_zone:                 "" => "<computed>"
  ebs_block_device.#:                "" => "<computed>"
  ephemeral_block_device.#:          "" => "<computed>"
  instance_state:                    "" => "<computed>"
  instance_type:                     "" => "m3.medium"
  key_name:                          "" => "test-key"
  network_interface_id:              "" => "<computed>"
  placement_group:                   "" => "<computed>"
  private_dns:                       "" => "<computed>"
  private_ip:                        "" => "<computed>"
  public_dns:                        "" => "<computed>"
  public_ip:                         "" => "<computed>"
  root_block_device.#:               "" => "<computed>"
  security_groups.#:                 "" => "<computed>"
  source_dest_check:                 "" => "true"
  spot_bid_status:                   "" => "<computed>"
  spot_instance_id:                  "" => "<computed>"
  spot_price:                        "" => "0.5"
  spot_request_state:                "" => "<computed>"
  spot_type:                         "" => "persistent"
  subnet_id:                         "" => "subnet-154b874d"
  tenancy:                           "" => "<computed>"
  vpc_security_group_ids.#:          "" => "1"
  vpc_security_group_ids.3754176122: "" => "sg-96857ff1"
  wait_for_fulfillment:              "" => "true"
aws_instance.test_ondemand: Creating...
  ami:                               "" => "ami-3ec2743e"
  associate_public_ip_address:       "" => "true"
  availability_zone:                 "" => "<computed>"
  ebs_block_device.#:                "" => "<computed>"
  ephemeral_block_device.#:          "" => "<computed>"
  instance_state:                    "" => "<computed>"
  instance_type:                     "" => "m3.medium"
  key_name:                          "" => "test-key"
  network_interface_id:              "" => "<computed>"
  placement_group:                   "" => "<computed>"
  private_dns:                       "" => "<computed>"
  private_ip:                        "" => "<computed>"
  public_dns:                        "" => "<computed>"
  public_ip:                         "" => "<computed>"
  root_block_device.#:               "" => "<computed>"
  security_groups.#:                 "" => "<computed>"
  source_dest_check:                 "" => "true"
  subnet_id:                         "" => "subnet-154b874d"
  tenancy:                           "" => "<computed>"
  vpc_security_group_ids.#:          "" => "1"
  vpc_security_group_ids.3754176122: "" => "sg-96857ff1"
aws_spot_instance_request.test_spot: Still creating... (10s elapsed)
aws_instance.test_ondemand: Still creating... (10s elapsed)
aws_instance.test_ondemand: Creation complete
aws_spot_instance_request.test_spot: Still creating... (20s elapsed)
aws_spot_instance_request.test_spot: Creation complete

Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

Outputs:

ondemand_pub_dns = ec2-54-199-217-99.ap-northeast-1.compute.amazonaws.com
ondemand_pub_ip = 54.199.217.99
spot_pub_dns = ec2-54-238-252-194.ap-northeast-1.compute.amazonaws.com
spot_pub_ip = 54.238.252.194

aws_subnet: map_public_ip_on_launch = false

terraform apply outputs:

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be persisted to local or remote state storage.


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed. Cyan entries are data sources to be read.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ aws_instance.test_ondemand
    ami:                         "ami-3ec2743e"
    associate_public_ip_address: "true"
    availability_zone:           "<computed>"
    ebs_block_device.#:          "<computed>"
    ephemeral_block_device.#:    "<computed>"
    instance_state:              "<computed>"
    instance_type:               "m3.medium"
    key_name:                    "test-key"
    network_interface_id:        "<computed>"
    placement_group:             "<computed>"
    private_dns:                 "<computed>"
    private_ip:                  "<computed>"
    public_dns:                  "<computed>"
    public_ip:                   "<computed>"
    root_block_device.#:         "<computed>"
    security_groups.#:           "<computed>"
    source_dest_check:           "true"
    subnet_id:                   "${aws_subnet.test_subnet.id}"
    tenancy:                     "<computed>"
    vpc_security_group_ids.#:    "<computed>"

+ aws_security_group.test_access_policy
    description:                          "Allow ssh:22 from all"
    egress.#:                             "1"
    egress.482069346.cidr_blocks.#:       "1"
    egress.482069346.cidr_blocks.0:       "0.0.0.0/0"
    egress.482069346.from_port:           "0"
    egress.482069346.prefix_list_ids.#:   "0"
    egress.482069346.protocol:            "-1"
    egress.482069346.security_groups.#:   "0"
    egress.482069346.self:                "false"
    egress.482069346.to_port:             "0"
    ingress.#:                            "1"
    ingress.2541437006.cidr_blocks.#:     "1"
    ingress.2541437006.cidr_blocks.0:     "0.0.0.0/0"
    ingress.2541437006.from_port:         "22"
    ingress.2541437006.protocol:          "tcp"
    ingress.2541437006.security_groups.#: "0"
    ingress.2541437006.self:              "false"
    ingress.2541437006.to_port:           "22"
    name:                                 "test_access_policy"
    owner_id:                             "<computed>"
    vpc_id:                               "${aws_vpc.test_vpc.id}"

+ aws_spot_instance_request.test_spot
    ami:                         "ami-3ec2743e"
    associate_public_ip_address: "true"
    availability_zone:           "<computed>"
    ebs_block_device.#:          "<computed>"
    ephemeral_block_device.#:    "<computed>"
    instance_state:              "<computed>"
    instance_type:               "m3.medium"
    key_name:                    "test-key"
    network_interface_id:        "<computed>"
    placement_group:             "<computed>"
    private_dns:                 "<computed>"
    private_ip:                  "<computed>"
    public_dns:                  "<computed>"
    public_ip:                   "<computed>"
    root_block_device.#:         "<computed>"
    security_groups.#:           "<computed>"
    source_dest_check:           "true"
    spot_bid_status:             "<computed>"
    spot_instance_id:            "<computed>"
    spot_price:                  "0.5"
    spot_request_state:          "<computed>"
    spot_type:                   "persistent"
    subnet_id:                   "${aws_subnet.test_subnet.id}"
    tenancy:                     "<computed>"
    vpc_security_group_ids.#:    "<computed>"
    wait_for_fulfillment:        "true"

+ aws_subnet.test_subnet
    availability_zone:       "ap-northeast-1c"
    cidr_block:              "192.168.10.128/25"
    map_public_ip_on_launch: "false"
    vpc_id:                  "${aws_vpc.test_vpc.id}"

+ aws_vpc.test_vpc
    cidr_block:                "192.168.10.0/24"
    default_network_acl_id:    "<computed>"
    default_security_group_id: "<computed>"
    dhcp_options_id:           "<computed>"
    enable_classiclink:        "<computed>"
    enable_dns_hostnames:      "true"
    enable_dns_support:        "true"
    instance_tenancy:          "<computed>"
    main_route_table_id:       "<computed>"


Plan: 5 to add, 0 to change, 0 to destroy.
ubuntu@ubuntu-VirtualBox:~/study/github$ terraform apply
aws_vpc.test_vpc: Creating...
  cidr_block:                "" => "192.168.10.0/24"
  default_network_acl_id:    "" => "<computed>"
  default_security_group_id: "" => "<computed>"
  dhcp_options_id:           "" => "<computed>"
  enable_classiclink:        "" => "<computed>"
  enable_dns_hostnames:      "" => "true"
  enable_dns_support:        "" => "true"
  instance_tenancy:          "" => "<computed>"
  main_route_table_id:       "" => "<computed>"
aws_vpc.test_vpc: Creation complete
aws_subnet.test_subnet: Creating...
  availability_zone:       "" => "ap-northeast-1c"
  cidr_block:              "" => "192.168.10.128/25"
  map_public_ip_on_launch: "" => "false"
  vpc_id:                  "" => "vpc-401de724"
aws_security_group.test_access_policy: Creating...
  description:                          "" => "Allow ssh:22 from all"
  egress.#:                             "" => "1"
  egress.482069346.cidr_blocks.#:       "" => "1"
  egress.482069346.cidr_blocks.0:       "" => "0.0.0.0/0"
  egress.482069346.from_port:           "" => "0"
  egress.482069346.prefix_list_ids.#:   "" => "0"
  egress.482069346.protocol:            "" => "-1"
  egress.482069346.security_groups.#:   "" => "0"
  egress.482069346.self:                "" => "false"
  egress.482069346.to_port:             "" => "0"
  ingress.#:                            "" => "1"
  ingress.2541437006.cidr_blocks.#:     "" => "1"
  ingress.2541437006.cidr_blocks.0:     "" => "0.0.0.0/0"
  ingress.2541437006.from_port:         "" => "22"
  ingress.2541437006.protocol:          "" => "tcp"
  ingress.2541437006.security_groups.#: "" => "0"
  ingress.2541437006.self:              "" => "false"
  ingress.2541437006.to_port:           "" => "22"
  name:                                 "" => "test_access_policy"
  owner_id:                             "" => "<computed>"
  vpc_id:                               "" => "vpc-401de724"
aws_subnet.test_subnet: Creation complete
aws_security_group.test_access_policy: Creation complete
aws_instance.test_ondemand: Creating...
  ami:                              "" => "ami-3ec2743e"
  associate_public_ip_address:      "" => "true"
  availability_zone:                "" => "<computed>"
  ebs_block_device.#:               "" => "<computed>"
  ephemeral_block_device.#:         "" => "<computed>"
  instance_state:                   "" => "<computed>"
  instance_type:                    "" => "m3.medium"
  key_name:                         "" => "test-key"
  network_interface_id:             "" => "<computed>"
  placement_group:                  "" => "<computed>"
  private_dns:                      "" => "<computed>"
  private_ip:                       "" => "<computed>"
  public_dns:                       "" => "<computed>"
  public_ip:                        "" => "<computed>"
  root_block_device.#:              "" => "<computed>"
  security_groups.#:                "" => "<computed>"
  source_dest_check:                "" => "true"
  subnet_id:                        "" => "subnet-924b87ca"
  tenancy:                          "" => "<computed>"
  vpc_security_group_ids.#:         "" => "1"
  vpc_security_group_ids.953702864: "" => "sg-69fb010e"
aws_spot_instance_request.test_spot: Creating...
  ami:                              "" => "ami-3ec2743e"
  associate_public_ip_address:      "" => "true"
  availability_zone:                "" => "<computed>"
  ebs_block_device.#:               "" => "<computed>"
  ephemeral_block_device.#:         "" => "<computed>"
  instance_state:                   "" => "<computed>"
  instance_type:                    "" => "m3.medium"
  key_name:                         "" => "test-key"
  network_interface_id:             "" => "<computed>"
  placement_group:                  "" => "<computed>"
  private_dns:                      "" => "<computed>"
  private_ip:                       "" => "<computed>"
  public_dns:                       "" => "<computed>"
  public_ip:                        "" => "<computed>"
  root_block_device.#:              "" => "<computed>"
  security_groups.#:                "" => "<computed>"
  source_dest_check:                "" => "true"
  spot_bid_status:                  "" => "<computed>"
  spot_instance_id:                 "" => "<computed>"
  spot_price:                       "" => "0.5"
  spot_request_state:               "" => "<computed>"
  spot_type:                        "" => "persistent"
  subnet_id:                        "" => "subnet-924b87ca"
  tenancy:                          "" => "<computed>"
  vpc_security_group_ids.#:         "" => "1"
  vpc_security_group_ids.953702864: "" => "sg-69fb010e"
  wait_for_fulfillment:             "" => "true"
aws_spot_instance_request.test_spot: Still creating... (10s elapsed)
aws_instance.test_ondemand: Still creating... (10s elapsed)
aws_instance.test_ondemand: Creation complete
aws_spot_instance_request.test_spot: Still creating... (20s elapsed)
aws_spot_instance_request.test_spot: Creation complete

Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

Outputs:

ondemand_pub_dns = ec2-54-238-132-93.ap-northeast-1.compute.amazonaws.com
ondemand_pub_ip = 54.238.132.93
spot_pub_dns =
spot_pub_ip =
// set your key-pair name
variable "key-pair" {
default = "test-key"
}
variable "region" {
default = "ap-northeast-1"
}
variable "az" {
default = "ap-northeast-1c"
}
// ubuntu 14.10 ebs
variable "ami" {
type = "map"
default = {
ap-northeast-1 = "ami-3ec2743e"
ap-southeast-1 = "ami-94e3e1c6"
ap-southeast-2 = "ami-2beea811"
eu-central-1 = "ami-9633368b"
eu-west-1 = "ami-daa5eead"
sa-east-1 = "ami-19199604"
us-east-1 = "ami-cf6cb0a4"
us-west-1 = "ami-65887521"
us-west-2 = "ami-3f353a0f"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment