ããã«ã¡ã¯ï¼ã¤ã¼ã´ãªã§ãã
ä»æ¥ã¯Terraformã§ç®¡çããAWSãªã½ã¼ã¹ãæåã§ä½ãç´ãã¦ãã¾ã£ãå ´åã®å¯¾çæ¹æ³ãç´¹ä»ãããã¨æãã¾ãã
èæ¯
ä¸è¨ã®.tfãã¡ã¤ã«ã«ããexample_sgã¨ããã»ãã¥ãªãã£ã°ã«ã¼ããæåã§ä½ãç´ãã¦ãã¾ãã¾ããããã®ããããterraform applyããå®è¡ããã¨ãä¸è¨ã®åä½ã«ãªã£ã¦ãã¾ãã¾ãã
.tfstateãã¡ã¤ã«ã«å
ã®ã»ãã¥ãªãã£ã°ã«ã¼ãï¼ä½ãç´ãåã®ã»ãã¥ãªãã£ã°ã«ã¼ãï¼ã®IDãè¨è¼ãã¦ããã¾ãããæåã§ä½ãç´ããã»ãã¥ãªãã£ã°ã«ã¼ãã®IDã¯åå¨ãã¦ããªãã®ã§ãæ°è¦ã»ãã¥ãªãã£ã°ã«ã¼ããä½æããã¨ããã¢ã¯ã·ã§ã³ã«ãªã£ã¦ãã¾ããâPlan: 1 to add, 0 to change, 0 to destroy.
.tfãã¡ã¤ã«
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 3.27" } } } provider "aws" { region = "ap-northeast-1" } data "aws_caller_identity" "current" {} output "account_id" { value = data.aws_caller_identity.current.account_id } ï¼ï¼ï¼çç¥ï¼ï¼ï¼ resource "aws_security_group" "example_sg" { name = "example-sg" description = "example-sg" vpc_id = "vpc-07d12d2" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 53 to_port = 53 protocol = "udp" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "ExampleSG" } }
terraform planã®çµæ
# aws_security_group.example_sg will be created + resource "aws_security_group" "example_sg" { + arn = (known after apply) + description = "example-sg" + egress = [ + { + cidr_blocks = [ + "0.0.0.0/0", ï¼ï¼ï¼çç¥ï¼ï¼ï¼ ] + name = "example-sg" + name_prefix = (known after apply) + owner_id = (known after apply) + revoke_rules_on_delete = false + tags = { + "Name" = "ExampleSG" } + tags_all = { + "Name" = "ExampleSG" } + vpc_id = "vpc-07d12d2" } Plan: 1 to add, 0 to change, 0 to destroy.
大ã¾ããªä½æ¥ã®æµã
- æåã§åé¤ãããAWSãªã½ã¼ã¹ã.tfstateããåé¤ãã
- æåã§ä½ãç´ããAWSãªã½ã¼ã¹ãTerraformã«ã¤ã³ãã¼ããã
- .tfstateã確èªãã
æé
ããã«ä¸è¨ã®Importã³ãã³ããå®è¡ããã¨ã¨ã©ã¼ã«ãªãã¾ãã®ã§ãã¾ãã¯æåã§åé¤ãããAWSãªã½ã¼ã¹ï¼ä»åã¯ã»ãã¥ãªãã£ã°ã«ã¼ãï¼ãæ¢åã®.tfstateããåé¤ãã¾ãã
terraform import aws_security_group.example_sg sg-XXX
ã¨ã©ã¼å 容ï¼
â Error: Resource already managed by Terraform â â Terraform is already managing a remote object for â aws_security_group.example_sg. To import to this address you must first â remove the existing object from the state.
æåã§åé¤ãããAWSãªã½ã¼ã¹ã.tfstateããåé¤ãã
terraform state rm aws_security_group.example_sg
æåã§ä½ãç´ããAWSãªã½ã¼ã¹ãTerraformã«ã¤ã³ãã¼ããã
terraform import aws_security_group.example_sg sg-XXX aws_security_group.example_sg: Importing from ID "sg-XXX"... aws_security_group.example_sg: Import prepared! Prepared aws_security_group for import ï¼ï¼ï¼çç¥ï¼ï¼ï¼ Import successful!
.tfstateã確èªãã
.tfstateã確èªããã¨ãæ°è¦ã»ãã¥ãªãã£ã°ã«ã¼ã IDãè¨è¼ãã¦ããã¾ãã
以ä¸ã御ä¸èªãããã¨ããããã¾ããã
æ¬ç° ã¤ã¼ã´ãª (è¨äºä¸è¦§)
ã«ã¹ã¿ãã¼ãµã¯ã»ã¹é¨
ã»2024 Japan AWS Top Engineers (Security)
ã»AWS SAP, DOP, SCS, DBS, SAA, DVA, CLF
ã»Azure AZ-900
ã»EC-Council CCSE
趣å³ï¼æ¥æ¬å½å æ è¡(47é½éåºçå¶è¦)ã»ãã©ã¤ãã»é³æ¥½