ฐานข้อมูลแบบมีการจัดการและบริการที่อ่านฐานข้อมูลนั้น บน Terraform และ CloudFormation
Aggregate หนึ่ง
บนFn::GetAtt ภายในFn::ImportValue ข้าม
บล็อก
ไฟล์
aggregate.yaml— เจตนาของ นัก พัฒนา: ส่วน ประกอบ dbชนิดcore/datastore/postgresพร้อมฐาน ข้อมูล เชิง ตรรกะ ordersที่เป็น ของ บทบาท orders_adminและส่วน ประกอบ apiชนิดcore/compute/serverที่ใช้ ส่วน ประกอบ นั้น ไม่มี คีย์ หรือ ค่า ใด ใน ไฟล์ เกี่ยว กับ AWS manifest-terraform.yaml— ผูกฐาน ข้อมูล กับ Recipe terraform/postgres-rds-terraform-aws-modulesผูกเซิร์ฟเวอร์ กับ terraform/server-ecsและผูก Feature core/backupกับterraform/backup-nativemanifest-cloudformation.yaml— ผูกฐาน ข้อมูล กับ cloudformation/postgres-rdsผูกเซิร์ฟเวอร์ กับ cloudformation/server-ecsและผูก Feature core/backupกับcloudformation/backup-nativerelease-terraform.yaml— ทั้งสอง ส่วน ประกอบ อยู่ ใน แนว เขต เดียว บิลด์ ผ่าน Manifest ของ Terraform release-one-stack.yaml— ทั้งสอง ส่วน ประกอบ อยู่ ใน แนว เขต เดียว บิลด์ ผ่าน Manifest ของ CloudFormation จึง ประกอบ เป็น สแตก เดียว release-two-stacks.yaml— เจตนาเดียวกัน ที่ แยก เป็น สอง แนว เขต การ อ้างอิง ของ เซิร์ฟเวอร์ ไป ยัง ฐาน ข้อมูล จึง ข้าม เส้น เชื่อม ของ สแตก .aggrega-config.yaml— ระบุชื่อ รีจิสทรี hub ที่ แค็ตตาล็อก ไลบรารี มาตรฐาน หา ค่า มา
เอนทิตี
สิ่งที่เซิร์ฟเวอร์ร้องขอ
เซิร์ฟเวอร์database และ
api: type: core/compute/server uses: database: db: target: db inject: env: - in: DB_HOST from: host - in: DB_PORT from: port - in: DB_USER from: user - in: DB_NAME from: databasecore/compute/server ไม่database จึงfrom: แต่ละ
สิ่งที่ผู้ดำเนินการจัดหา
แต่ละ Release ระบุinfra.overrides สิ่งinfra.image Recipes ของ
ทุก123456789012 คือexample-cluster, example-private-db-subnets, id ของ subnet-… และ sg-… role และ registry.example.com/orders-api:1.0.0 แทน
Recipes ของ Terraform อ่านsubnet_group, subnet_ids, vpc_security_groups, cluster, execution_role_arn) ส่วน Recipes ของ CloudFormation อ่านDBSubnetGroupName, VPCSecurityGroups, Cluster, Subnets, SecurityGroups, ExecutionRoleArn) ค่า
ร่างตัวอย่าง
คุณ
เขียน
aggrega draft --example aws-rds-and-ecs-serviceจาก
cd aws-rds-and-ecs-serviceบิลด์
ตัวอย่าง--auto-pull ใน
aggrega build --release release-terraform.yaml --auto-pullผล<workspace> แทน
Pull complete: release-terraform.lock (entities=11, source_artifacts=1, external_inputs=0)Total build time: 9.4sBuild completed: 151 files written to <workspace>/distเมื่อ
aggrega build --release release-terraform.yamlTotal build time: 5.7sBuild completed: 151 files written to <workspace>/distกับผู้ช่วย AI
ผู้
ใช้เครื่องมือของ Aggrega เท่านั้น และอย่าสร้างโฟลเดอร์เอง หากโฟลเดอร์ของพื้นที่ทำงานยังไม่มีโฟลเดอร์ `aws-rds-and-ecs-service` เรียกเครื่องมือ `draft` หนึ่งครั้งโดยตั้ง `example` เป็น `aws-rds-and-ecs-service` และไม่ระบุ `dir`: เครื่องมือจะเขียนตัวอย่างลงในโฟลเดอร์ `aws-rds-and-ecs-service` ใหม่ จากนั้นเรียกเครื่องมือ `build` โดยตั้ง `workspace_root` เป็น `aws-rds-and-ecs-service` ตั้ง `release` เป็น `release-terraform.yaml` ตั้ง `auto_pull` เป็น true และตั้ง `write` เป็น true แล้วบอกฉันว่าบิลด์เขียนไฟล์ใดบ้าง ไฟล์เหล่านั้นอยู่ใต้ `aws-rds-and-ecs-service/dist/`การกำหนดค่ารูทของ Terraform ชุดเดียว
การdist/one-root/terraform/aggrega.modules.tf และmodule ของmodules/db/ และmodules/api/ การ
# Root configuration for this boundary. Each component rendered here is one# module block below, and the directory that block points at holds that# component's own files. One configuration means one dependency graph, which is# what lets a reference or a depends_on reach from one component to another.
module "api" { source = "./modules/api"
assign_public_ip = false cluster = "example-cluster" cpu = "256" desired_count = 2 environment = {"DB_HOST":"${module.db.db_instance_address}","DB_NAME":"orders","DB_PORT":"5432","DB_USER":"orders_admin"} execution_role_arn = "arn:aws:iam::123456789012:role/example-ecs-task-execution" image = "registry.example.com/orders-api:1.0.0" memory = "512" name = "api" port = 8080 security_groups = ["sg-0fedcba9876543210"] subnet_ids = ["subnet-0123456789abcdef0","subnet-0fedcba9876543210"]}
module "db" { source = "./modules/db"
allocated_storage = 50 backup_retention_period = 14 create_db_subnet_group = false db_name = "orders" db_subnet_group_name = "example-private-db-subnets" engine = "postgres" engine_version = "16" family = "postgres16" identifier = "db" instance_class = "db.t3.small" manage_master_user_password = true multi_az = true port = "5432" skip_final_snapshot = false storage_type = "gp3" username = "orders_admin" vpc_security_group_ids = ["sg-0123456789abcdef0"]}อาร์กิวเมนต์ environment ของDB_HOST คือ ${module.db.db_instance_address}: Recipe ของdb_instance_address ของmodule "db" ดัง
สิ่งsource: การmodule ของaggrega.modules.tf — ไม่ใช่
บนtofu init -backend=false โหลด
Initializing modules...- api in modules/api- db in modules/db- db.db_instance in modules/db/modules/db_instance- db.db_instance_role_association in modules/db/modules/db_instance_role_association- db.db_option_group in modules/db/modules/db_option_group- db.db_parameter_group in modules/db/modules/db_parameter_group- db.db_subnet_group in modules/db/modules/db_subnet_group[… omitted: provider installation, whose versions move with the registry, and OpenTofu's closing advice]จากtofu validate พิมพ์:
Success! The configuration is valid.สแตก CloudFormation สแตกเดียว
aggrega build --release release-one-stack.yaml --auto-pullPull complete: release-one-stack.lock (entities=11, source_artifacts=0, external_inputs=0)Total build time: 6.2sBuild completed: 1 file written to <workspace>/distสแตกdist/one-stack/cloudformation/cloudformation-stack.json มีDb คือApi คือApiTaskDefinition คือ task definition ที่ApiTaskDefinition:
"Environment": [ { "Name": "DB_HOST", "Value": { "Fn::GetAtt": [ "Db", "Endpoint.Address" ] } }, { "Name": "DB_NAME", "Value": "orders" }, { "Name": "DB_PORT", "Value": "5432" }, { "Name": "DB_USER", "Value": "orders_admin" } ],DB_HOST คือ Fn::GetAtt บน Endpoint.Address ของDb ที่ Recipe ของdb CloudFormation หาDb จึง
สแตก CloudFormation สองสแตก
aggrega build --release release-two-stacks.yaml --auto-pullPull complete: release-two-stacks.lock (entities=11, source_artifacts=0, external_inputs=0)Total build time: 6.2sBuild completed: 2 files written to <workspace>/distไม่มีaggregate.yaml เปลี่ยน Release วางdata และapp แต่ละdist/two-stacks-data/cloudformation/cloudformation-stack.json เผยExport ส่วน Outputs ของ
"Outputs": { "dbXXdatabase": { "Value": "orders", "Export": { "Name": "aggrega--orders-two-stacks--two-stacks-data-zscloudformation--db--database" } }, "dbXXhost": { "Value": { "Fn::GetAtt": [ "Db", "Endpoint.Address" ] }, "Export": { "Name": "aggrega--orders-two-stacks--two-stacks-data-zscloudformation--db--host" } }, "dbXXport": { "Value": "5432", "Export": { "Name": "aggrega--orders-two-stacks--two-stacks-data-zscloudformation--db--port" } }, "dbXXprotocol": { "Value": "postgres" }, "dbXXuser": { "Value": "orders_admin", "Export": { "Name": "aggrega--orders-two-stacks--two-stacks-data-zscloudformation--db--user" } } },protocol ก็Export เพราะdist/two-stacks-app/cloudformation/cloudformation-stack.json อ่าน
"Environment": [ { "Name": "DB_HOST", "Value": {"Fn::ImportValue":"aggrega--orders-two-stacks--two-stacks-data-zscloudformation--db--host"} }, { "Name": "DB_NAME", "Value": {"Fn::ImportValue":"aggrega--orders-two-stacks--two-stacks-data-zscloudformation--db--database"} }, { "Name": "DB_PORT", "Value": {"Fn::ImportValue":"aggrega--orders-two-stacks--two-stacks-data-zscloudformation--db--port"} }, { "Name": "DB_USER", "Value": {"Fn::ImportValue":"aggrega--orders-two-stacks--two-stacks-data-zscloudformation--db--user"} } ],ชื่อ export แต่ละFn::ImportValue ภายใน
รหัสผ่านเป็นของ RDS เอง
Aggregate ระบุmanage_master_user_password = true บนManageMasterUserPassword บนProperties ของ
"Properties": { "AllocatedStorage": 50, "BackupRetentionPeriod": 14, "DBInstanceClass": "db.t3.small", "DBName": "orders", "DBSubnetGroupName": "example-private-db-subnets", "Engine": "postgres", "EngineVersion": "16", "ManageMasterUserPassword": true, "MasterUsername": "orders_admin", "MultiAZ": true, "Port": 5432, "StorageType": "gp3", "VPCSecurityGroups": [ "sg-0123456789abcdef0" ] },RDS สร้าง
การสำรองเป็นการแนบ
RDS สำรองcore/backup กับdb:
db: features: backup: type: core/backup infra: retention_days: 14Manifest แต่ละterraform/backup-native หรือ cloudformation/backup-native และbackup_retention_period = 14 บน"BackupRetentionPeriod": 14 ใน Properties ของ
การบิลด์ไม่ใช่การปรับใช้
แต่ละdist/ แล้ว
อินพุตเดียวกัน เอาต์พุตเดียวกัน
การ
dist/, .aggrega/ และ*.lock ถูก