我想安全地从s3_bucket_object. 这意味着如果 s3 对象不存在,则提供默认值。有没有办法做到这一点 ?
如果我指定不存在的密钥,则会失败
data "aws_s3_bucket_object" "deployed_builds_s3" {
bucket = my_bucket_name
key = "path/to/file.txt"
}
Run Code Online (Sandbox Code Playgroud)
错误:
Error: Failed getting S3 object: NotFound: Not Found
Run Code Online (Sandbox Code Playgroud)
我知道可以像这样处理本地文件:
locals {
file_content = fileexists("file.txt") ? file("file.txt") : ""
}
Run Code Online (Sandbox Code Playgroud)
s3 对象有类似的东西吗?
我已经构建了一个 Terraform 配置,用于部署 MongoDB atlas 云集群,并使用我的 AWS 账户设置 VPC 对等点。terraform 配置将凭证存储在 AWS Secrets Manager 中。不幸的是,我的 Lambda 无法访问 Secrets Manager API 端点或 Atlas 上托管的 MongoDB 集群。我读到我需要在我的 VPC 上设置 NAT 网关才能访问公共互联网。我不是网络专业人士,我尝试添加一堆不同的配置但无济于事。请帮忙:
理想情况下,我想尽可能地锁定与外部互联网的连接,但如果这不是一个选择,我可以接受任何可行的实现。
这是我的 Terraform 配置
variable "admin_profile" {
type = string
default = "superadmin"
}
variable "region" {
type …Run Code Online (Sandbox Code Playgroud) amazon-vpc aws-lambda mongodb-atlas terraform-provider-aws aws-secrets-manager
我想通过基于列表变量(email_addresses)的 loping 使用 terraform template_file 创建 CFT。以下是我尝试生成的变量和模板。
variables:-
emails_addresses = ["sample-1@gmail.com", "sample-2@gmail.com"]
sns_arn = "arn:aws:sns:us-east-1:xxxxxx:xxxx"
protocol = "email"
Run Code Online (Sandbox Code Playgroud)
期待模板:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"sample-1": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"Endpoint": "sample-1@gmail.com",
"Protocol": "email",
"TopicArn": "arn:aws:sns:us-east-1:xxxx:xxxxx"
}
},
"sample-2": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"Endpoint": "sample-2@gmil.com",
"Protocol": "email",
"TopicArn": "arn:aws:sns:us-east-1:xxx:xxxx"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
CFT 中的资源名称可以是一些随机字符串,但在多个计划/应用的情况下,每个邮件应该相同。
amazon-web-services amazon-sns terraform terraform-provider-aws
我正在使用 terraform 创建 lambda 函数,根据 terraform 语法 lambda 代码应作为 zip 文件传递。以类似的方式,我传入一个资源块,并且它的创建也没有任何问题。但是当我尝试在下次运行中使用 terraform 更新 lambda 代码时,它没有得到更新。下面的块供参考。
data "archive_file" "stop_ec2" {
type = "zip"
source_file = "src_dir/stop_ec2.py"
output_path = "dest_dir/stop_ec2_upload.zip"
}
resource "aws_lambda_function" "stop_ec2" {
function_name = "stopEC2"
handler = "stop_ec2.handler"
runtime = "python3.6"
filename = "dest_dir/stop_ec2_upload.zip"
role = "..."
}
Run Code Online (Sandbox Code Playgroud)
需要帮助来解决此问题。
我正在尝试按照此页面使用 terraform 在 SageMaker 上创建模型, 由于权限限制,我无法为 sagemaker 角色分配完整的访问策略,因此我创建了一个角色并附加了具有部分权限的策略
当我测试时Terraform plan,它给了我这个:
Error: Invalid template interpolation value
...
Run Code Online (Sandbox Code Playgroud)
..........................
141: "ecr:GetRepositoryPolicy"
142: ],
143: "Resource": [
144: "arn:aws:s3:::${aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket}",
145: "arn:aws:s3:::${local.binaries_bucket_name}",
146: "arn:aws:s3:::${aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket}/*",
147: "arn:aws:s3:::${local.binaries_bucket_name}/*",
148: "arn:aws:ecr:us-east-1:*:repository/*",
149.....................
157: }
158: ]
159: }
160: POLICY
|----------------
| aws_s3_bucket.xx_xxxxxxxxxx_xxx_bucket is object with 25 attributes
Cannot include the given value in a string template: string required.
Run Code Online (Sandbox Code Playgroud)
我对此很陌生,只是想知道这是在抱怨存储桶名称太长还是其他原因?我该怎么办才能解决这个问题,我有点困惑。非常感谢。
(PS:Terraform版本v0.13.4+提供商registry.terraform.io/hashicorp/aws v3.20.0)
amazon-s3 amazon-web-services terraform amazon-sagemaker terraform-provider-aws
有没有办法在 Terraform 中管理 AWS 安全组来编辑现有 SG 的规则?
例如:如果我配置一个新实例,现有 SG 的入口规则将更新以允许新配置的实例。当实例终止时,SG 还需要更新。
如果 Terraform 没有直接支持,请随意建议其他常见做法。
amazon-web-services terraform aws-security-group terraform-provider-aws
根据下面给出的文档示例,我看到实例类型的标签集。但是如果我希望将相同的标签应用于多个资源,那么我将如何设置它 https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template
tag_specifications {
resource_type = "instance"
tags = {
Name = "test"
}
}
Run Code Online (Sandbox Code Playgroud)
我使用 terraform 在 ap-southeast-1 中设置了 AWS 基础设施,但是,我想使用 aws_alb_listener 资源将在 us-east1 中创建的 ACM 证书链接到我的负载均衡器。
resource "aws_alb_listener" "https" {
load_balancer_arn = aws_lb.main.id
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = var.acm_certificate_arn
depends_on = [aws_alb_target_group.main]
default_action {
target_group_arn = aws_alb_target_group.main.arn
type = "forward"
}
}
Run Code Online (Sandbox Code Playgroud)
当我应用 terraform 时,它会引发错误。
是否可以使用 terraform 将 ACM 证书附加到来自不同区域的 alb?
我的用例是该证书还将在 AWS CloudFront 中用作 CDN。
amazon-web-services terraform terraform-provider-aws aws-acm
我需要 terraform s3 后端将一个存储桶用于我的生产 AWS 帐户,将另一个存储桶用于我的开发 AWS 帐户。我们需要这个,因为我们无法允许开发 AWS 账户中的用户访问生产 AWS 账户中的 s3 存储桶。S3存储桶名称必须是全局唯一的,因此存储桶名称字段不能相同。
我尝试在这里使用变量,但 terraform 后端中出现变量错误。这是一个非常需要的功能,并且有一个 GitHub 问题,但它已经开放了 4 年,似乎没有任何计划添加此功能,所以我需要一个解决方法。一项建议来自同一 GitHub 问题。该建议是使用terraform 远程状态数据源。不幸的是,这似乎不起作用。这是我尝试过的:
// backend.tf
terraform {
backend "s3" {}
}
data terraform_remote_state "state" {
backend = "s3"
config {
bucket = var.aws_account == "123456789000" ? "my-prod-bucket" : "my-dev-bucket"
key = "apps/main-stack.tfstate"
region = "us-east-1"
}
}
Run Code Online (Sandbox Code Playgroud)
没有使用任何值,并且它会提示手动输入所有值。
$ terraform init
Initializing modules...
Initializing the backend...
bucket
The name of the S3 bucket
Enter a …Run Code Online (Sandbox Code Playgroud) 我正在使用 Terraform 创建 AWS SQS 队列。对于每项服务,我需要创建两个队列,一个正常队列和一个错误队列。每个的设置大多相同,但我需要首先创建错误队列,以便我可以将其 ARN 传递到正常队列,作为其重新驱动策略的一部分。必须有一种更好的方法来循环替换仅名称,而不是创建 10 个模块。所以编程逻辑...对于queue_prefixes中的每个队列,创建错误模块,然后创建常规模块。我确信我只是没有正确搜索或提出正确的问题。
沙箱/main.tf
provider "aws" {
region = "us-west-2"
}
module "hfd_sqs_error_sandbox" {
source = "../"
for_each = var.queue_prefixes
name= each.key+"_Error"
}
module "hfd_sqs_sandbox" {
source = "../"
name=hfd_sqs_error_sandbox.name
redrive_policy = jsonencode({
deadLetterTargetArn = hfd_sqs_error_sandbox_this_sqs_queue_arn,
maxReceiveCount = 3
})
}
Run Code Online (Sandbox Code Playgroud)
变量.tf
variable "queue_prefixes" {
description = "Create these queues with the enviroment prefixed"
type = list(string)
default = [
"Clops",
"Document",
"Ledger",
"Log",
"Underwriting",
"Wallet",
]
}
Run Code Online (Sandbox Code Playgroud)