gpt4 book ai didi

amazon-s3 - 如何使用 Terraform 从 GitHub Enterprise 下载文件?

转载 作者:行者123 更新时间:2023-12-04 08:40:56 32 4
gpt4 key购买 nike

这是我的 s3_policy.json

{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"mybucket",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":[
"arn:aws:s3:::${bucket_name}/*"
],
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"10.xx.xxx.x",
"172.168.xx.x",
........,
.........,
..........,
...........,
]
}
}
}
]
}

我有共同的 repo,我将它用于不同的项目。这个常见的 repo 有一个 yaml 格式的 CIDR IP 列表。

我想将它拉入我的 Terraform 项目,以便我可以重新使用相同的文件而不是硬编码 IP 地址。

我无法找到一种方法来自动执行此操作,而不是在此 repo 中对 IP 地址进行硬编码。

最佳答案

您可以将 IP 地址用作数据源并使用它。

您的保单文件将如下所示:

resource "aws_iam_policy" "whitelist_ips" {
name = "whitelist_ips"
description = "${var.policy_description}"

policy = <<EOF
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"mybucket",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":[
"arn:aws:s3:::${bucket_name}/*"
],
"Condition": {
"IpAddress": {
"aws:SourceIp": ["${data.external.ip_addresses.result}"]
}
}
}
]
}
EOF
}

您需要创建一个 external data source可以运行它从某个位置获取 IP 地址并将 IP 作为逗号分隔的字符串返回。
data "external" "ip_addresses" {
program = ["python", "${path.module}/get_ips.py"]
}

哪里 get_ips.py可能看起来像这样:
#!/usr/bin/env python
from __future__ import print_function
import json
import re

yaml_string = """ - 1.2.3.4/32
- 1.2.3.5/32
- 1.3.0.0/16
"""

result = []
lines = yaml_string.split("\n")

for line in lines:
# Remove empty lines
if line != "":
result.append(re.sub('\s*-\s*', '', line))

print(json.dumps(','.join(result)))

但显然您需要从 Github 获取 YAML 列表,而不是在此数据源中毫无意义地对其进行硬编码。

关于amazon-s3 - 如何使用 Terraform 从 GitHub Enterprise 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317910/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com