当我aws_cloudwatch_log_resource_policy在配置文件中使用该文件时,它已成功应用。我期望某个策略出现在Web控制台的IAM->“策略”列表中,但是没有新策略的迹象。
aws_cloudwatch_log_resource_policy创建什么样的资源?
在一个Terraform项目中,我将通过获取并使用最新的生产数据库快照来创建RDS集群:
# Get latest snapshot from production DB
data "aws_db_snapshot" "db_snapshot" {
most_recent = true
db_instance_identifier = "${var.db_instance_to_clone}"
}
#Create RDS instance from snapshot
resource "aws_db_instance" "primary" {
identifier = "${var.app_name}-primary"
snapshot_identifier = "${data.aws_db_snapshot.db_snapshot.id}"
instance_class = "${var.instance_class}"
vpc_security_group_ids = ["${var.security_group_id}"]
skip_final_snapshot = true
final_snapshot_identifier = "snapshot"
parameter_group_name = "${var.parameter_group_name}"
publicly_accessible = true
timeouts {
create = "2h"
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于,在运行terraform代码之后(一旦拍摄了另一个快照),希望使用数据库的最新快照重新创建主RDS实例(以及随后的只读副本)。我在考虑指定首次运行的布尔计数参数的含义,但是count = 0在快照资源上进行设置会导致数据库资源的snapshot_id参数出现问题。同样count = 0,在db资源上设置a 表示将破坏数据库。
为此,用例是能够更改该地形计划管理的生产基础结构的其他方面,而不必重新创建整个RDS集群,这是销毁/创建非常耗时的资源。
amazon-web-services amazon-rds terraform terraform-provider-aws
# example.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0d44833027c1a3297"
instance_type = "t2.micro"
security_groups = ["${aws_security_group.example.name}"]
key_name = "${aws_key_pair.generated_key.key_name}"
provisioner "remote-exec" {
inline = [
"cd /home/ubuntu/",
"nohup python3 -m http.server 8080 &",
]
connection {
type = "ssh"
private_key = "${tls_private_key.example.private_key_pem}"
user = "ubuntu"
timeout = "1m"
}
}
}
resource "tls_private_key" "example" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = "example_key_pair"
public_key = "${tls_private_key.example.public_key_openssh}"
}
resource …Run Code Online (Sandbox Code Playgroud) 我要让Terraform在另一个需要MFA的帐户中担任IAM角色真是太糟糕了。这是我的设置
AWS配置
[default]
region = us-west-2
output = json
[profile GEHC-000]
region = us-west-2
output = json
....
[profile GEHC-056]
source_profile = GEHC-000
role_arn = arn:aws:iam::~069:role/hc/hc-master
mfa_serial = arn:aws:iam::~183:mfa/username
external_id = ~069
Run Code Online (Sandbox Code Playgroud)
AWS凭证
[default]
aws_access_key_id = xxx
aws_secret_access_key = xxx
[GEHC-000]
aws_access_key_id = same as above
aws_secret_access_key = same as above
Run Code Online (Sandbox Code Playgroud)
分配给IAM用户的策略
STS政策
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AssumeRole",
"Effect": "Allow",
"Action": [
"sts:AssumeRole"
],
"Resource": [
"arn:aws:iam::*:role/hc/hc-master"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
用户政策
{
"Statement": [
{
"Action": …Run Code Online (Sandbox Code Playgroud) amazon-web-services amazon-iam terraform terraform-provider-aws
我有一个带有 vpc、公共子网、路由和安全组的简单 AWS 部署。运行terraform apply将启动一个 AWS 实例,我将该实例配置为关联一个公共 IP。创建实例后,我运行terraform plan它并正确地说一切都是最新的。到目前为止没有问题。
我们有一个管理节点,如果它在一段时间内未使用,作为节省成本的措施,它将关闭该实例。
问题是:一旦该实例关闭,当我运行时terraform plan,aws 提供程序会看到所有配置正确,但由于公共 IP 已发布,associate_public_ip_address 的值不再与 terraform 配置中的配置相匹配,因此 terraform 需要删除并重新创建该实例:
associate_public_ip_address: "false" => "true" (forces new resource)
有没有办法让 terraform 忽略那个参数?
这个问题与https://github.com/hashicorp/terraform/issues/7262略有关系。但在我的情况下,我不想设置预期状态,我只想能够告诉 terraform 忽略该参数,因为它现在没有关联就可以了,只要它被配置为关联时开始。
(我在写这个问题时想到了这一点:我没有尝试配置子网以自动关联其中启动的实例的公共 ip。可以想象,通过使子网自动执行此操作,并从“aws_instance”中删除选项,我可能会能够使 terraform 不注意该值...但我对此表示怀疑。)
我正在使用 terraform 创建 Cognito 用户池、用户池客户端和域。如果 Cognito 用户池(例如属性)有更新,terraform 需要销毁并重新创建这三个资源,但是 terraform apply 在销毁 aws_cognito_user_pool_domain 期间失败并出现错误:
InvalidParameter: 1 validation error(s) found。- 最小字段大小为 1,DeleteUserPoolDomainInput.UserPoolId。
Terraform 版本:0.11.11
aws 提供程序版本:1.52.0
我曾尝试手动删除域并运行 terraform plan/apply 但随后失败并显示“InvalidParameterException:不存在这样的域或用户池”。
resource "aws_cognito_user_pool" "admin_cognito_pool" {
name = "dev-admin-pool"
alias_attributes = ["email"]
auto_verified_attributes = ["email"]
admin_create_user_config = {
allow_admin_create_user_only = true
}
}
resource "aws_cognito_user_pool_client" "admin_cognito_pool_client" {
name = "dev-admin-pool-client"
user_pool_id = "${aws_cognito_user_pool.admin_cognito_pool.id}"
generate_secret = false
...
}
resource "aws_cognito_user_pool_domain" "admin_cognito_domain" {
domain = "demo-dev"
user_pool_id = "${aws_cognito_user_pool.admin_cognito_pool.id}"
}
Run Code Online (Sandbox Code Playgroud)
以上代码将成功创建用户池、用户池客户端、用户池域。
接下来修改上面代码中的aws_cognito_user_pool并运行terraform plan/apply
resource "aws_cognito_user_pool" …Run Code Online (Sandbox Code Playgroud) 我有一个要创建的用户列表、一个 sns 主题列表并创建策略以授予用户对主题的权限。这些都是针对用户的命名空间...
鉴于:
主文件
provider "aws" {
region = "eu-west-1"
profile = "terraform"
}
module "topics" {
source = "./queues/topics"
}
module "users" {
source = "./users"
}
module "policies" {
source = "./policies"
sns_topics = "${module.topics.sns_topics}"
}
Run Code Online (Sandbox Code Playgroud)
./queues/topics.tf
resource "aws_sns_topic" "svc_topic" {
count = "${length(var.sns_topics)}"
name = "${element(var.sns_topics, count.index)}"
}
Run Code Online (Sandbox Code Playgroud)
./queues/topics/vars.tf
# List of topics
variable "sns_topics" {
type = "list"
default = [
"a-topic",
"b-topic",
"c-topic",
]
}
Run Code Online (Sandbox Code Playgroud)
./queues/topics/output.tf
output "sns_topics" {
value = "${var.sns_topics}"
}
Run Code Online (Sandbox Code Playgroud)
./users/main.tf …
我有一个使用两个模块的计划:bucket-website和cloudfront-website
除了bucket模块中的其他内容(策略等)之外,还有以下资源用于创建bucket并将其作为网站使用:
resource "aws_s3_bucket" "bucket-website" {
bucket = "${var.bucket_name}"
region = "${var.region}"
website {
index_document = "index.html"
}
tags = "${local.common_tags}"
}
Run Code Online (Sandbox Code Playgroud)
此模块还具有以下输出:
output "website_endpoint" {
value = "${aws_s3_bucket.bucket-website.website_endpoint}"
}
Run Code Online (Sandbox Code Playgroud)
该cloudfront-website模块具有包含所有这些云前端属性(IP、缓存内容等)的资源,但相关部分是:
resource "aws_cloudfront_distribution" "distribution" {
.....
origin {
domain_name = "${var.domain_name}"
origin_id = "${var.domain_name}"
}
.....
}
Run Code Online (Sandbox Code Playgroud)
计划中对 cloudfront 模块的调用传递了以下参数:
domain_name = "${module.bucket-website.website_endpoint}"
Run Code Online (Sandbox Code Playgroud)
我可以确认该值是正确的,因为在terraform applyis的日志中可以看到:
origin.123456.domain_name: "" => "foo.s3-website-eu-west-1.amazonaws.com"
origin.123456.origin_id: "" => "foo.s3-website-eu-west-1.amazonaws.com"
Run Code Online (Sandbox Code Playgroud)
如果我仅使用 AWS 控制台进行此设置,我将使用哪个端点,即获取存储桶的静态 Web 端点(与标准存储桶端点不同)并将其用作 Cloudfront 的来源。
但是,出于某种原因,Terraform …
使用“模式”中的任何内容在 Terraform 中创建 aws_cognito_user_pool 会导致每次运行 Terraform 时重新创建用户池。我们想使用自定义属性,因此需要在架构中设置选项。
根据文档
“在定义 String 或 Number 的 attribute_data_type 时,需要相应的属性约束配置块(例如 string_attribute_constraints 或 number_attribute_contraints)以防止重新创建 Terraform 资源。此要求对于标准(例如名称、电子邮件)和自定义架构属性均适用。 ”
如果我理解正确,我还需要列出模式中的所有标准属性,以便我可以添加 string_attribute_contraints。
resource "aws_cognito_user_pool" "pool" {
count = "${var.user_pool_count}"
name = "${lookup(var.user_pool[count.index], "name")}"
username_attributes = ["email"]
auto_verified_attributes = ["email"]
schema = [
{
name = "address"
attribute_data_type = "String"
string_attribute_constraints = {
min_length = 1
}
},
{
name = "birthdate"
attribute_data_type = "String"
string_attribute_constraints = {
min_length = 1
}
},
{
name = "email"
attribute_data_type = "String" …Run Code Online (Sandbox Code Playgroud) 我有一个用于分配 AWS Lambda 的 Terraform 配置。我希望它在检测到更改时自动更新代码。为此,我正在使用该source_code_hash属性,如下所示。作为构建过程的一部分,我将所有代码压缩,然后用于openssl获取 zip 文件的 SHA256 哈希值,将其粘贴到文本文件中,然后将两者上传到 S3 存储桶。
这是我的 Terraform 配置:
data "aws_s3_bucket_object" "mylambdacode_sha256" {
bucket = "myapp-builds"
key = "build.zip.sha256"
}
resource "aws_lambda_function" "my_lambda" {
function_name = "${var.lambda_function_name}"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "index.handler"
runtime = "nodejs10.x"
s3_bucket = "myapp-builds"
s3_key = "build.zip"
source_code_hash = "${data.aws_s3_bucket_object.mylambdacode_sha256.body}"
timeout = 900
}
Run Code Online (Sandbox Code Playgroud)
我跑了terraform apply,它告诉我source_code_hash已经改变了:
~ source_code_hash = <<~EOT
- c9Nl4RRfuh/Z5fJvEOT69GDTJqY9n/QTB5cBGtOniYc=
+ 73d365e1145fba1fd9e5f26f10e4faf460d326a63d9ff4130797011ad3a78987
Run Code Online (Sandbox Code Playgroud)
该73d365e1145fba1fd9e5f26f10e4faf460d326a63d9ff4130797011ad3a78987部分确实是 S3 上文件中的内容。所以,这一切看起来都是正确的。但是当我terraform apply …
hash amazon-web-services terraform devops terraform-provider-aws