当您在 terraform 中创建 AWS VPC 时,将为其分配一个默认路由表,该路由表将在 VPC 的 CIDR 块内路由流量。
我想为此添加默认路由,以将所有其他流量发送到 Internet。
routetable amazon-web-services terraform terraform-provider-aws
我将 Terraform 与 AWS 结合使用,并且能够使用 aws_storagegateway_gateway 资源创建 AWS Storage Gateway 文件网关。
网关将创建,状态将为“在线”,但控制台中尚未添加缓存磁盘,这是正常的,因为必须在创建网关后完成。该虚拟机确实有一个磁盘,并且可以在控制台中添加它,并且在控制台中执行此操作效果很好。
但是,一旦创建了网关,我就尝试使用 Terraform 添加磁盘,但似乎无法让代码工作,或者很可能不明白如何让它工作。
尝试使用 aws_storagegateway_cache 资源,但我在 disk_id 上收到错误,并且不知道如何让它从网关创建的代码中返回。
有人可能有一个工作示例,说明如何在创建网关后使用 Terraform 添加缓存磁盘,或者知道如何获取 disk_id 以便我可以添加它?
添加代码
provider "aws" {
access_key = "${var.access-key}"
secret_key = "${var.secret-key}"
token = "${var.token}"
region = "${var.region}"
}
resource "aws_storagegateway_gateway" "hmsgw" {
gateway_ip_address = "${var.gateway-ip-address}"
gateway_name = "${var.gateway-name}"
gateway_timezone = "${var.gateway-timezone}"
gateway_type = "${var.gateway-type}"
smb_active_directory_settings {
domain_name = "${var.domain-name}"
username = "${var.username}"
password = "${var.password}"
}
}
resource "aws_storagegateway_cache" "sgwdisk" {
disk_id = "SCSI"
gateway_arn = …Run Code Online (Sandbox Code Playgroud) 我使用以下命令将之前部署的 RDS 实例替换为手动配置的 RDS 实例:
./terraform destroy -target aws_db_instance.my_db./terraform import aws_db_instance.my_db my-rds-instance(在我可以使用之前必须销毁旧实例import。)
当我现在运行时./terraform plan,terraform想要销毁并重新创建RDS数据库:
-/+ aws_db_instance.my_db (new resource required)
id: "my-rds-instance" => <computed> (forces new resource)
address: "my-rds-instance.path.rds.amazonaws.com" => <computed>
allocated_storage: "100" => "100"
allow_major_version_upgrade: "false" => "false"
apply_immediately: "false" => "false"
arn: "arn:aws:rds:eu-central-1:123456789123:db:my-rds-instance" => <computed>
auto_minor_version_upgrade: "false" => "false"
availability_zone: "eu-central-1b" => <computed>
backup_retention_period: "7" => "7"
backup_window: "09:46-10:16" => "09:46-10:16"
ca_cert_identifier: "rds-ca-2015" => <computed>
character_set_name: "" => <computed>
copy_tags_to_snapshot: "false" => "false" …Run Code Online (Sandbox Code Playgroud) 当尝试使用 Terraform 设置某些 CloudWatch 警报时,由于某种原因,它找不到指标,并且警报仍然陷入数据不足的状态。Terraform 不会输出任何错误,如果我在 AWS 中手动搜索,我可以找到指标。我在这里缺少什么?
指向目标组的简单健康主机警报示例:
#healthy host alarm
resource "aws_cloudwatch_metric_alarm" "health" {
alarm_name = "${var.tag_app}_healthy_host"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "HealthyHostCount"
namespace = "AWS/ApplicationELB"
period = "60"
statistic = "Maximum"
threshold = "1"
alarm_description = "Healthy host count for EC2 machine"
alarm_actions = ["${data.aws_sns_topic.blabla.arn}"]
ok_actions = ["${data.aws_sns_topic.blabla.arn}"]
dimensions = {
TargetGroup = "${aws_lb_target_group.alb_target.arn_suffix}"
}
}
Run Code Online (Sandbox Code Playgroud)
当我选择另一个资源(EC2、RDS)和另一个指标时,我会收到指向正确指标的 CloudWatch 警报,并且它不会陷入数据不足的困境。
amazon-web-services amazon-cloudwatch terraform terraform-provider-aws
我在 Terraform 中定义了一个资源来创建我不想安排的 Glue Crawler。但我希望它在创建和更新后运行。我在文档中找不到有关如何触发此操作的任何内容。
resource "aws_glue_crawler" "my_crawler" {
database_name = "my_db"
name = "my_crawler"
role = "arn:aws:iam::111111111111:role/service-role/someRole"
s3_target {
path = "s3://my_bucket/key/prefix"
}
}
Run Code Online (Sandbox Code Playgroud) amazon-web-services terraform aws-glue terraform-provider-aws
在 terraform 中,我有一张地图service_map:
variable "service_map" {
type = map
description = "Map of some services and their ports."
default = {
"dns" = "53"
"web" = "443"
"ssh" = "22"
"proxy" = ""
}
}
Run Code Online (Sandbox Code Playgroud)
要在 AWS 上创建 LB 侦听器,我想调用资源aws_lb_listener,循环遍历地图service_map,跳过所有没有值的项目(在本例中,仅跳过proxy):
resource "aws_lb_listener" "listeners" {
for_each = var.service_map
load_balancer_arn = aws_lb.all_lbs[each.key].arn
port = each.value
protocol = each.key != "dns" ? "TCP" : "TCP_UDP"
default_action {
type = "forward"
target_group_arn = …Run Code Online (Sandbox Code Playgroud) 我的 main.tf 中有以下内容:
data "aws_iam_policy_document" "task_role_policy" {
dynamic "statement" {
for_each = var.policy_statements
content {
actions = statement.value.actions
resources = statement.value.resources
effect = "Allow"
}
}
}
Run Code Online (Sandbox Code Playgroud)
当 var.policy_statements 为空列表或什么都没有时,我在运行时收到以下错误terraform apply:
Error: Error creating IAM policy dev-chatbot-engine-policy: MalformedPolicyDocument: Syntax errors in policy.
status code: 400, request id: a181b065-b659-4261-87d5-9aae8c4454aa
on .terraform/modules/service/main.tf line 68, in resource "aws_iam_policy" "task_role":
68: resource "aws_iam_policy" "task_role" {
Run Code Online (Sandbox Code Playgroud) 是否terraform支持aws从保管库恢复映像的备份功能(https://www.terraform.io/docs/providers/aws/r/backup_plan.html)?
当我阅读该文档时,我可以看到它确实支持创建备份计划、分配资源和策略、创建保管库,但不支持恢复映像或 ebs 卷
How do i add the restore block in my terraform template
我正在尝试使用 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
我正在尝试更新启动配置用户数据。但是在申请后,启动配置正在创建和更新 ASG。但是正在运行的实例仍然带有旧的用户数据。为什么这样 ?
下面是启动配置和 ASG 块。
resource "aws_launch_configuration" "BackEndWebLaunchConfig" {
name_prefix = "${var.component_name}-BackEndWebLaunchConfig"
user_data = file("user_data/${terraform.workspace}/vision-be-user-data.sh")
image_id = var.ASLCWEBAPPSAMI
instance_type = var.ASGWebAppsInstanceType
key_name = var.ssh_key_name
security_groups = [module.vpc.sgssh, aws_security_group.vision_backend_EC2SG.id]
root_block_device {
volume_size = var.EC2_EBS_SIZE
volume_type = "standard"
encrypted = true
}
#iam_instance_profile = var.EC2_instance_profile
associate_public_ip_address = false
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "vision_asg" {
name = "${var.component_name}-BackEnd-ASG-TF"
max_size = var.ASGWEBAPPSMaxSize
min_size = var.ASGWEBAPPSMinSize
health_check_grace_period = 300
force_delete = true
health_check_type = "ELB"
desired_capacity = var.ASGWEBAPPSDesiredSize
launch_configuration …Run Code Online (Sandbox Code Playgroud) terraform ×10
amazon-rds ×1
aws-backup ×1
aws-cli ×1
aws-glue ×1
aws-policies ×1
routetable ×1