- Terraform ã®ç¶æ ãã¡ã¤ã«ï¼tfstateï¼ã¨ã¤ã³ãã¼ãï¼importï¼
- ãå³è§£ãAurora ã¯ã©ã¹ã¿ã¼ããªã¹ãã¢ããæµã
- 1.Terraform ã§ä½æãã Aurora ã¯ã©ã¹ã¿ã¼ãããç¶æ
- 2.Terraform ã§ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æ
- 3.AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãã¢ããªã®DBæ¥ç¶ããã¯ã©ã¹ã¿ã¼ãåãæ¿ããï¼Terraform å¤ã®ä½æ¥ï¼
- 4.ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf) ãåé¤
- 5.ç¶æ ãã¡ã¤ã«ï¼tfstateï¼ã§ããã¨ãã¨ããã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf)ã 㨠ãå®ç°å¢ã«ãããªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ãã対å¿ä»ãã
- 6. AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãå ã®ã¯ã©ã¹ã¿ã¼ãåé¤ï¼Terraform å¤ã®ä½æ¥ï¼
- æ¤è¨¼ã«å©ç¨ãããµã³ãã«ã³ã¼ã
- ãã¨ã㨠Terraform 㧠ä½æãã¦ãã Aurora ã¯ã©ã¹ã¿ã¼ï¼aws_rds_cluster.testï¼
- ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ãï¼aws_rds_cluster.test_restoreï¼
- AWS ç°å¢ã«ãããªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ããå ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ãã«ï¼aws_rds_cluster.testï¼ import ããã³ã¼ã
- å®éã«ãã£ã¦ã¿ã
- 1.Terraform ã§ä½æãã Aurora ã¯ã©ã¹ã¿ã¼ãããç¶æ
- 2.Terraform ã§ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æ
- 3.AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãã¢ããªã®DBæ¥ç¶ããã¯ã©ã¹ã¿ã¼ãåãæ¿ããï¼Terraform å¤ã®ä½æ¥ï¼
- 4.ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf) ãåé¤
- 5.ç¶æ ãã¡ã¤ã«ï¼tfstateï¼ã§ããã¨ãã¨ããã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf)ã 㨠ãå®ç°å¢ã«ãããªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ãã対å¿ä»ãã
- 6. AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãå ã®ã¯ã©ã¹ã¿ã¼ãåé¤ï¼Terraform å¤ã®ä½æ¥ï¼
- ã¾ã¨ã
æ¬è¨äºãå·çããã«ããããåææ¡ä»¶ã¨ãªã Terraform ã®ãç¶æ
ãã¡ã¤ã«ï¼tfstateï¼ãã¨ãã¤ã³ãã¼ãï¼importï¼ãã«ã¤ãã¦èª¬æãè¨è¼ãã¾ãã
ãã®æ¬¡ã«ãAurora ã¯ã©ã¹ã¿ã¼ããªã¹ãã¢ããæé ã«ã¤ãã¦ãæ¦è¦ã¨å®éã®æé ãè¨è¼ãã¾ãã
Terraform ã®ç¶æ ãã¡ã¤ã«ï¼tfstateï¼ã¨ã¤ã³ãã¼ãï¼importï¼
ç¶æ ãã¡ã¤ã«ï¼tfstateï¼
Terraform ã§ã¯ã terraform apply
ãå®è¡ããéã«ãå®éã®AWS ç°å¢ã®ç¶æ
ãä¿æãã¦ãã¾ãã apply ã ä¸å以ä¸å®è¡ããç°å¢ã«ã¯ãç¶æ
ãã¡ã¤ã« (tfstate) ãåå¨ãã¾ããï¼ãã¡ã¤ã«åï¼terraform.tfstateï¼
ç¶æ
ãã¡ã¤ã« (tfstate)ã§ã¯ããã³ã¼ã (tf) ã«è¨è¼ãã 1 ã¤ã®ãªã½ã¼ã¹ (resource)ã ã¨ããå®ç°å¢ã«ä½æãããªã½ã¼ã¹ãã 1 対 1 ã§å¯¾å¿ä»ããã¦ãã¾ãã
ç¶æ
ãã¡ã¤ã« (tfstate) ããããã¨ã«ãããäºåç®ä»¥éã® plan / apply æã«ã³ã¼ã (tf) 㨠AWS ç°å¢éã®å·®åã®ã¿ã表示ã»æ´æ°ãããã¨ãã§ãã¾ãã
terraform plan
ã terraform apply
ã§ã¯ãtfstate ãå
ã«ããã³ã¼ã (tf) ã«è¨è¼ãã 1 ã¤ã®ãªã½ã¼ã¹ (resource) ãã¨ããå®ç°å¢ã«ä½æãããªã½ã¼ã¹ã®ç¾å¨ã®ç°å¢ä¸ã§ã®ç¶æ
ããæ¯è¼ãã表示ã»æ´æ°ãã¦ãã¾ãã
ãåè1ï¼State | Terraform | HashiCorp Developer
This state is stored by default in a local file named "terraform.tfstate", but we recommend storing it in Terraform Cloud to version, encrypt, and securely share it with your team.
Terraform uses state to determine which changes to make to your infrastructure. Prior to any operation, Terraform does a refresh to update the state with the real infrastructure.
The primary purpose of Terraform state is to store bindings between objects in a remote system and resource instances declared in your configuration. When Terraform creates a remote object in response to a change of configuration, it will record the identity of that remote object against a particular resource instance, and then potentially update or delete that object in response to future configuration changes.
For more information on why Terraform requires state and why Terraform cannot function without state, please see the page state purpose.
詳細ã«ã¤ãã¦ã¯ä»¥ä¸ã®è¨äºããåç §ããã ããã¨å¹¸ãã§ãã
ç¶æ ãã¡ã¤ã«ï¼tfstateï¼ãããªã½ã¼ã¹ã®æ å ±ãåé¤ãã
ç¶æ
ãã¡ã¤ã«ï¼tfstateï¼ãããªã½ã¼ã¹ã®æ
å ±ãåé¤ãããã¨ãã§ãã¾ãã
Terraform ã§ä½æãããã®ã®ãä»å¾ terraform plan
ã terraform apply
ã®å®è¡å¯¾è±¡ããå¤ãããå ´åã«ä½¿ç¨ãã¾ãã
é常ã§ã¯ãã»ã¨ãã©ãªãã±ã¼ã¹ã§ãããã
ã³ãã³ãã®ä¾ã§ãã
terraform state rm aws_rds_cluster.db-1
ãåè1ï¼Command: state rm | Terraform | HashiCorp Developer
You can use terraform state rm in the less common situation where you wish to remove a binding to an existing remote object without first destroying it, which will effectively make Terraform "forget" the object while it continues to exist in the remote system.
é¡ä¼¼ã®ã³ãã³ãã«ã対å¿ä»ããããªã½ã¼ã¹ãå¤æ´ããterraform state mv
ãããã¾ãããã¡ããå©ç¨ã·ã¼ã³ã¯é常ã«éããã¾ãã
ãåè2ï¼Command: state mv | Terraform | HashiCorp Developer
You can use terraform state mv in the less common situation where you wish to retain an existing remote object but track it as a different resource instance address in Terraform, such as if you have renamed a resource block or you have moved it into a different module in your configuration.
ã¤ã³ãã¼ãï¼importï¼
Import ã§ã¯ãã³ã¼ã (tf) ã«è¨è¼ãããªã½ã¼ã¹ (resource)ã¨ã AWS ç°å¢ã«æ¢ã«ãããªã½ã¼ã¹ã®å¯¾å¿ä»ããè¡ããã¨ãã§ãã¾ãã
ã¤ã¾ããç¶æ
ãã¡ã¤ã«ï¼tfstateï¼å
ã«ãªã½ã¼ã¹ã®å¯¾å¿ä»ãã追å ãããã¨ãã§ãã¾ãã
Terraform 以å¤ã®æ¹æ³ã§ä½æãã AWS ãªã½ã¼ã¹ããæ¹ã㦠Terraform ã§ã³ã¼ã管çãããããªã¨ããªã©ã«ãå©ç¨ã§ãã¾ãã
import ããåæã¯ä»¥ä¸ã§ãã
- ã³ã¼ã (tf) ã«å¯¾è±¡ã®ãªã½ã¼ã¹ (resource) æ å ±ãè¨è¿°ãã¦ãããã¨
- AWS ç°å¢ã«æ¢ã«ãªã½ã¼ã¹ããããã¨
Import ããéã«ã¯ããã³ã¼ã (tf) ä¸ã®ãªã½ã¼ã¹å (resource)ã ã¨ããå®éã® AWS ç°å¢ã«ãããªã½ã¼ã¹ã®æ
å ±ãã import ãããã¯ã«æå®ãã¾ããä¸ã®ä¾ã§ã¯ãTerraform ä¸ã®ãªã½ã¼ã¹åï¼aws_rds_cluster.test
ã¨ãå®éã® AWS ç°å¢ã«ãã Aurora ã¯ã©ã¹ã¿ã¼ã®èå¥å db1-test
ã対å¿ä»ããã¾ãã
import { to = aws_rds_cluster.test id = "db1-test" }
以å㯠terraform import
ã³ãã³ãããã£ããã®ã®ãTerraform v1.5.0 以éã§ã¯ import ãããã¯ã使ããããã«ãªãã¾ãããçµæãterraform plan
ã terraform apply
ã³ãã³ãã®ã¿ã§ import ã§ããããã«ãªãã¾ããã
ãåè1ï¼Import - Configuration Language | Terraform | HashiCorp Developer
Use the import block to import existing infrastructure resources into Terraform, bringing them under Terraform's management. Unlike the terraform import command, configuration-driven import using import blocks is predictable, works with CICD pipelines, and lets you preview an import operation before modifying state.
Aurora ã¯ã©ã¹ã¿ã¼ã®å ´åã¯ã¯ã©ã¹ã¿ã¼ã®èå¥å (cluster_identifier
) ããå¼æ°ã«ãªãã¾ãããªã½ã¼ã¹ã«ãã£ã¦ã import ã³ãã³ãã«æ¸¡ãå¼æ°ãå¤ããã®ã§ã対象ãªã½ã¼ã¹ã®ããã¥ã¡ã³ãã確èªããå¿
è¦ãããã¾ãã
ãåè2ï¼Resource: aws_rds_cluster
Using terraform import, import RDS Clusters using the cluster_identifier. For example:
% terraform import aws_rds_cluster.aurora_cluster aurora-prod-cluster
æå¾ã«ãimport ãå®è¡ããã«ã¯ãterraform apply
ãå¿
è¦ã§ãã
Terraform processes the import block during the plan stage. Once a plan is approved, Terraform imports the resource into its state during the subsequent apply stage.
ãªããimport ãããã¯ã«ã¤ãã¦ã¯ãimport å¾ã«åé¤ããå¿
è¦ã¯ããã¾ããã
åãå
容㮠import ãè¤æ°åå®è¡ãã¦ã害ã¯ããã¾ããã
Terraform only needs to import a given resource once. Attempting to import a resource into the same address again is a harmless no-op. You can remove import blocks after completing the import or safely leave them in your configuration as a record of the resource's origin for future module maintainers. For more information on maintaining configurations over time, see Refactoring.
ãåèï¼Import - Configuration Language | Terraform | HashiCorp Developer
管çä¸ãä¸é½åããããªãåé¤ãã¦ã大ä¸å¤«ã§ãã
ãå³è§£ãAurora ã¯ã©ã¹ã¿ã¼ããªã¹ãã¢ããæµã
Terraform 㧠Aurora ã¯ã©ã¹ã¿ã¼ããªã¹ãã¢ãã¦ã¿ã¾ãããã
ãã¨ã㨠Terraform 㧠Aurora ã¯ã©ã¹ã¿ã¼ãä½æãã¦ããã¨ä»®å®ãã¾ãï¼resource åï¼aws_rds_cluster.testï¼ã
ãã¨ãã¨ããã¯ã©ã¹ã¿ã¼ãããã¤ã³ãã¤ã³ã¿ã¤ã ãªã«ããªæ©è½ã使ããææ°ã®ã¹ãããã·ã§ãããããªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æãã¾ãã
ãªã¹ãã¢ããã¨ãå
ã®ã¯ã©ã¹ã¿ã¼ã¨ç°ãªãèå¥åã®æ°ããã¯ã©ã¹ã¿ã¼ãã§ãã¾ãã®ã§ãã¢ããªããDBæ¥ç¶ãããããã«ãèå¥åãä»ãæ¿ãã¾ãã
åèï¼DB ã¯ã©ã¹ã¿ã¼ãæå®ã®æç¹ã®ç¶æ ã«å¾©å ãã - Amazon Aurora
DB ã¯ã©ã¹ã¿ã¼ãç¹å®ã®æç¹ã«å¾©å ããæ°ãã DB ã¯ã©ã¹ã¿ã¼ãä½æãããã¨ãã§ãã¾ãã
æå¾ã«ããªã¹ãã¢ããæ°ããã¯ã©ã¹ã¿ã¼ããå ã®ã¯ã©ã¹ã¿ã¼ãä½æãã Terraform ã³ã¼ãï¼resource åï¼aws_rds_cluster.testï¼ã§ç®¡çããã«ã¯ãç¶æ ãã¡ã¤ã« (tfstate) ã®å¯¾å¿ä»ããå¤æ´ããå¿ è¦ãããã¾ãã
å
·ä½çãªãµã³ãã«ã³ã¼ãã¯å¾ã»ã©è¨è¼ãã¾ãã
ã¾ãã¯å³ã§ä½æ¥ã®æµãã示ãã¾ãã
1.Terraform ã§ä½æãã Aurora ã¯ã©ã¹ã¿ã¼ãããç¶æ
Aurora ã¯ã©ã¹ã¿ã¼ã 1 ã¤ãããã¤ã³ã¹ã¿ã³ã¹ã 1 ã¤ããç¶æ
ã§ãã
2.Terraform ã§ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æ
ãã¨ãã¨ããã¯ã©ã¹ã¿ã¼ãããã¤ã³ãã¤ã³ã¿ã¤ã ãªã«ããªã§ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf) ã terraform apply
ãä½æãã¾ãã
3.AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãã¢ããªã®DBæ¥ç¶ããã¯ã©ã¹ã¿ã¼ãåãæ¿ããï¼Terraform å¤ã®ä½æ¥ï¼
AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§ããã¨ãã¨ããã¯ã©ã¹ã¿ã¼ã®èå¥åãããªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ã«ä»ãæ¿ãããã¨ã«ãããã¢ããªã®DBæ¥ç¶ããã¯ã©ã¹ã¿ã¼ãåãæ¿ãã¾ãã
4.ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf) ãåé¤
ãªã¹ãã¢ãå®äºããããããªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf) ãåé¤ãã¾ãã
5.ç¶æ ãã¡ã¤ã«ï¼tfstateï¼ã§ããã¨ãã¨ããã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf)ã 㨠ãå®ç°å¢ã«ãããªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ãã対å¿ä»ãã
ç¶æ
ãã¡ã¤ã«ï¼tfstateï¼ã§ã¯ãããã¨ãã¨ããã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf)ã 㨠ãå®ç°å¢ã«ãããªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ãã対å¿ä»ããã¦ç®¡çãã¾ãã
ãã®ããã«ãã¾ãã¯ç¶æ
ãã¡ã¤ã«ï¼tfstateï¼ãããªã½ã¼ã¹ã®æ
å ±ãåé¤ï¼terraform state rm
ï¼ãã¾ãã
ããã¦ããªã½ã¼ã¹ã®å¯¾å¿ä»ããæ°ãã«è¿½å ï¼import
ï¼ãã¾ãã
æå¾ã«ãterraform apply
ãå®æ½ãã¾ãã
6. AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãå ã®ã¯ã©ã¹ã¿ã¼ãåé¤ï¼Terraform å¤ã®ä½æ¥ï¼
AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãå
ã®ã¯ã©ã¹ã¿ã¼ãåé¤ãã¾ãã管çè
ã¦ã¼ã¶ã¼ã®èªè¨¼æ
å ±ã Secret Manager ã§ç®¡çãã¦ããå ´åã«ã¯ãã·ã¼ã¯ã¬ãããåé¤ãã¾ãã
ããã«ã¦ãªã¹ãã¢ãå®äºããç¶æ
ãã¡ã¤ã«ï¼tfstateï¼ãæ´åæ§ã®ããç¶æ
ã§ãã
ï¼å
ã®ã¯ã©ã¹ã¿ã¼ã管çãã¦ããã³ã¼ã (tf) ãå¤æ´ããã¨ããªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ã®æ¹ã«å¤æ´ãå ãããã¨ãã§ããç¶æ
ã§ããï¼
æ¤è¨¼ã«å©ç¨ãããµã³ãã«ã³ã¼ã
ãã¼ã¸ã§ã³ã¯ä»¥ä¸ãå©ç¨ãã¾ããã
- Terraform v1.7.4
- awsãããã¤ãã¼ v5.41.0
ãã¨ã㨠Terraform 㧠ä½æãã¦ãã Aurora ã¯ã©ã¹ã¿ã¼ï¼aws_rds_cluster.testï¼
ãã¨ã㨠Terraform 㧠ä½æãã¦ãã Aurora ã¯ã©ã¹ã¿ã¼ï¼aws_rds_cluster.testï¼ã® tf ãã¡ã¤ã«ã§ãã
- rds.tf
resource "aws_rds_cluster" "test" { cluster_identifier = "provisioning-rds" db_subnet_group_name = aws_db_subnet_group.test.name db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.test.name engine_version = "15.3" engine = "aurora-postgresql" engine_mode = "provisioned" enable_http_endpoint = true iam_roles = [] master_username = "postgres" manage_master_user_password = true enable_global_write_forwarding = false network_type = "IPV4" serverlessv2_scaling_configuration { max_capacity = 16 min_capacity = 2 } vpc_security_group_ids = ["sg-xxxxxxxxxx"] skip_final_snapshot = true } resource "aws_rds_cluster_instance" "test" { cluster_identifier = aws_rds_cluster.test.cluster_identifier db_subnet_group_name = aws_rds_cluster.test.db_subnet_group_name db_parameter_group_name = aws_db_parameter_group.test.name engine = aws_rds_cluster.test.engine engine_version = aws_rds_cluster.test.engine_version identifier = "rds-1" instance_class = "db.serverless" } resource "aws_db_subnet_group" "test" { name = "test_db_group" subnet_ids = ["subnet-xxxxxxxxxx","subnet-xxxxxxxxxx"] } resource "aws_rds_cluster_parameter_group" "test" { name = "test-rds-cluster-parameter-group" family = "aurora-postgresql15" description = "test" } resource "aws_db_parameter_group" "test" { name = "test-rds-instance-parameter-group" family = "aurora-postgresql15" description = "test" } resource "aws_ssm_parameter" "db_endpoint" { name = "jdbc_url" type = "String" value = aws_rds_cluster.test.endpoint } resource "aws_ssm_parameter" "reference_test" { name = "reference" type = "String" value = aws_ssm_parameter.db_endpoint.value }
ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ãï¼aws_rds_cluster.test_restoreï¼
ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããéã Terraform ã使ãã¾ããï¼aws_rds_cluster.test_restoreï¼ããã¡ãã¯ã¹ãããã·ã§ãããããªã¹ãã¢ããã³ã¼ãã«ãªã£ã¦ãããããé常éç¨æã® terraform plan/apply
ã§ã¯ä½¿ããªãã³ã¼ãã§ããé常æã¯åé¤ãã¦ããã¾ãã
ãµããããã°ã«ã¼ãããã©ã¡ã¼ã¿ã°ã«ã¼ãã¯ããã¨ã㨠Terraform 㧠ä½æãã¦ãã Aurora ã¯ã©ã¹ã¿ã¼ï¼aws_rds_cluster.testï¼ã¨åããã®ã使ç¨ãã¾ãã
- rds-restore.tf
data "aws_rds_cluster" "snapshot" { cluster_identifier = "provisioning-rds" } resource "aws_rds_cluster" "test_restore" { cluster_identifier = "provisioning-rds-restore" db_subnet_group_name = data.aws_rds_cluster.snapshot.db_subnet_group_name db_cluster_parameter_group_name = data.aws_rds_cluster.snapshot.db_cluster_parameter_group_name engine_version = data.aws_rds_cluster.snapshot.engine_version engine = data.aws_rds_cluster.snapshot.engine engine_mode = data.aws_rds_cluster.snapshot.engine_mode enable_http_endpoint = true iam_roles = data.aws_rds_cluster.snapshot.iam_roles master_username = data.aws_rds_cluster.snapshot.master_username manage_master_user_password = true enable_global_write_forwarding = false network_type = data.aws_rds_cluster.snapshot.network_type serverlessv2_scaling_configuration { max_capacity = 16 min_capacity = 2 } vpc_security_group_ids = data.aws_rds_cluster.snapshot.vpc_security_group_ids skip_final_snapshot = true restore_to_point_in_time { source_cluster_identifier = data.aws_rds_cluster.snapshot.id use_latest_restorable_time = true } } resource "aws_rds_cluster_instance" "test_restore" { cluster_identifier = aws_rds_cluster.test_restore.cluster_identifier db_subnet_group_name = aws_rds_cluster.test_restore.db_subnet_group_name db_parameter_group_name = "test-rds-instance-parameter-group" engine = aws_rds_cluster.test_restore.engine engine_version = aws_rds_cluster.test_restore.engine_version identifier = "rds-restore-1" instance_class = "db.serverless" }
AWS ç°å¢ã«ãããªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ããå ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ãã«ï¼aws_rds_cluster.testï¼ import ããã³ã¼ã
ã¯ã©ã¹ã¿ã¼ã¨ãã¤ã³ã¹ã¿ã³ã¹ã import ãã¾ãã
- rds-import.tf
import { to = aws_rds_cluster.test id = "provisioning-rds" } import { to = aws_rds_cluster_instance.test id = "rds-1" }
å®éã«ãã£ã¦ã¿ã
1.Terraform ã§ä½æãã Aurora ã¯ã©ã¹ã¿ã¼ãããç¶æ
2.Terraform ã§ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æ
ãã¨ã㨠Terraform 㧠ä½æãã¦ãã Aurora ã¯ã©ã¹ã¿ã¼ãããã¨ããã«ããªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ãï¼aws_rds_cluster.test_restoreï¼ã apply ãã¾ãã
ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ã¨ã¤ã³ã¹ã¿ã³ã¹ãã§ãã¾ããã
管çè
ã¦ã¼ã¶ã¼ã®æ
å ±ã Secrets Manager ã§ç®¡çããããã«ãã¦ããããªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ã«é¢ãã管çè
ã¦ã¼ã¶ã¼ã®ã·ã¼ã¯ã¬ãããã§ãã¾ããã
注æï¼ï¼
1 ç¹ã ã注æãããã¾ããã
ã³ã¼ã (tf) ã§ã¯ãenable_http_endpoint = true
ãè¨å®ããRDS Data API ãæå¹ã«ãã¦ããªã¹ãã¢ãã¦ãã¾ãã
å®éã«ãªã¹ãã¢ããã¤ã³ã¹ã¿ã³ã¹ã§ã¯ãRDS Data API ãç¡å¹ã«ãªã£ã¦ãã¾ããã
ãã®ãããAWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ããæåã§æå¹ã«ãã¾ããã
ã¡ãªã¿ã«ãAWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§ RDS Data API ãæå¹ã«ãã¦ããªã¹ãã¢ãã¦ããRDS Data API ãç¡å¹ã«ãªã£ã¦ãã¾ããã
ãã®ãããTerraform å´ã®åé¡ã§ã¯ãªããã¹ãããã·ã§ããããã®ãªã¹ãã¢æ©è½ã«èµ·å ãããã®ã¨èãããã¾ãã
3.AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãã¢ããªã®DBæ¥ç¶ããã¯ã©ã¹ã¿ã¼ãåãæ¿ããï¼Terraform å¤ã®ä½æ¥ï¼
ãã¨ãã¨ã®ã¯ã©ã¹ã¿ã¼åã®æ«å°¾ã« "-old" ãä»ä¸ãã¦ãããã«é©ç¨ãã¾ãã
ãªã¹ãã¢ããã¯ã©ã¹ã¿ã¼åããã¨ãã¨ã®ã¯ã©ã¹ã¿ã¼åã«å¤æ´ãã¦ãããã«é©ç¨ãã¾ãã
ãã¨ãã¨ã®ã¯ã©ã¹ã¿ã¼ã«ããã¤ã³ã¹ã¿ã³ã¹åã®æ«å°¾ã« "-old" ãä»ä¸ãã¦ãããã«é©ç¨ãã¾ãã
ãªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ã«ããã¤ã³ã¹ã¿ã³ã¹åãããã¨ãã¨ã®ã¤ã³ã¹ã¿ã³ã¹åã«å¤æ´ãã¦ãããã«é©ç¨ãã¾ãã
èå¥åã®å¤æ´ãçµããã¾ããã
4.ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf) ãåé¤
ãªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ãï¼aws_rds_cluster.test_restoreï¼ããã rds-restore.tf
ã Terraform ã®å®è¡ãã£ã¬ã¯ããªããåé¤ãã¾ããã
ããã«ãããterraform plan
ã terraform apply
ã®å½±é¿ç¯å²ããå¤ãã¾ããã
5.ç¶æ ãã¡ã¤ã«ï¼tfstateï¼ã§ããã¨ãã¨ããã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ã (tf)ã 㨠ãå®ç°å¢ã«ãããªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ãã対å¿ä»ãã
Aurora ã¯ã©ã¹ã¿ã¼ãã¤ã³ã¹ã¿ã³ã¹ã«ã¯ "Resource ID" ã¨ããããªã¼ã¸ã§ã³å
ã§ä¸æã® ID ãä»ãã¦ãã¾ããèå¥åï¼DB identifierï¼ã¨ã¯ç°ãªãã¾ãã
ç¾ç¶ã®ç¶æ
ãã¡ã¤ã« (tfstate)å
ã«ããããã³ã¼ã (tf) ã«è¨è¼ãã 1 ã¤ã®ãªã½ã¼ã¹ (resource)ã ã¨ããå®ç°å¢ã«ä½æãããªã½ã¼ã¹ãã® 1 対 1 ã®å¯¾å¿ä»ãå
容ãã"Resource ID" ã使ã£ã¦ç¢ºèªã§ãã¾ãã
以ä¸ã®ã³ãã³ãã§ç¢ºèªã§ãã¾ããåºåçµæã¯å²æãã¾ãã
terraform state show aws_rds_cluster.test
ãå®è¡ããcluster_resource_id
ã確èªããã¨ããã¨ãã¨ã® Aurora ã¯ã©ã¹ã¿ã¼ã® Resource ID ã«ãªã£ã¦ãã¾ããterraform state show aws_rds_cluster_instance.test
ãå®è¡ããdbi_resource_id
ã確èªããã¨ããã¨ãã¨ã® Aurora ã¯ã©ã¹ã¿ã¼ã«ããã¤ã³ã¹ã¿ã³ã¹ã® Resource ID ã«ãªã£ã¦ãã¾ãã
ç¾ç¶ã確èªã§ããã®ã§ãç¶æ ãã¡ã¤ã«ï¼tfstateï¼ããæ¢åã®æ å ±ãåé¤ãã¾ãã
terraform state rm aws_rds_cluster.test
terraform state rm aws_rds_cluster_instance.test
åé¤ã§ãã¾ããã
ãªã¹ãã¢ããã¯ã©ã¹ã¿ã¼ã»ã¤ã³ã¹ã¿ã³ã¹ã¨å¯¾å¿ä»ããããããã Terraform ã®å®è¡ãã£ã¬ã¯ããªã« import ç¨ã®ã³ã¼ã (tf) ãé
ç½®ãã¾ãã
- rds-import.tf
import { to = aws_rds_cluster.test id = "provisioning-rds" } import { to = aws_rds_cluster_instance.test id = "rds-1" }
terraform plan
ãå®è¡ãã¾ãã
2 ã¤ã® import 㨠1 ã¤ã® change ãããã¾ãã
change ã®è©³ç´°ã¯ãçæäºé
ããè¦ã¦ãã ããã
import ãåæ ãããããã«ãterraform apply
ãå®è¡ãã¾ãã
ããä¸åº¦ apply ãå®è¡ããã¨ãå·®åãªãã«ãªãã¾ããð
çæäºé
ã³ã¼ã(tf)ã¨å®éã®ãªã½ã¼ã¹ã®å¯¾å¿ä»ãã®ã¿è¡ããããimport ãåºæ¥ãã°è¯ãã¨èãã¦ããã®ã§ã1 ã¤ã® change ã¯æ³å®å¤ã§ããã
1 ã¤ã® change ã®å
容ã¯ãenable_global_write_forwarding = false
ã«ãããã¨ãããã®ã§ããã
enable_global_write_forwarding
㯠Aurora ã°ãã¼ãã«ã¯ã©ã¹ã¿ã¼åãã®ãã©ã¡ã¼ã¿ã§ãã°ãã¼ãã«ã¯ã©ã¹ã¿ã¼ã§ãªãå ´å㯠false
ã«ãªãã¾ãã
ã»ã«ã³ããªã¼ãªã¼ã¸ã§ã³ã«ã»ã«ã³ããªã¼ã®ã¯ã©ã¹ã¿ã¼ãä½æããéã«æå¹åã§ãã¾ãã
Aurora ã¯ã©ã¹ã¿ã¼ã®ä½ææã®ã³ã¼ãï¼aws_rds_cluster.testï¼ãããªã¹ãã¢ç¨ã®ã¯ã©ã¹ã¿ã¼ãä½æããã³ã¼ãï¼aws_rds_cluster.test_restoreï¼ã§ãæ示çã« false
ã«ãã¦ãã¦ããimport ã®éã« change ã¨ãã¦åºã¦ãã¾ãããã§ããã
issue ãæ¢ãããã®ã®ãè¦ã¤ããã¾ããã
terraform apply
ãå®è¡ãã¦ã false ã true/false ã«å¤æ´ããå¦çã¯çºçããããã¦ã³ã¿ã¤ã ãªã©å½±é¿ããªããããç¡è¦ãã¦åé¡ãªãã¨å¤æãã¾ããã
CloudTrail ã« ModifyDBCluster ã®å®è¡ãã°ã¯ãããã®ã®ãEnableLocalWriteForwarding ã«é¢ããå¤æ´å
容ãåºã¦ãã¾ããã§ããã
åè1ï¼Using write forwarding in an Aurora PostgreSQL global database - Amazon Aurora
ããã©ã«ãã§ã¯ãã»ã«ã³ããªã¯ã©ã¹ã¿ã¼ã Aurora Global Database ã«è¿½å ããã¨ãæ¸ãè¾¼ã¿è»¢éã¯æå¹ã«ãªãã¾ãããã»ã«ã³ã㪠DB ã¯ã©ã¹ã¿ã¼ã®æ¸ãè¾¼ã¿è»¢éã¯ãä½æä¸ã¾ãã¯ä½æå¾ã«ãã¤ã§ãæå¹ã«ã§ãã¾ããå¿ è¦ã«å¿ãã¦ãå¾ã§ç¡å¹ã«ãããã¨ãã§ãã¾ããæ¸ãè¾¼ã¿è»¢éãæå¹ã¾ãã¯ç¡å¹ã«ãã¦ãããã¦ã³ã¿ã¤ã ãåèµ·åã¯çºçãã¾ããã
åè2ï¼enable_global_write_forwarding
(Optional) Whether cluster should forward writes to an associated global cluster. Applied to secondary clusters to enable them to forward writes to an aws_rds_global_cluster's primary cluster. See the Aurora Userguide documentation for more information.
ãããissue ãè¦ã¤ãããã¨ãã§ããå ´åããissue ãä½æããå ´åã«ã¯æ¬è¨äºè¿½è¨ãã¾ãã
ⶠIssue ãä½æãã¾ããã è¨å®å¤ã¨ã㦠False ããªããªã½ã¼ã¹ã«å¯¾ãã¦ãæ¯å False ã¨è¨å®ãç¶ãããã¨ã«ãã£ã¦ãchanges ãçºçãã¦ããã¨ã®ãã¨ã§ããã
åå é¨åï¼
対å¿åªå 度ã®æ票ã¯ãThumbs Up ã®çµµæåãªã¢ã¯ã·ã§ã³æ°ãææ¨ã«ãã¦ããããã§ãã è¯ããã°æ票ããé¡ããã¾ãã
æç¥¨ï¼ æ票ãªã³ã¯ï¼https://github.com/hashicorp/terraform-provider-aws/issues/38578#issue-2435162470
6. AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ã§æåã§ãå ã®ã¯ã©ã¹ã¿ã¼ãåé¤ï¼Terraform å¤ã®ä½æ¥ï¼
æå¾ã«å
ã®ã¤ã³ã¹ã¿ã³ã¹ãå
ã®ã¯ã©ã¹ã¿ã¼ã®é ã« AWS ããã¸ã¡ã³ãã³ã³ã½ã¼ã«ãã Aurora ã¯ã©ã¹ã¿ã¼ãåé¤ãã¾ãã
Secret Manager ã®ã·ã¼ã¯ã¬ãããå¿ããã«åé¤ãã¾ãããã
ã¾ã¨ã
Terraform ã å©ç¨ãã¦ãAurora ã¯ã©ã¹ã¿ã¼ããªã¹ãã¢ãããã¨ãã¨ã®ã³ã¼ã (tf) ããªã¹ãã¢ããã¤ã³ã¹ã¿ã³ã¹ã«å¯¾å¿ä»ãããã¨ãã§ãã¾ããã
å±±æ¬ å²ä¹ (è¨äºä¸è¦§)
ã«ã¹ã¿ãã¼ãµã¯ã»ã¹é¨ã®ã¤ã³ãã©ã¨ã³ã¸ãã¢ã
å±±ãèµ°ãã®ã趣å³ã§ãã