# variable.py
buckets_info = [
{
"name": "my-s3-bucket-1",
"lifecycle_rules": [
{
"id": "expire_objects",
"enabled": True,
"expiration_days": 8,
}
],
},
{
"name": "my-s3-bucket-2",
"lifecycle_rules": [
{
"id": "expire_objects",
"enabled": True,
"expiration_days": 8,
}
],
},
# 添加更多桶的配置...
]
# s3_module.py
from cdktf import App, TerraformStack
from constructs import Construct
from imports.aws import AwsProvider, s3
import variable
class MyS3Buckets(TerraformStack):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)
AwsProvider(self, "AWS", region="us-east-1")
# 根据 variable.py 中定义的信息创建多个 S3 桶
for bucket_info in variable.buckets_info:
s3.Bucket(self,
bucket_info["name"],
bucket=bucket_info["name"],
lifecycle_rule=bucket_info["lifecycle_rules"],
versioning=[s3.BucketVersioningArgs(
enabled=False
)])
app = App()
MyS3Buckets(app, "my-s3-buckets")
app.synth()