Amazon web services Terraform:如何捕获aws_实例的公共IP?

Amazon web services Terraform:如何捕获aws_实例的公共IP?,amazon-web-services,terraform,Amazon Web Services,Terraform,Scenerio:我正在尝试捕获我在执行shell脚本中使用的aws_实例的公共IP 我有以下代码 资源“aws_实例”“k8控制平面”{ 计数=1 ami=“ami-xxxxxxxx” 实例_type=“t2.micro” iam_实例_profile=“blah user” 子网\u id=“blah-xxxxxx” key_name=“blah bot” vpc_security_group_id=[aws_security_group.blah sec grp.id] #在远程实例上执行

Scenerio:我正在尝试捕获我在执行shell脚本中使用的aws_实例的公共IP

我有以下代码

资源“aws_实例”“k8控制平面”{
计数=1
ami=“ami-xxxxxxxx”
实例_type=“t2.micro”
iam_实例_profile=“blah user”
子网\u id=“blah-xxxxxx”
key_name=“blah bot”
vpc_security_group_id=[aws_security_group.blah sec grp.id]
#在远程实例上执行
user\u data=~/my\u ip.txt
#跑库比德
kubeadm init--令牌“${local.token}”--令牌ttl 15m--apiserver证书额外SAN${self.public.ip}--pod网络cidr“192.168.0.0/16”
#准备kubeconfig文件以下载到本地计算机
kubectl--kubeconfig/home/centos/admin.conf配置集集群kubernetes--server https://${self.public.ip}:6443
EOF
}
问题:我不知道如何注入aws_实例的相应公共IP(如上面${self.public.IP}所示)。显然,这是错误的,因此我不确定如何做到这一点。

只能用于供应器,而不能用于
用户\u数据。但您可以使用:

基于评论的更新:应该是私有ip,而不是公共ip:

resource "aws_instance" "k8-control-plane" {
  count                  = 1
  ami                    = "ami-XXXXXXXXXXXXX"
  instance_type          = "t2.micro"
  iam_instance_profile   = "blah-user"
  subnet_id              = "blah-xxxxxx"
  key_name               = "blah-bot"
  vpc_security_group_ids = [aws_security_group.blah-sec-grp.id]

  #Execute on remote instance
  user_data = <<-EOF
  #!/bin/bash

  #DEBUG
   
  private_ip=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)

  echo $private_ip >> ~/my_ip.txt 

  # Run kubeadm
  kubeadm init --token "${local.token}" --token-ttl 15m --apiserver-cert-extra-sans $private_ip --pod-network-cidr "192.168.0.0/16"

  # Prepare kubeconfig file for download to local machine
  kubectl --kubeconfig /home/centos/admin.conf config set-cluster kubernetes --server https://$private_ip:6443
  EOF
}
资源“aws_实例”“k8控制平面”{
计数=1
ami=“ami-xxxxxxxx”
实例_type=“t2.micro”
iam_实例_profile=“blah user”
子网\u id=“blah-xxxxxx”
key_name=“blah bot”
vpc_security_group_id=[aws_security_group.blah sec grp.id]
#在远程实例上执行
user\u data=~/my\u ip.txt
#跑库比德
kubeadm init--令牌“${local.token}”--令牌ttl 15m--apiserver证书额外SAN$private_ip--pod网络cidr“192.168.0.0/16”
#准备kubeconfig文件以下载到本地计算机
kubectl--kubeconfig/home/centos/admin.conf配置集集群kubernetes--server https://$private_ip:6443
EOF
}

由于某些原因,这仍然不起作用。它没有回显任何内容,另一个命令在此处为空referenced@stackoverflow用户_数据以根用户身份执行,因此请签入根文件夹,不是ec2用户。@stackoverflow可以运行
curl ifconfig.me
来验证公共ip吗?@stackoverflow私有ip可以获得
curlhttp://169.254.169.254/latest/meta-data/local-ipv4
。这也是空的?@stackoverflow,很好。我会更新答案,然后使用私有ip。
resource "aws_instance" "k8-control-plane" {
  count                  = 1
  ami                    = "ami-XXXXXXXXXXXXX"
  instance_type          = "t2.micro"
  iam_instance_profile   = "blah-user"
  subnet_id              = "blah-xxxxxx"
  key_name               = "blah-bot"
  vpc_security_group_ids = [aws_security_group.blah-sec-grp.id]

  #Execute on remote instance
  user_data = <<-EOF
  #!/bin/bash

  #DEBUG
   
  private_ip=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)

  echo $private_ip >> ~/my_ip.txt 

  # Run kubeadm
  kubeadm init --token "${local.token}" --token-ttl 15m --apiserver-cert-extra-sans $private_ip --pod-network-cidr "192.168.0.0/16"

  # Prepare kubeconfig file for download to local machine
  kubectl --kubeconfig /home/centos/admin.conf config set-cluster kubernetes --server https://$private_ip:6443
  EOF
}