Terraform aws_launch_template user_data 错误

huangapple go评论139阅读模式
英文:

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 &quot;aws_s3_bucket&quot; &quot;user_data&quot; {
  bucket = var.bucket_name
  acl    = &quot;private&quot;
}

resource &quot;aws_s3_object&quot; &quot;object&quot; {
  bucket = aws_s3_bucket.user_data.id
  key    = &quot;SamplePage.php&quot;
  source = &quot;web/SamplePage.php&quot;
  etag = filemd5(&quot;web/SamplePage.php&quot;)
}

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 &quot;&lt;?php
define(&#39;DB_SERVER&#39;, &#39;${rds_endpoint}&#39;);  
define(&#39;DB_USERNAME&#39;,&#39;${user}&#39;);
define(&#39;DB_PASSWORD&#39;, &#39;${password}&#39;);
define(&#39;DB_DATABASE&#39;, &#39;${dbname}&#39;);
?&gt;&quot; &gt; dbinfo.inc
sudo aws s3 cp s3://${var.bucket_name}/SamplePage.php /var/www/html/SamplePage.php

launch template I have:

resource &quot;aws_launch_template&quot; &quot;web&quot; {
  name_prefix            = &quot;web-&quot;
  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(&quot;user_data.tftpl&quot;, { rds_endpoint =        &quot;${aws_db_instance.rds.endpoint}&quot;, user = var.db_username, password = var.db_password, dbname = var.db_name})

In result, I am getting this error:

in resource &quot;aws_launch_template&quot; &quot;web&quot;:
│   29:   user_data              = templatefile(&quot;user_data.tftpl&quot;, { rds_endpoint = &quot;${aws_db_instance.rds.endpoint}&quot;, user = var.db_username, password = var.db_password, dbname = var.db_name})
│     ├────────────────
│     while calling templatefile(path, vars)
│
│ Invalid value for &quot;vars&quot; parameter: vars map does not contain key &quot;var&quot;, 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 "&lt;?php
define('DB_SERVER', '${rds_endpoint}');  
define('DB_USERNAME','${user}');
define('DB_PASSWORD', '${password}');
define('DB_DATABASE', '${dbname}');
?&gt;" &gt; 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 &quot;&lt;?php
define(&#39;DB_SERVER&#39;, &#39;${rds_endpoint}&#39;);  
define(&#39;DB_USERNAME&#39;,&#39;${user}&#39;);
define(&#39;DB_PASSWORD&#39;, &#39;${password}&#39;);
define(&#39;DB_DATABASE&#39;, &#39;${dbname}&#39;);
?&gt;&quot; &gt; 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(&quot;user_data.tftpl&quot;, {
  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
  })

huangapple
  • 本文由 发表于 2023年3月9日 18:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定