I am trying to get the public_dns
from an aws_resource
in a template file
. I am getting the following error:
Error: Cycle: data.template_file.jupyter, aws_instance.jupyter
I am guessing that this is because the template file is created before the public_dns is made after a template file is rendered?
Can anyone think of a way to get the public_dns from the aws_resource into my user_data/template script?
Here’s main.tf
provider "aws" {
region = "us-west-2"
}
data "aws_ami" "al2" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
owners = ["amazon"]
}
resource "tls_private_key" "key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = "key-${uuid()}"
public_key = "${tls_private_key.key.public_key_openssh}"
}
resource "local_file" "pem" {
filename = "${aws_key_pair.generated_key.key_name}.pem"
content = "${tls_private_key.key.private_key_pem}"
file_permission = "400"
}
resource "aws_security_group" "jupyter" {
name = "${var.service}-${uuid()}"
description = "Security group for ${title(var.service)}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "SSH"
}
ingress {
from_port = 8888
to_port = 8898
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Jupyter Notebook"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Contact = "${var.contact}"
Environment = "${title(var.environment)}"
Name = "${var.service}-${uuid()}"
Service = "${title(var.service)}"
Terraform = "true"
}
}
resource "aws_instance" "jupyter" {
ami = "${data.aws_ami.al2.id}"
instance_type = "${var.instance_type}"
key_name = "${aws_key_pair.generated_key.key_name}"
vpc_security_group_ids = ["${aws_security_group.jupyter.id}"]
user_data = "${data.template_file.jupyter.rendered}"
disable_api_termination = false
tags = {
Name = "${title(var.service)}-${timestamp()}"
Service = "${title(var.service)}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
volume_tags = {
Name = "${title(var.service)}-${timestamp()}_ROOT"
Service = "${title(var.service)}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
}
resource "aws_ebs_volume" "jupyter" {
availability_zone = "${var.availability_zone}"
size = 20
type = "gp2"
tags = {
Name = "${title(var.service)}-${timestamp()}_Anaconda3"
Service = "${var.service}"
Contact = "${var.contact}"
Environment = "${title(lower(var.environment))}"
Terraform = "true"
}
}
resource "aws_volume_attachment" "jupyter" {
device_name = "/dev/sdb"
instance_id = "${aws_instance.jupyter.id}"
volume_id = "${aws_ebs_volume.jupyter.id}"
force_detach = true
}
data "template_file" "jupyter" {
template = "${file("script.tpl")}"
vars = {
publicdns = "${aws_instance.jupyter.public_dns}"
}
}
terraform {
backend "local" {
}
}
Here’s my script/template/user_data:
#!/bin/bash
sleep 2m
# Log stdout to file
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>/home/ec2-user/terraform-log.out 2>&1
# Update AL2
sudo yum update -y
# Mount /anaconda
sudo mkfs.xfs /dev/sdb -f
sudo mkdir /anaconda3
sudo mount /dev/sdb /anaconda3
sudo chown -R ec2-user:ec2-user /anaconda3
sudo echo "UUID=$(lsblk -nr -o UUID,MOUNTPOINT | grep "/anaconda3" | cut -d ' ' -f 1) /anaconda3 xfs defaults,nofail 1 2" >> /etc/fstab
# Install Anaconda
wget https://repo.anaconda.com/archive/Anaconda3-2018.12-Linux-x86_64.sh -O /home/ec2-user/anaconda.sh &&
bash /home/ec2-user/anaconda.sh -u -b -p /anaconda3 &&
echo 'export PATH="/anaconda3/bin:$PATH"' >> /home/ec2-user/.bashrc &&
source /home/ec2-user/.bashrc &&
rm -rf /home/ec2-user/anaconda.sh &&
# Configure Jupyter for AWS HTTP
sudo su ec2-user &&
jupyter notebook --generate-config &&
sed -i -e "s/#c.NotebookApp.ip = 'localhost'/c.NotebookApp.ip = '${publicdns}'/g" /home/ec2-user/.jupyter/jupyter_notebook_config.py &&
sed -i -e "s/#c.NotebookApp.allow_origin = ''/c.NotebookApp.allow_origin = '*'/g" /home/ec2-user/.jupyter/jupyter_notebook_config.py &&
sed -i -e "s/#c.NotebookApp.open_browser = True/c.NotebookApp.open_browser = False/g" /home/ec2-user/.jupyter/jupyter_notebook_config.py