[英]Terraform aws instance user_data not running
trying to use user_data when running an EC2 instance with Terraform.在使用 Terraform 运行 EC2 实例时尝试使用 user_data。
resource "aws_instance" "ec2nginx1" {
ami = nonsensitive(data.aws_ssm_parameter.ami.value)
instance_type = var.instance_type
subnet_id = aws_subnet.prisubnet1.id
vpc_security_group_ids = [aws_security_group.nginx_ec2sg.id]
depends_on = [null_resource.building_docker_image]
user_data = <<EOF
#!/bin/bash
sudo yum install -y openssh-server
sudo yum install -y aws
sudo yum install -y docker
sudo yum install -y curl
sudo systemctl start docker
sudo aws configure set aws_access_key_id ${var.ACCESS_KEY}
sudo aws configure set aws_secret_access_key ${var.SECRET_KEY}
sudo aws ecr get-login-password --region us-east-1 | sudo docker login --username AWS --password-stdin ${aws_ecr_repository.myrepo.registry_id}.dkr.ecr.us-east-q.amazonaws.com
sudo docker pull ${aws_ecr_repository.myrepo.repository_url}:latest
sudo docker container run --network host ${aws_ecr_repository.myrepo.repository_url}:latest
EOF
}
Debugging found:调试发现:
- .user_data: planned value cty.StringVal("858c462aa8a14896b64953424d1d0c5e2f79e122") does not match config value cty.StringVal(" #! /bin/bash\nsudo yum install -y openssh-server\nsudo yum install -y aws\nsudo yum install -y docker\nsudo yum install -y curl\nsudo systemctl start docker\nsudo aws configure set aws_access_key_id ${var.ACCESS_KEY}\nsudo aws configure set aws_secret_access_key ${var.SECRET_KEY}\nsudo aws configure set default.region us-east-1\nsudo aws ecr get-login-password --region us-east-1 | sudo docker login --username AWS --password-stdin ${aws_ecr_repository.myrepo.registry_id}.dkr.ecr.us-east-1.amazonaws.com\nsudo docker pull ${aws_ecr_repository.myrepo.repository_url}:latest \n\nsudo docker container run --network host ${aws_ecr_repository.myrepo.repository_url}:latest\n")
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
- .user_data_replace_on_change: planned value cty.False for a non-computed attribute
I've been searching around and couldn't find any actual solution for that issue.我一直在四处寻找,找不到该问题的任何实际解决方案。 tried with cloud-init, base64 encoding, running as a script with: (file{./"filename"}).
尝试使用 cloud-init,base64 编码,作为脚本运行:(file{./"filename"})。
Still doesn't run with same output.仍然没有以相同的输出运行。
Please help :)请帮忙 :)
If your instances are in private subnet, by definition they have no internet access.如果您的实例位于私有子网中,则根据定义,它们无法访问 Internet。 You have to use NAT gateway to install software or build custom AIM with all software pre installed.
您必须使用 NAT 网关来安装软件或构建自定义 AIM,并预先安装所有软件。 Then you just create your instance from your custom Ami.
然后,您只需从您的自定义 Ami 创建您的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.