gpt4 book ai didi

amazon-web-services - user_data 未在创建实例时执行

转载 作者:行者123 更新时间:2023-12-04 18:25:58 24 4
gpt4 key购买 nike

我在我的 Terraform 脚本中提供了一个 .sh 文件作为 user_data:

resource "aws_instance" "alpha-terraform"{
ami=var.amis[var.region]
instance_type="t2.micro"
vpc_security_group_ids=[aws_security_group.alpha-terraform-sg.id]
user_data = file("user_data_bootstrap.sh")
key_name = "alpha-terraform"

tags={
type="alpha"
purpose="terraform"
}
}

.sh 文件包含:

#Jenkins Installation:
sudo apt update
sudo apt install openjdk-8-jdk --yes
wget –q –O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add –
sudo sh -c 'echo deb https://pkg.jenkins.io/debian binary/ >> /etc/apt/sources.list'
sudo apt update
sudo apt install Jenkins
sudo systemctl status jenkins
find /usr/lib/jvm/java-1.8* | head -n 3

我的实例正在正确创建。但是,我相信我的 user_data 没有被执行。我可以看到 user_data 被复制到 /var/lib/cloud/instance/ 作为 user-data.txtuser-data.txt.i 但仍未执行。

为什么会发生这种情况,我该如何进一步调试?

更新

我尝试通过访问服务器并逐条执行每个命令,但在更新 apt 包时出现以下错误:

ubuntu@ip-172-31-52-**:~$ apt update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
ubuntu@ip-172-31-52-**:~$ sudo apt update
Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/

最佳答案

我在我的沙盒帐户上验证脚本。

它有错误。修正后的版本如下:

#!/bin/bash -xe

#Jenkins Installation:
apt update
apt install openjdk-8-jdk --yes
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
echo "deb https://pkg.jenkins.io/debian binary/" >> /etc/apt/sources.list
apt update
apt install -y jenkins
systemctl status jenkins
find /usr/lib/jvm/java-1.8* | head -n 3

变化是:

  • 删除 sudo
  • 修复 -
  • 修复 apt install Jenkinsapt install -y jenkins

我现在可以确认它有效 terraform在 Ubuntu 18.04 实例上。

启动实例后,jenkins可在 http://<instance-ip>:8080 获得:

enter image description here

关于amazon-web-services - user_data 未在创建实例时执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63144992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com