Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to output public_ip value when requesting spot instances #4313

Closed
m8051 opened this issue Apr 23, 2018 · 5 comments
Closed

Unable to output public_ip value when requesting spot instances #4313

m8051 opened this issue Apr 23, 2018 · 5 comments
Labels
bug Addresses a defect in current functionality. regression Pertains to a degraded workflow resulting from an upstream patch or internal enhancement. service/ec2 Issues and PRs that pertain to the ec2 service.

Comments

@m8051
Copy link

m8051 commented Apr 23, 2018

Terraform Version

  • Terraform v0.11.7
    • provider.aws v1.15.0

Affected Resource(s)

  • aws_spot_instance_request

Terraform Configuration Files

# ----------------------------------------#
#
#       | Terraform Main file |
#
# ----------------------------------------#
# File: main.rf
# Autor: Jordi <m8051>
# ----------------------------------------#

# Specify the provider and access details
provider "aws" {
  access_key = ""
  secret_key = ""
  region = "${var.aws_region}"
}


# Request an Aws Spot Instance
resource "aws_spot_instance_request" "staging" {

  key_name = "${var.aws_key_name}"  
  ami = "${var.aws_ami}"
  instance_type = "${var.aws_instance_type}"
  availability_zone = "${var.aws_zones}"

  spot_price = "${var.aws_spotprice}"
  wait_for_fulfillment = true
  instance_initiated_shutdown_behavior = "terminate"

  iam_instance_profile = "${var.aws_iam_role}"
  user_data = "${file("../userdata/userdata.sh")}"
  security_groups = ["${aws_security_group.office_terraform.name}"]

  tags {
    Name = "staging-spot-terraform"
  }
}
# ----------------------------------------#
#
#       | Terraform Outputs file |
#
# ----------------------------------------#
# File: outputs.rf
# Autor: Jordi <m8051>
# ----------------------------------------#
output "public_ip" {
  value = "${aws_spot_instance_request.staging.public_ip}"
}

output "instance_id" {
  value = "${aws_spot_instance_request.staging.spot_instance_id}"
}

Expected Behavior

terraform output

instance_id = i-09b4d95244329f3dd
public_ip = 34.242.214.106

Actual Behavior

terraform output

instance_id = i-09b4d95244329f3dd
public_ip =

Steps to Reproduce

terraform plan -out=staging.plan -var "aws_ami=ami-63fbd91a" -var "aws_instance_type=m3.medium" -var "aws_spot_price=0.0.169"
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.

aws_security_group.office_terraform: Refreshing state... (ID: sg-733ceb05)

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_spot_instance_request.staging
      id:                                   <computed>
      ami:                                  "ami-63fbd91a"
      associate_public_ip_address:          <computed>
      availability_zone:                    "eu-west-1a"
      ebs_block_device.#:                   <computed>
      ephemeral_block_device.#:             <computed>
      get_password_data:                    "false"
      iam_instance_profile:                 "staging"
      instance_initiated_shutdown_behavior: "terminate"
      instance_interruption_behaviour:      "terminate"
      instance_state:                       <computed>
      instance_type:                        "m3.medium"
      ipv6_address_count:                   <computed>
      ipv6_addresses.#:                     <computed>
      network_interface.#:                  <computed>
      network_interface_id:                 <computed>
      password_data:                        <computed>
      placement_group:                      <computed>
      primary_network_interface_id:         <computed>
      private_dns:                          <computed>
      private_ip:                           <computed>
      public_dns:                           <computed>
      public_ip:                            <computed>
      root_block_device.#:                  <computed>
      security_groups.#:                    "1"
      security_groups.969816526:            "terraform"
      source_dest_check:                    "true"
      spot_bid_status:                      <computed>
      spot_instance_id:                     <computed>
      spot_price:                           "0.0146"
      spot_request_state:                   <computed>
      spot_type:                            "persistent"
      subnet_id:                            <computed>
      tags.%:                               "1"
      tags.Name:                            "staging-spot-terraform"
      tenancy:                              <computed>
      user_data:                            "8e8548ca9a858cc8529c216367273dc54f78afef"
      vpc_security_group_ids.#:             <computed>
      wait_for_fulfillment:                 "true"


Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

This plan was saved to: staging.plan

To perform exactly these actions, run the following command to apply:
    terraform apply "staging.plan"

terraform apply "staging.plan"

terraform output

instance_id = i-09b4d95244329f3dd
public_ip =

References

Context

This used to work well until last Friday 20th of April when I noticed our RSpec tests were failing due to a non public_ip output when using baked Amis and Spots.

I know some attributes are expected to change over time and I already implemented a workaround but I wanted to give you a heads up so you guys are aware of this just in case there's something new on the AWS side ...

@m8051 m8051 changed the title Unable to output public_ip value when requesting spots instances Unable to output public_ip value when requesting spot instances Apr 23, 2018
@radeksimko radeksimko added bug Addresses a defect in current functionality. regression Pertains to a degraded workflow resulting from an upstream patch or internal enhancement. service/ec2 Issues and PRs that pertain to the ec2 service. labels Apr 24, 2018
@ikekim
Copy link

ikekim commented Oct 13, 2018

@m8051 what was your workaround? I'm having the same issue as you except I'm trying to get the output of the private_ip addresses from the aws_spot_instance_request.

@m8051
Copy link
Author

m8051 commented Oct 15, 2018

Hi @ikekim, I'm not sure my workaround will be of any help to you as I use a collection of Linux scripts to build a pipeline using packer + terraform + serverspec, but I'm happy to share it.

What I did was, output the instance_id through terraform and assign its value to a variable:

target=$(terraform output instance_id)

Then I used the AWS CLI to find out what the public_ip of that instance_id is by running:

target_ip=$(aws ec2 describe-instances  --instance-ids ${target} | grep PublicIpAddress | grep -o -P "\d+\.\d+\.\d+\.\d+" | grep -v '^10\.')

I suppose you could also use the local-exec provisioner.

Hope this helps ...

@robcxyz
Copy link

robcxyz commented Sep 9, 2019

If you want to do this within terraform you can do it like this.

module "instance_id" {
  source = "matti/resource/shell"
  command = format("aws ec2 wait spot-instance-request-fulfilled --spot-instance-request-ids %s && aws ec2 describe-spot-instance-requests --spot-instance-request-ids %s | jq -r '.SpotInstanceRequests[].InstanceId'", aws_spot_instance_request.this.*.id[0], aws_spot_instance_request.this.*.id[0])
}

and then output it referencing the module

output "instance_id" {
  value = module.instance_id.stdout
}

You can get more information out of the spot instance request with jq but instance id is the most important.

@nywilken
Copy link
Member

This is an old issue, but it is marked as a regression so I am working on trying to clear this up. Is anyone still running into this issue?

I'm unable to reproduce this issue with the latest provider for both Terraform 0.11 and Terraform 0.12. I'm going to close this issue as it doesn't appear to be reproducible. If anyone is experiencing this issue using the latest version(s) of the provider please open a new issue with the details requested in the Bug Template

@ghost
Copy link

ghost commented Jan 20, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@hashicorp hashicorp locked and limited conversation to collaborators Jan 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. regression Pertains to a degraded workflow resulting from an upstream patch or internal enhancement. service/ec2 Issues and PRs that pertain to the ec2 service.
Projects
None yet
Development

No branches or pull requests

5 participants