更新
我不明白,但我重新运行terraform apply并且它没有尝试复制资源(没有错误)。现在它可以正确检查资源。有点出乎意料的结局。
我正在学习 Terraform,我创建了模块以允许创建一些基本的安全组。它第一次运行良好并按预期创建资源。但是如果我terraform apply第二次运行,它会尝试再次创建相同的组,然后我会收到重复的错误,因为这样的安全组已经存在。
如果我不使用 直接创建安全组module,Terraform 会识别它并且不会尝试重新创建现有资源。
我可能在这里做错了什么。
这是我的模块以及我如何尝试使用它:
我的项目结构是这样的
??? main.tf
??? modules
? ??? security_group_ec2
? ? ??? main.tf
? ? ??? outputs.tf
? ? ??? variables.tf
? ??? security_group_rds
? ??? main.tf
? ??? outputs.tf
? ??? variables.tf
??? scripts
? ??? update-odoo-cfg.py
??? security_groups.tf
??? terraform.tfstate
??? terraform.tfstate.backup
??? variables.tf
??? vpc.tf
Run Code Online (Sandbox Code Playgroud)
现在我的 security_group_ec2 内容:
主要.tf:
resource "aws_security_group" "sg" {
name = "${var.name}"
description = "${var.description}"
vpc_id = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Terraform 创建一个 CodeBuild 项目,但是当我构建时,在 DOWNLOAD_SOURCE 步骤中出现以下错误:
CLIENT_ERROR:未找到主要源和源版本的存储库
该项目使用 CodeCommit 存储库作为源。这很奇怪,因为从 CodeCommit 控制台 GUI 到存储库的所有链接都适用于此构建 - 我可以看到提交,单击链接并访问 CodeCommit 存储库等,因此源设置似乎没问题。用于构建的策略对存储库具有“codecommit:GitPull”权限。
奇怪的是,如果我转到控制台中的构建并取消选中“允许 AWS CodeBuild 修改此服务角色,以便它可以与此构建项目一起使用”复选框,然后更新源,构建将起作用!但是我找不到从 Terraform 设置它的任何方法,如果您返回到“更新源”屏幕,它将默认重新打开。
这是我用来创建构建的 Terraform 代码。
# IAM role for CodeBuild
resource "aws_iam_role" "codebuild_myapp_build_role" {
name = "mycompany-codebuild-myapp-build-service-role"
description = "Managed by Terraform"
path = "/service-role/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
# IAM policy for the CodeBuild role
resource "aws_iam_policy" …Run Code Online (Sandbox Code Playgroud) 在现有的 Terraform 目录中:
~ terraform version
Terraform v0.11.11
+ provider.aws v1.51.0
Run Code Online (Sandbox Code Playgroud)
如果我设置了一个新的 Terraform 目录:
~ terraform version
Terraform v0.11.11
+ provider.aws v1.55.0
Run Code Online (Sandbox Code Playgroud)
如何升级我的provider.aws?如果我设置version = "~> 1.55.0"在provider "aws"我的.tf文件,我得到一个错误:
* provider.aws: no suitable version installed
version requirements: "~> 1.55.0"
versions installed: "1.51.0"
Run Code Online (Sandbox Code Playgroud)
我希望找到一个terraform update命令或类似的东西。但我找不到那个。
我不应该升级提供程序吗?我需要删除状态,重新运行init然后refresh吗?或者,还有更好的方法?
我想使用 terraform 而不是控制台创建具有访问权限和密钥的 IAM 用户(服务帐户)。我怎样才能做到这一点?谢谢,迪帕克
我想使用 Terraform 创建一个测试用户的AWS Cognito 用户池。创建用户池非常简单:
resource "aws_cognito_user_pool" "users" {
name = "${var.cognito_user_pool_name}"
admin_create_user_config {
allow_admin_create_user_only = true
unused_account_validity_days = 7
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我找不到创建 AWS Cognito 用户的资源。使用AWS Cli是可行的
aws cognito-idp admin-create-user --user-pool-id <value> --username <value>
Run Code Online (Sandbox Code Playgroud)
关于如何使用 Terraform 做到这一点的任何想法?
有没有人想出一个合适的方法来做到这一点?
简而言之,你有一个provider "aws",通过环境变量或配置文件配置,有或没有sts,都没关系。也许你有几个。
现在您想要调用awscli,因为 aws 提供程序中的某些内容没有得到很好的实现。就我而言,我需要生成一些敏感信息并将其直接上传到我不希望出现在状态文件中的 S3 存储桶。无论如何,它是s3 sync,因此该操作是幂等的。
但是,似乎无法将提供者凭据(永久、环境变量、配置文件到临时 sts)传递给子句null_resource:
provider "aws" {
# set using explicit setting or profile or however
alias = "myaws"
}
resource "null_resource" "cli" {
provisioner "local-exec" {
command = "aws <do something>"
environment {
# happy to pass AWS_PROFILE or AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY here...
# if there were a way to retrieve it from the "myaws" provider
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个由 Terraform 创建的 IAM 用户。密钥存储在 Hashicrop Vault 中,应用程序从那里读取它们。
我已经开发了 ansible 代码/bash 脚本来成功地定期轮换密钥。
但问题是 terraform 不喜欢旋转键。每当我们尝试运行 terraform 时,它都会尝试重新创建密钥
有没有办法通过 terraform 管理密钥轮换?或者我们可以在地形中忽略这一点吗?任何有关示例的帮助都会非常有帮助。
amazon-web-services amazon-iam terraform terraform-provider-aws
有谁知道是否有可能用代码片段来表示我是否可以在 terraform 变量的地图变量中创建地图变量?
variable "var" {
type = map
default = {
firstchoice = {
firstAChoice ="foo"
firstBChoice = "bar"
}
secondchoice = {
secondAChoice = "foobar"
secondBChoice = "barfoo"
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人对这是否可行或任何详细说明的文档有任何见解,那就太好了。
我想知道我们如何停止和重新启动使用 terraform 创建的 AWS ec2 实例。有没有办法做到这一点?
我想配置 CloudWatch Events 以使用 Terraform 将输入发送到 Lambda 函数。我使用以下脚本来执行此操作:
resource "aws_cloudwatch_event_rule" "aa-rule-event" {
count = "${var.count}"
name = "${var.application_name}-${element(var.names, count.index)}"
description = "${element(var.descriptions, count.index)}"
schedule_expression = "${element(var.cron-expressions, count.index)}"
is_enabled = "${element(var.rule-status-states, count.index)}"
}
resource "aws_cloudwatch_event_target" "aa-rule-target" {
count = "${var.count}"
rule = "${var.application_name}-${element(var.names, count.index)}"
target_id = "CloudWatchToLambda"
arn = "arn:aws:lambda:${var.aws_region}:${var.aws_account_number}:function:${var.application_name}-${element(var.target-lambda-function, count.index)}"
}
Run Code Online (Sandbox Code Playgroud)
我需要通过此 CloudWatch Event 向目标 Lambda 提供输入。我知道可以配置输入,但是如何在 Terraform 中配置它?
amazon-web-services amazon-cloudwatch terraform terraform-provider-aws