我想编写用于CI / CD的Terraform脚本,我不喜欢脚本中的CD,而是有特定的路径。
我试过了 terraform init c:\my\folder\containing\tf-file
但是运行该命令会将.terraform文件夹放入我的cwd中。
我正在 Azure 中的 bash cloud shell 中使用 Terraform。我正在尝试将外部数据源添加到我的 Terraform 配置文件中,该文件将用于az cli查询virtualip模板部署的 Microsoft.Web/hostingEnvironment 上的对象。
AZ CLI 命令行:
az resource show --ids /subscriptions/<subscription Id>/resourceGroups/my-ilbase-rg/providers/Microsoft.Web/hos
tingEnvironments/my-ilbase/capacities/virtualip
从命令行运行时的输出:
{
"additionalProperties": {
"internalIpAddress": "10.10.1.11",
"outboundIpAddresses": [
"52.224.70.119"
],
"serviceIpAddress": "52.224.70.119",
"vipMappings": []
},
"id": null,
"identity": null,
"kind": null,
"location": null,
"managedBy": null,
"name": null,
"plan": null,
"properties": null,
"sku": null,
"tags": null,
"type": null
}
Run Code Online (Sandbox Code Playgroud)
在我的 Terraform 配置中,我为--ids值创建一个变量:
variable ilbase_resourceId {
default = "/subscriptions/<subscription Id>/resourceGroups/my-ilbase-rg/providers/Microsoft.Web/hostingEnvironments/my-ilbase/capacities/virtualip"
}
Run Code Online (Sandbox Code Playgroud)
然后我以这种方式构建数据源:
data …Run Code Online (Sandbox Code Playgroud) 您好EC2实例已创建,但作为userdata.sh一部分的命令未得到执行。当我通过腻子手动连接到EC2时,我发现EC2实例中未安装nginx。为了验证脚本是否正在执行,我添加了echo消息,但是当我运行terraform apply时,命令提示符中未显示任何输出。如何验证用户数据是否正在执行?
我已经在C驱动器中安装了Terraform,并且以下脚本位于同一文件夹C:/Terraform/userdata.sh、C:/Terraform/main.tf中,我尝试给出的路径为$ {file(“ ./ userdata.sh”) }”,但仍然无法正常工作。
请指教,因为我正在学习terraform。谢谢。
#!/bin/bash -v
echo "userdata-start"
sudo apt-get update -y
sudo apt-get install -y nginx > /tmp/nginx.log
sudo service nginx start
echo "userdata-end"
Run Code Online (Sandbox Code Playgroud)
这在我的terraform程序[main.tf]中称为如下:
# resource "template_file" "user_data" {
# template = "userdata.sh"
# }
data "template_file" "user_data" {
template = "${file("userdata.sh")}"
}
resource "aws_instance" "web" {
instance_type = "t2.micro"
ami = "ami-5e8bb23b"
key_name = "sptest"
vpc_security_group_ids = ["${aws_security_group.default.id}"]
subnet_id = "${aws_subnet.tf_test_subnet.id}"
user_data = "${data.template_file.user_data.template}"
#user_data = "${template_file.user_data.rendered}"
#user_data = "${file("userdata.sh")}"
#user_data = "${file("./userdata.sh")}" …Run Code Online (Sandbox Code Playgroud) 目前正在开发一个包含一些粘合服务的 TF 模板。当尝试初始化模板时,系统提示我以下错误:
Error: Invalid template control keyword
on main.tf, in resource "aws_glue_classifier" "SrcPlanClassifier":
grok_pattern = "%{DATA:col},%{DATA:col2},%{DATA:col3},"%{DATA:col4}",% .
{DATA:col5},%{DATA:col6},%{DATA:col7},%{DATA:col8},%{DATA:col9},%{DATA:col10},% .
{DATA:col11},%{DATA:col12},%{DATA:col}13$"
"DATA" is not a valid template control keyword.
Run Code Online (Sandbox Code Playgroud)
该模板是将现有 CloudFormation 模板转换为 Terraform 的结果,因此该模式在过去一直有效。据我在 Glue 的 AWS 文档中所知,DATA 术语是一个可接受的内置分类器。这对我来说意味着这是 Terraform 端的问题。我在这里缺少什么?
先谢谢您的帮助!
我正在尝试使用 terraform 模板语法检查模板文件中是否存在变量,但出现This object does not have an attribute named "proxy_set_header.
$ cat nginx.conf.tmpl
%{ for location in jsondecode(locations) }
location ${location.path} {
%{ if location.proxy_set_header }
proxy_set_header ${location.proxy_set_header};
%{ endif }
}
%{ endfor }
Run Code Online (Sandbox Code Playgroud)
我尝试过if location.proxy_set_header != "",if location.proxy_set_header但没有成功。
如何检查字符串模板中是否存在变量?
将多个模板文件分配给 user_Data 变量。由于某些架构模式,我们不想将此模板合并到单个文件中。
我是 terraform 的新手,所以在这方面很挣扎。
data "template_file" "userdata_lin1" {
template = <<EOF
#!/bin/bash
crontab cronjobfileremote
EOF
}
data "template_file" "userdata_lin2" {
template = <<EOF
#!/bin/bash
echo "hello"
EOF
}
user_data = "${data.template_file.user_data1.rendered}"
Run Code Online (Sandbox Code Playgroud) 有没有更简单的方法来理解它们?当我在代码中使用提供程序并执行terraform init- 它是否下载提供程序或插件?
第三方插件在哪里以及什么是?与第三方提供商一样吗?
我正在尝试从 github 获取 terraform 模块,如下所示:
module "example" {
source = "https://github.com/cloudposse/terraform-example-module.git?ref=master"
example = "Hello world!"
}
Run Code Online (Sandbox Code Playgroud)
当我运行时terraform init,出现以下错误:
Error: Failed to download module
Could not download module "example" (main.tf:6) source code from
"https://github.com/cloudposse/terraform-example-module.git?ref=master": error
downloading
'https://github.com/cloudposse/terraform-example-module.git?ref=master': no
source URL was returned
Run Code Online (Sandbox Code Playgroud)
我确认该回购确实存在......我错过了什么?
因此,通过 Terraform,我创建了一个 IAM 策略并将其附加到一个角色。我目前正在运行:
Terraform v0.12.16
provider.aws v2.40.0
provider.template v2.1.2
Run Code Online (Sandbox Code Playgroud)
执行代码时,我可以毫无问题地初始化 terraform。运行 terraform plan 时,出现以下错误:
Error: "policy" contains an invalid JSON: invalid character '}' looking for beginning of value
on ec2-iam.tf line 8, in resource "aws_iam_role_policy" "s3_ec2_policy":
8: resource "aws_iam_role_policy" "s3_ec2_policy" {
Run Code Online (Sandbox Code Playgroud)
我陷入了这个错误。任何意见将是有益的。下面是我的代码:
data "template_file" "s3_web_policy" {
template = file("scripts/iam/web-ec2-policy.json")
vars = {
s3_bucket_arn = "arn:aws:s3:::${var.my_app_s3_bucket}/*"
}
}
resource "aws_iam_role_policy" "s3_ec2_policy" {
name = "s3_ec2_policy"
role = aws_iam_role.s3_ec2_role.id
policy = data.template_file.s3_web_policy.rendered
}
resource "aws_iam_role" "s3_ec2_role" {
name = "s3_ec2_role"
assume_role_policy …Run Code Online (Sandbox Code Playgroud) amazon-web-services terraform terraform-template-file terraform-provider-aws
我需要帮助每周安排 ec2 卷快照,并且需要使用 terraform aws 的云监视规则存档\删除超过 1 个月的备份。
请指导我使用 terraform 编写云监视规则。
问候, 公羊
terraform terraform-template-file terraform-provider-azure terraform-provider-aws terraform-provider-gcp
我正在尝试使用 Terraform 在 AWS 中构建云基础设施。我想为 S3 存储桶添加一个策略,该策略通过templatefileterraform 的功能使用基于属性的授权 (ABAC)。我的问题是 terraform 和 AWS 使用的变量语法是相同的 ( ${...})。
这是策略模板:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReadRole1",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::${bucketName}/*",
"Effect": "Allow",
"Principal": "*",
"Condition": {
"s3:ExistingObjectTag/myid": "${aws:PrincipalTag/myid}"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
terrafrom 文件的相关部分是:
resource "aws_s3_bucket_policy" "mybuckets-policy" {
bucket = aws_s3_bucket.mybuckets[count.index].bucket
policy = templatefile("${path.module}/bucket-policy.json", {
bucketName = aws_s3_bucket.mybuckets[count.index].bucket
})
count = 2
}
Run Code Online (Sandbox Code Playgroud)
所以我想要的是${bucketName}模板的一部分被 terraform 替换,同时保留 AWS 表达式${aws:PrincipalTag/user-id}。但是在上面的配置上运行 terraform 会导致错误消息
调用函数“templatefile”失败:./bucket-policy.json:14,49-50:插值表达式后出现额外字符;期望用右大括号结束插值表达式,但发现了额外的字符。
如果我在模板中放置另一个项目${foobar} …
amazon-web-services terraform terraform-template-file terraform-provider-aws aws-policies
我想使用本地提供商使用 terraform 将内容添加到文件中。这是我正在使用的示例脚本
terraform {
required_version = "~>0.13"
required_providers {
local = "~>1.4"
}
}
resource "local_file" "literature" {
filename = "art_of_war.txt"
content = <<EOT
Hello
world
EOT
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误 Expected the start of an expression, but found an invalid expression token.。您能指出可能出现的错误吗?
我是Terraform的新手,所以甚至不确定这样的事情是否可行。举例来说,假设我有一个模板,该模板中部署了一个Azure资源组和一个密钥库。然后说我有另一个模板,可以将虚拟机部署到同一资源组中。是否可以在不破坏密钥库和资源组的情况下使用虚拟机模板进行破坏?我们试图将大型解决方案的各个部分划分开来,而不必将其全部放在一个模板中,并且我们希望能够独立管理每个部分而又不影响其他部分。
在相关说明中...我们将状态文件存储在Azure存储帐户中。如果我们将部署分解为多个分区的部署...每个部署应该有自己的状态文件还是应该都使用相同的状态文件?