{"id":671,"date":"2020-08-30T19:31:32","date_gmt":"2020-08-30T10:31:32","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/08\/30\/aws-terraform-sate"},"modified":"2022-10-28T22:35:44","modified_gmt":"2022-10-28T13:35:44","slug":"aws-terraform-sate","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/08\/30\/aws-terraform-sate\/","title":{"rendered":"AWS\u30ea\u30bd\u30fc\u30b9\u3092Terraform\u7ba1\u7406\u5bfe\u8c61\u306b\u300c\u8ffd\u52a0\u300d\u300c\u524a\u9664\u300d\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"

\u7b48\u4e95\u3067\u3059\u3002<\/p>\n

\u7530\u820e\u66ae\u3089\u3057\u00d7\u30ea\u30e2\u30fc\u30c8\u30ef\u30fc\u30af<\/code> \u3092\u5b9f\u8df5\u3057\u30661\u30f6\u6708\u304c\u7d4c\u3061\u307e\u3057\u305f\u3002<\/p>\n

\u8c4a\u304b\u306a\u81ea\u7136\u304c\u7e54\u308a\u306a\u3059\u7f8e\u3057\u3044\u98a8\u666f\u306b\u7652\u3084\u3055\u308c\u306a\u304c\u3089\u65e5\u3005\u4ed5\u4e8b\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u3055\u3066\u3001MMM\u3067\u306f\u904b\u7528\u3059\u308bAWS\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306e\u69cb\u6210\u7ba1\u7406\u306bTerraform\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u904b\u7528\u306e\u4e2d\u3067\u3001\u624b\u52d5\u3067\u4f5c\u6210\u3057\u305f\u30a4\u30f3\u30d5\u30e9\u3092Terraform\u306e\u30b3\u30fc\u30c9\u7ba1\u7406\u4e0b\u306b\u5408\u6d41\u3055\u305b\u305f\u308a\u3001
\n\u9006\u306b\u7ba1\u7406\u4e0b\u304b\u3089\u5916\u3057\u305f\u308a\u3059\u308b\u5834\u9762\u3082\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u4eca\u56de\u306f\u3053\u3061\u3089\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n

AWS\u30ea\u30bd\u30fc\u30b9\u3092\u7ba1\u7406\u3059\u308bTerraform\u306e\u72b6\u614b\u3092\u78ba\u8a8d<\/h2>\n

Terraform\u3067\u4f5c\u6210\u3057\u305fAWS\u30ea\u30bd\u30fc\u30b9\u306b\u95a2\u3059\u308b\u69cb\u6210\u7ba1\u7406\u60c5\u5831\u306f terraform.tfstate<\/code> \u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u306b\u8a18\u9332\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u3053\u306e\u69cb\u6210\u7ba1\u7406\u60c5\u5831\u3092\u53c2\u7167\u3059\u308b\u306b\u306f terraform state list<\/code> \u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u4e0b\u8a18\u5b9f\u884c\u4f8b\u3067\u3059\u3002<\/p>\n

# \u4e00\u89a7\u8868\u793a\n$ terraform state list\n\ndata.aws_acm_certificate.sample\naws_ecs_cluster.sample\naws_ecs_service.sample\naws_ecs_task_definition.sample\n\u30fb\n\u30fb\n\u30fb\naws_vpc.sample<\/code><\/pre>\n

\u203b terraform destroy<\/code> \u3067\u30ea\u30bd\u30fc\u30b9\u3092\u524a\u9664\u3057\u3066\u3044\u305f\u5834\u5408\u3001 terraform state list<\/code> \u30b3\u30de\u30f3\u30c9\u306e\u5b9f\u884c\u7d50\u679c\u306b\u306f\u8868\u793a\u3055\u308c\u307e\u305b\u3093<\/p>\n

\u3061\u306a\u307f\u306b terraform state show <\u30ea\u30bd\u30fc\u30b9\u30bf\u30a4\u30d7>.<\u30ea\u30bd\u30fc\u30b9\u540d><\/code> \u3068\u5b9f\u884c\u3059\u308b\u3068\u3001
\n\u7279\u5b9a\u306e\u30ea\u30bd\u30fc\u30b9\u306e\u8a73\u7d30\u3092\u78ba\u8a8d\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n

<\u30ea\u30bd\u30fc\u30b9\u30bf\u30a4\u30d7>.<\u30ea\u30bd\u30fc\u30b9\u540d><\/code> \u306e\u7b87\u6240\u306f terraform state list<\/code> \u30b3\u30de\u30f3\u30c9\u306e\u51fa\u529b\u7d50\u679c\u3092\u305d\u306e\u307e\u307e\u4f7f\u7528\u3059\u308c\u3070OK\u3067\u3059\u3002<\/p>\n

\u4f8b\u3048\u3070 aws_ecs_cluster.sample<\/code> \u306e\u5834\u5408\u3001aws_ecs_cluster<\/code> \u304c <\u30ea\u30bd\u30fc\u30b9\u30bf\u30a4\u30d7><\/code> \u306b\u3001
\nsample<\/code> \u304c <\u30ea\u30bd\u30fc\u30b9\u540d><\/code> \u306b\u76f8\u5f53\u3057\u307e\u3059\u3002<\/p>\n

\u4e0b\u8a18\u5b9f\u884c\u4f8b\u3067\u3059\u3002<\/p>\n

# \u72b6\u614b\u78ba\u8a8d\n$ terraform state show aws_ecs_cluster.stg\n\n# aws_ecs_cluster.sample:\nresource "aws_ecs_cluster" "sample" {\n    arn                = "arn:aws:ecs:ap-northeast-1:************:cluster\/sample-cluster"\n    capacity_providers = []\n    id                 = "arn:aws:ecs:ap-northeast-1:************:cluster\/sample-cluster"\n    name               = "sample-cluster"\n    tags               = {}\n\n    setting {\n        name  = "containerInsights"\n        value = "disabled"\n    }\n}<\/code><\/pre>\n

Terraform\u7ba1\u7406\u4e0b\u304b\u3089\u7279\u5b9a\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u5916\u3059<\/h2>\n

\u4f5c\u6210\u6e08\u307f\u306eAWS\u30ea\u30bd\u30fc\u30b9\u30fb\u524a\u9664\u6e08\u307f\u306eAWS\u30ea\u30bd\u30fc\u30b9\u3092\u3001Terraform\u7ba1\u7406\u4e0b\u304b\u3089\u5916\u3059\u5834\u5408\u3001 terraform state rm<\/code> \u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n

$ terraform state rm <\u30ea\u30bd\u30fc\u30b9\u30bf\u30a4\u30d7>.<\u30ea\u30bd\u30fc\u30b9\u540d><\/code><\/pre>\n

\u4e0b\u8a18\u5b9f\u884c\u4f8b\u3067\u3059\u3002<\/p>\n

$ terraform state rm aws_ecs_cluster.sample\n\nRemoved aws_ecs_cluster.sample\nSuccessfully removed 1 resource instance(s).<\/code><\/pre>\n

Terraform\u7ba1\u7406\u4e0b\u306b\u65e2\u5b58\u306eAWS\u30ea\u30bd\u30fc\u30b9\u3092\u8ffd\u52a0\u3059\u308b<\/h2>\n

Terraform\u306e\u7ba1\u7406\u5916\u3067\u4f5c\u6210\u3057\u305fAWS\u30ea\u30bd\u30fc\u30b9\u3092\u3001Terraform\u7ba1\u7406\u4e0b\u306b\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u3001
\nterraform import<\/code> \u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n

\u307e\u305a\u306fTerraform\u30b3\u30fc\u30c9\u306b\u30ea\u30bd\u30fc\u30b9\u30d6\u30ed\u30c3\u30af\u3092\u8a18\u8ff0\u3057\u3001Terraform\u304c\u4eba\u6c17\u3059\u308b\u540d\u524d\u3092\u8a2d\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u305f\u3068\u3048\u3070\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30b0\u30eb\u30fc\u30d7\u3092\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n

resource "aws_security_group" "sample" {\n  # ...instance configuration...\n}<\/code><\/pre>\n

\u6b21\u306b\u30a4\u30f3\u30dd\u30fc\u30c8\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002AWS\u30b3\u30f3\u30bd\u30fc\u30eb\u304b\u3089\u5bfe\u8c61\u30ea\u30bd\u30fc\u30b9\u306eID\u3092\u78ba\u8a8d\u3057\u3001
\n\u4e0b\u8a18\u306e\u3088\u3046\u306b\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n

$ terraform import aws_security_group.sample sg-0eed97aa6********\n\naws_security_group.sample: Importing from ID "sg-0eed97aa6********"...\naws_security_group.sample: Import prepared!\n  Prepared aws_security_group for import\n  Prepared aws_security_group_rule for import\n  Prepared aws_security_group_rule for import\naws_security_group_rule.sample: Refreshing state... [id=sgrule-**********]\naws_security_group.sample: Refreshing state... [id=sg-0eed97aa6********]\naws_security_group_rule.sample-1: Refreshing state... [id=sgrule-**********]\n\nImport successful!\n\nThe resources that were imported are shown above. These resources are now in\nyour Terraform state and will henceforth be managed by Terraform.<\/code><\/pre>\n

terraform state<\/code> \u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001aws_security_group.sample<\/code> \u304c\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\u3055\u308c\u3066\u3044\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u307e\u3068\u3081<\/h2>\n

AWS\u30ea\u30bd\u30fc\u30b9\u3092Terraform\u306e\u7ba1\u7406\u4e0b\u306b\u8ffd\u52a0\u30fb\u524a\u9664\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3057\u305f\u3002<\/p>\n

\u4e0a\u8a18\u306e\u624b\u6cd5\u3092\u99c6\u4f7f\u3059\u308b\u3068\u3001AWS\u30b3\u30f3\u30bd\u30fc\u30eb\u4e0a\u3067\u624b\u52d5\u4f5c\u6210\u3057\u305f\u65e2\u5b58\u30a4\u30f3\u30d5\u30e9\u3092Terraform\u306b\u3088\u308b\u30b3\u30fc\u30c9\u7ba1\u7406\u306b\u5408\u6d41\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\n\u307e\u305f\u30011\u3064\u306e\u30b7\u30b9\u30c6\u30e0\u306e\u672c\u756a\u7a3c\u50cd\u74b0\u5883\u3068\u691c\u8a3c\u7528\u74b0\u5883\u3092\u3001\u5225\u3005\u306eTerraform\u30b3\u30fc\u30c9\u30ea\u30dd\u30b8\u30c8\u30ea\u3067\u7ba1\u7406\u3059\u308b\u3088\u3046\u306a\u5909\u66f4\u3092\u52a0\u3048\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n

\u4ee5\u4e0a\u3001\u306a\u306b\u304b\u306e\u53c2\u8003\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7b48\u4e95\u3067\u3059\u3002 \u7530\u820e\u66ae\u3089\u3057\u00d7\u30ea\u30e2\u30fc\u30c8\u30ef\u30fc\u30af \u3092\u5b9f\u8df5\u3057\u30661\u30f6\u6708\u304c\u7d4c\u3061\u307e\u3057\u305f\u3002 \u8c4a\u304b\u306a\u81ea\u7136\u304c\u7e54\u308a\u306a\u3059\u7f8e\u3057\u3044\u98a8\u666f\u306b\u7652\u3084\u3055\u308c\u306a\u304c\u3089\u65e5\u3005\u4ed5\u4e8b\u3092\u3057\u3066\u3044\u307e\u3059\u3002 \u3055\u3066\u3001MMM\u3067\u306f\u904b\u7528\u3059\u308bAWS\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306e\u69cb\u6210\u7ba1\u7406\u306bTerraform\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002 \u904b\u7528\u306e\u4e2d\u3067\u3001\u624b\u52d5\u3067\u4f5c\u6210\u3057\u305f\u30a4\u30f3\u30d5\u30e9\u3092Terraform\u306e\u30b3\u30fc\u30c9\u7ba1\u7406\u4e0b\u306b\u5408\u6d41\u3055\u305b\u305f\u308a\u3001 \u9006\u306b\u7ba1\u7406\u4e0b\u304b\u3089\u5916\u3057\u305f\u308a\u3059\u308b\u5834\u9762\u3082\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u4eca\u56de\u306f\u3053\u3061\u3089\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057 […]<\/p>\n","protected":false},"author":9,"featured_media":684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[384,399],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/671"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=671"}],"version-history":[{"count":1,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/671\/revisions"}],"predecessor-version":[{"id":1099,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/671\/revisions\/1099"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}