假设我想在我的开发帐户中启动EC2实例,但我可能会意外地运行错误的命令并创建prod帐户的临时凭证而不是开发帐户,然后当我Terraform申请时,我将在prod帐户启动EC2 ?
我怎样才能避免这种情况发生?我可以在此文件夹中创建具有开发帐户ID的文本文件,然后在启动EC2之前让Terraform将我的临时凭证的帐户ID与此文件中的帐户ID进行比较,也许在null_resource中?我无法弄清楚如何实现它.
我正在按照示例https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/aws_auth.tf使用 Terraform 设置 EKS 集群,现在我有两个 Terraform 文件:
kubeconfig.tf
resource "local_file" "kubeconfig" {
content = "${data.template_file.kubeconfig.rendered}"
filename = "tmp/kubeconfig"
}
data "template_file" "kubeconfig" {
template = "${file("template/kubeconfig.tpl")}"
...
}
Run Code Online (Sandbox Code Playgroud)
aws-auth.tf
resource "null_resource" "update_config_map_aws_auth" {
provisioner "local-exec" {
command = "kubectl apply -f tmp/config-map-aws-auth_${var.cluster-name}.yaml --kubeconfig /tmp/kubeconfig"
}
...
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个 local-exec 命令失败
输出:错误:stat tmp/kubeconfig:没有这样的文件或目录
在第二次运行时它成功了。我认为该文件是在 local-exec 尝试使用它之后创建的,并且 local-exec 应该依赖于文件资源。所以我尝试通过使用插值(隐式依赖)来表达依赖关系,如下所示:
resource "null_resource" "update_config_map_aws_auth" {
provisioner "local-exec" {
command = "kubectl apply -f tmp/config-map-aws-auth_${var.cluster-name}.yaml --kubeconfig ${resource.local_file.kubeconfig.filename}"
}
Run Code Online (Sandbox Code Playgroud)
但这总是给我
错误:资源“null_resource.update_config_map_aws_auth”供应商local-exec(#1):变量resource.local_file.kubeconfig.filename中引用了未知资源“resource.local_file”
到目前为止我已经得到了这个:
data "aws_iam_policy" "config_role" {
arn = "arn:aws:iam::aws:policy/service_role/AWSConfigRole"
}
Run Code Online (Sandbox Code Playgroud)
但我不确定如何将其附加到组中。
我编写了一些 Terraform 代码来创建一些服务器。对于 AMI,我使用 Terraform 数据模块获取最新的 Ubuntu 16.04 映像 ID 并将其分配给 EC2 实例。
最近我想向这个环境添加另一个 EC2 实例,但是当我运行时,terraform plan
我可以看到 Terraform 正在尝试删除现有的 EC2 实例并重新创建它们。原因是新的 Ubuntu 映像已发布,它正在尝试删除旧实例并使用新的 AMI ID 创建新实例。
由于我不想意外删除我们的生产服务器,我是否有机会解决这个问题?
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
module "jenkins" {
source = "terraform-aws-modules/ec2-instance/aws"
name = "Jenkins"
instance_count = 1
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.small"
associate_public_ip_address = true
disable_api_termination = true
key_name …
Run Code Online (Sandbox Code Playgroud) 我需要将文件夹上传到S3 Bucket。但是当我第一次申请时。它只是上传。但是我这里有两个问题:
terraform apply
再次运行时,显示Apply complete! Resources: 0 added, 0 changed, 0 destroyed
。我希望在运行terraform apply
并创建新版本时始终上传所有内容。我究竟做错了什么?这是我的Terraform配置:
resource "aws_s3_bucket" "my_bucket" {
bucket = "my_bucket_name"
versioning {
enabled = true
}
}
resource "aws_s3_bucket_object" "file_upload" {
bucket = "my_bucket"
key = "my_bucket_key"
source = "my_files.zip"
}
output "my_bucket_file_version" {
value = "${aws_s3_bucket_object.file_upload.version_id}"
}
Run Code Online (Sandbox Code Playgroud) amazon-s3 amazon-web-services terraform terraform-provider-aws
我正在尝试使用 aws_network_interface 创建一个 aws 实例,如下所示:
resource "aws_network_interface" "lustre-mds01" {
subnet_id = "${var.subnet_id}"
private_ips = ["10.1.0.10"]
}
resource "aws_instance" "lustre-mds01" {
ami = "${var.ec2_ami}"
instance_type = "t2.nano"
key_name = "${var.key_name}"
vpc_security_group_ids = [ "${var.vpc_security_group_id}" ]
root_block_device {
volume_type = "gp2"
volume_size = 128
}
network_interface {
network_interface_id = "${aws_network_interface.lustre-mds01.id}"
device_index = 0
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这会导致:
错误:“network_interface”:与 vpc_security_group_ids 冲突
这似乎存在问题,但由于不活动,该票证已关闭。我是一个 terraform noob,所以我不确定这看起来像一个错误还是只是用户错误。
我的环境:
$ terraform -v
Terraform v0.12.2
+ provider.aws v2.15.0
+ provider.external v1.1.2
+ provider.local v1.2.2
+ provider.null v2.1.2
Run Code Online (Sandbox Code Playgroud) 通过 Terraform 在 RDS 中创建角色后,我试图将其添加到 Postgresql 数据库。
我有两个单独的模块,一个创建 RDS 实例,一个向其中添加新角色。数据库地址是persistence
模块的输出和模块的输入persistenceApplicationRole
。问题好像是在创建RDS实例之前运行了Postgresql provider,所以地址为空。
我得到的错误是:
Error: Error initializing PostgreSQL client: error detecting capabilities: error PostgreSQL version: dial tcp :5432: connect: connection refused
on ../modules/persistenceApplicationRole/main.tf line 9, in provider "postgresql":
9: provider postgresql {
Run Code Online (Sandbox Code Playgroud)
通过-target=module.persistence
标志单独运行模块是有效的,因为persistenceApplicationRole
一旦创建数据库地址就会获取它。我在此处的文档中为 MySQL Provider 找到了一个包含这种确切场景的示例。
# module.persistenceApplicationRole
provider postgresql {
host = var.databaseAddress
username = data.external.root_credentials.result["username"]
password = data.external.root_credentials.result["password"]
superuser = false
}
resource "postgresql_role" "application_role" {
name = data.external.application_credentials.result["username"]
password …
Run Code Online (Sandbox Code Playgroud) postgresql amazon-web-services amazon-rds terraform terraform-provider-aws
我想credentials.json
在目录中创建一个文件 ( ),例如content
使用 Terraform。
内容将是私人服务帐户密钥的输出。
我使用以下代码创建服务帐户并获取其密钥data
:
resource "google_service_account" "my-account" {
account_id = "${var.account_id}"
project = "${var.project_id}"
}
resource "google_service_account_key" "my-account" {
service_account_id = "${google_service_account.my-account.name}"
}
data "google_service_account_key" "my-account" {
name = "${google_service_account_key.cd.name}"
public_key_type = "TYPE_X509_PEM_FILE"
}
Run Code Online (Sandbox Code Playgroud)
然后我如何将其转储到本地文件?
我的用例是我想创建一个credentials.json
启用定期备份jenkins
到谷歌云存储桶的功能。
我正在尝试在 GCP 中使用 terraform 创建 Firestore 索引。下面是我的 Terraform 脚本:
resource "google_firestore_index" "job_config1_index" {
project = var.projectId
collection = var.job_config_firestore
depends_on = [
"google_firestore_index.job_config4_index"
]
fields {
field_path = "customer_id"
order = "ASCENDING"
}
fields {
field_path = "job_type"
order = "ASCENDING"
}
fields {
field_path = "start_date_time"
order = "ASCENDING"
}
fields {
field_path = "__name__"
order = "ASCENDING"
}
}
Run Code Online (Sandbox Code Playgroud)
以下是日志:
Step #2: Error: Error waiting to create Index: Error waiting for Creating Index: timeout while waiting for state …
Run Code Online (Sandbox Code Playgroud) 有没有办法在请求实例之前获取实例类型(例如 t3.medium)可用的可用区?我正在尝试运行以下代码,但对于某些区域,它会因以下错误而失败:
Error: Error launching source instance: Unsupported: Your requested instance type (t3.micro) is not supported in your requested Availability Zone (us-east-1e). Please retry your request by not specifying an Availability Zone or choosing us-east-1a, us-east-1b, us-east-1c, us-east-1d, us-east-1f.
Run Code Online (Sandbox Code Playgroud)
显然,我可以手动将可用区域指定为受支持的区域之一,但我想最小化硬编码可用区域。
amazon-ec2 amazon-web-services terraform terraform-provider-aws
terraform ×10
amazon-ec2 ×2
amazon-ami ×1
amazon-iam ×1
amazon-rds ×1
amazon-s3 ×1
devops ×1
postgresql ×1