英文:
Terraform aws_launch_template user_data error
问题
在我的S3存储桶中,我有:
resource "aws_s3_bucket" "user_data" {
bucket = var.bucket_name
acl = "private"
}
resource "aws_s3_object" "object" {
bucket = aws_s3_bucket.user_data.id
key = "SamplePage.php"
source = "web/SamplePage.php"
etag = filemd5("web/SamplePage.php")
}
在我的user_data.tftpl
文件中:
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install php8.0 mariadb10.5
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo usermod -a -G apache ec2-user
sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
cd /var/www
mkdir inc
cd inc
sudo echo "<?php
define('DB_SERVER', '${rds_endpoint}');
define('DB_USERNAME','${user}');
define('DB_PASSWORD', '${password}');
define('DB_DATABASE', '${dbname}');
?>" > dbinfo.inc
sudo aws s3 cp s3://${var.bucket_name}/SamplePage.php /var/www/html/SamplePage.php
在我的启动模板中:
resource "aws_launch_template" "web" {
name_prefix = "web-"
image_id = data.aws_ami.amazon-linux.id
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.web_sg.id]
key_name = aws_key_pair.kp.key_name
user_data = templatefile("user_data.tftpl", { rds_endpoint = "${aws_db_instance.rds.endpoint}", user = var.db_username, password = var.db_password, dbname = var.db_name })
}
结果,我得到了这个错误:
in resource "aws_launch_template" "web":
│ 29: user_data = templatefile("user_data.tftpl", { rds_endpoint = "${aws_db_instance.rds.endpoint}", user = var.db_username, password = var.db_password, dbname = var.db_name })
│ ├────────────────
│ while calling templatefile(path, vars)
│
│ Invalid value for "vars" parameter: vars map does not contain key "var", referenced at
│ user_data.tftpl:22,23-26.
请问我该如何解决这个问题。
英文:
I am trying to connect my ec2 web server to my database instance. I created s3 bucket where I put my php code given it is bigger than userdata 16k Character Limit and then I passed the bucket object to userdata file. Below is my code and error.
In my S3 bucket I have:
resource "aws_s3_bucket" "user_data" {
bucket = var.bucket_name
acl = "private"
}
resource "aws_s3_object" "object" {
bucket = aws_s3_bucket.user_data.id
key = "SamplePage.php"
source = "web/SamplePage.php"
etag = filemd5("web/SamplePage.php")
}
In my user_data.tftpl I have:
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install php8.0 mariadb10.5
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo usermod -a -G apache ec2-user
sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
cd /var/www
mkdir inc
cd inc
sudo echo "<?php
define('DB_SERVER', '${rds_endpoint}');
define('DB_USERNAME','${user}');
define('DB_PASSWORD', '${password}');
define('DB_DATABASE', '${dbname}');
?>" > dbinfo.inc
sudo aws s3 cp s3://${var.bucket_name}/SamplePage.php /var/www/html/SamplePage.php
launch template I have:
resource "aws_launch_template" "web" {
name_prefix = "web-"
image_id = data.aws_ami.amazon-linux.id
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.web_sg.id]
key_name = aws_key_pair.kp.key_name
user_data = templatefile("user_data.tftpl", { rds_endpoint = "${aws_db_instance.rds.endpoint}", user = var.db_username, password = var.db_password, dbname = var.db_name})
In result, I am getting this error:
in resource "aws_launch_template" "web":
│ 29: user_data = templatefile("user_data.tftpl", { rds_endpoint = "${aws_db_instance.rds.endpoint}", user = var.db_username, password = var.db_password, dbname = var.db_name})
│ ├────────────────
│ while calling templatefile(path, vars)
│
│ Invalid value for "vars" parameter: vars map does not contain key "var", referenced at
│ user_data.tftpl:22,23-26.
How can I resolve this please.
答案1
得分: 2
在模板文件`user_data.tftpl`中,S3存储桶的引用使用了常规的变量引用:
```hcl
sudo aws s3 cp s3://${var.bucket_name}/SamplePage.php /var/www/html/SamplePage.php
要修复这个问题,你需要更新模板文件,使用以下内容:
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install php8.0 mariadb10.5
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo usermod -a -G apache ec2-user
sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
cd /var/www
mkdir inc
cd inc
sudo echo "<?php
define('DB_SERVER', '${rds_endpoint}');
define('DB_USERNAME','${user}');
define('DB_PASSWORD', '${password}');
define('DB_DATABASE', '${dbname}');
?>" > dbinfo.inc
sudo aws s3 cp s3://${bucket_name}/SamplePage.php /var/www/html/SamplePage.php
然后在templatefile
函数调用中,调整传入的变量:
user_data = templatefile("user_data.tftpl", {
rds_endpoint = aws_db_instance.rds.endpoint
user = var.db_username
password = var.db_password
dbname = var.db_name
bucket_name = aws_s3_bucket.user_data.id
})
英文:
The S3 bucket reference in the template file user_data.tftpl
is using the regular variable reference:
sudo aws s3 cp s3://${var.bucket_name}/SamplePage.php /var/www/html/SamplePage.php
To fix that, you need to update the template file with the following:
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install php8.0 mariadb10.5
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo usermod -a -G apache ec2-user
sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
cd /var/www
mkdir inc
cd inc
sudo echo "<?php
define('DB_SERVER', '${rds_endpoint}');
define('DB_USERNAME','${user}');
define('DB_PASSWORD', '${password}');
define('DB_DATABASE', '${dbname}');
?>" > dbinfo.inc
sudo aws s3 cp s3://${bucket_name}/SamplePage.php /var/www/html/SamplePage.php
And then in the templatefile
function call, adjust the variables that are passed in:
user_data = templatefile("user_data.tftpl", {
rds_endpoint = aws_db_instance.rds.endpoint
user = var.db_username
password = var.db_password
dbname = var.db_name
bucket_name = aws_s3_bucket.user_data.id
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论