{"id":5008,"date":"2024-03-01T21:48:36","date_gmt":"2024-03-01T13:48:36","guid":{"rendered":"http:\/\/xinblog.ltd\/?p=5008"},"modified":"2024-03-04T20:57:22","modified_gmt":"2024-03-04T12:57:22","slug":"terraform%e7%9a%84%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=5008","title":{"rendered":"Terraform\u7684\u5b66\u4e60"},"content":{"rendered":"<p>\u9996\u5148\u662fTerraform\u7684\u5b89\u88c5\uff0c\u8fd9\u91cc\u9009\u62e9\u5229\u7528\u4e8c\u8fdb\u5236\u65b9\u5f0f\u8fdb\u884c\u5b89\u88c5<\/p>\n<p>\u4e0b\u8f7d\u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236\u5305<a href=\"https:\/\/developer.hashicorp.com\/terraform\/downloads\">https:\/\/developer.hashicorp.com\/terraform\/downloads<\/a><\/p>\n<p>\u7136\u540e\u76f4\u63a5\u5c06\u4e8c\u8fdb\u5236\u5305unzip\u4e4b\u540emv\u5230path\u8def\u5f84\u5373\u53ef<\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\u952e\u5165 terraform\u8fdb\u884c\u6821\u9a8c\u5b89\u88c5\u662f\u5426\u5b8c\u6210<\/p>\n<p>\u4e4b\u540e\u67e5\u770b\u5176\u63d0\u4f9b\u7684\u57fa\u672c\u4f7f\u7528\u6587\u6863<\/p>\n<p>\u4e5f\u5c31\u662f\u542f\u52a8\u4e00\u4e2aNginx docker\u5bb9\u5668\uff0c\u5176\u4e3b\u8981\u4ee3\u7801\u5b58\u5728\u4e8emain.tf\u4e2d<\/p>\n<table>\n<tr>\n<td>\n  terraform {<\/p>\n<p>required_providers {<\/p>\n<p>docker = {<\/p>\n<p>source\u00a0 =   &#8220;kreuzwerker\/docker&#8221;<\/p>\n<p>version = &#8220;~&gt; 3.0.1&#8221;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>provider &#8220;docker&#8221; {}<\/p>\n<p>resource &#8220;docker_image&#8221;   &#8220;nginx&#8221; {<\/p>\n<p>name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = &#8220;nginx&#8221;<\/p>\n<p>keep_locally = false<\/p>\n<p>}<\/p>\n<p>resource &#8220;docker_container&#8221;   &#8220;nginx&#8221; {<\/p>\n<p>image = docker_image.nginx.image_id<\/p>\n<p>name\u00a0 = &#8220;tutorial&#8221;<\/p>\n<p>ports {<\/p>\n<p>internal = 80<\/p>\n<p>external = 8000<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/table>\n<p>\u90a3\u4e48\u6211\u4eec\u7684\u4e3b\u8981\u7ed3\u6784\u4e5f\u5c31\u662f\u8fd9\u6837<\/p>\n<p>\u58f0\u660e\u4e00\u4e2a\u63d0\u4f9b\u5546\uff0c\u7136\u540e\u58f0\u660e\u591a\u91cd\u4f9d\u8d56\u7684resource<\/p>\n<p>\u5e76\u7ed9resource\u914d\u7f6e\u4e0d\u540c\u7684ports<\/p>\n<p>\u7ed9\u4eba\u7684\u611f\u89c9\u662f\u62bd\u8c61\u4e86\u5f88\u591a\u5c42\uff0c\u8bed\u6cd5\u7b80\u6d01\uff0c\u4f46\u9700\u8981\u7ecf\u5e38\u7ffb\u4ed6\u4eec\u7684\u8bed\u6cd5\u6587\u6863<\/p>\n<p>\u4e4b\u540e\u6700\u91cd\u8981\u7684\u662fterraform init\u548cterraform apply<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e0b\u5982\u4f55\u5229\u7528terraform\u6765\u542f\u52a8\u4e00\u4e2aAWS\u8d44\u6e90<\/p>\n<p>\u8fd8\u662f\u4ee5\u4e00\u4e2a\u5355\u4e00\u7684tf\u6587\u4ef6\u6765\u67e5\u770b\uff0c\u76f4\u63a5\u662fmain.tf<\/p>\n<table>\n<tr>\n<td>\n  terraform {<\/p>\n<p>required_providers {<\/p>\n<p>aws = {<\/p>\n<p>source\u00a0 =   &#8220;hashicorp\/aws&#8221;<\/p>\n<p>version = &#8220;~&gt; 4.16&#8221;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>required_version = &#8220;&gt;= 1.2.0&#8221;<\/p>\n<p>}<\/p>\n<p>provider &#8220;aws&#8221; {<\/p>\n<p>region\u00a0 = &#8220;us-west-2&#8221;<\/p>\n<p>}<\/p>\n<p>resource &#8220;aws_instance&#8221;   &#8220;app_server&#8221; {<\/p>\n<p>ami\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =   &#8220;ami-830c94e3&#8221;<\/p>\n<p>instance_type = &#8220;t2.micro&#8221;<\/p>\n<p>tags = {<\/p>\n<p>Name = &#8220;ExampleAppServerInstance&#8221;<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/table>\n<p>required_version \u58f0\u660e\u8981\u6c42\u7684terraform\u7684\u7248\u672c<\/p>\n<p>\u5728\u63d0\u4f9b\u5546\u4e2d\u58f0\u660e\u4e86\u8981\u6c42\u7684\u63d0\u4f9b\u5546\u662fAWS<\/p>\n<p>\u7136\u540e\u5728\u8d44\u6e90\u4e2d\u58f0\u660e\u4e86\u4e00\u4e2aEC2\u5b9e\u4f8b<\/p>\n<p>\u8d44\u6e90\u540e\u9762\u8ddf\u7740\u4e24\u4e2a\u5b57\u7b26\u4e32\u540d\u5b57\uff0c\u8d44\u6e90\u7c7b\u578b\uff0c\u8d44\u6e90\u540d\u79f0<\/p>\n<p>\u4e24\u8005\u5171\u540c\u6784\u6210\u4e86\u4e00\u4e2a\u552f\u4e00ID<\/p>\n<p>\u4e4b\u540e\u5c31\u53ef\u4ee5\u521d\u59cb\u5316\u76ee\u5f55 terraform init<\/p>\n<p>\u6700\u540eterraform apply<\/p>\n<p>\u4e4b\u540e\u5982\u679c\u6211\u4eec\u6539\u53d8\u4e86\u9879\u76ee\uff0c\u90a3\u4e48\u4f1a\u5728apply\u7684\u65f6\u5019\u8fdb\u884c\uff0c\u8fdb\u884c\u4fee\u6539\u3002\u5982\u679c\u9047\u5230\u4e86\u65e0\u6cd5\u4fee\u6539\u7684\u53c2\u6570\uff0c\u6bd4\u5982EC2\u7684AMI\uff0c\u90a3\u4e48\u4f1a\u5148\u6467\u6bc1\u65e7\u7684\u5b9e\u4f8b\u5728\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u3002<\/p>\n<p>\u4e4b\u540e\u7531\u4e8e\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5728Aws\u4e0a\u90e8\u7f72\u548c\u7ba1\u7406\u8d44\u6e90\uff0c\u6240\u4ee5\u9700\u8981\u67e5\u770bAWS \u8d44\u6e90\u63d0\u4f9b\u5546\u63d0\u4f9b\u7684\u4e00\u4e9b\u914d\u7f6e\u53c2\u6570<\/p>\n<p><a href=\"https:\/\/registry.terraform.io\/providers\/hashicorp\/aws\/latest\/docs\">Docs overview | hashicorp\/aws | Terraform | Terraform Registry<\/a><\/p>\n<p>\u5bf9\u4e8e\u5176\u4e2d\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5229\u7528variables.tf\u6765\u58f0\u660e\uff0c\u5728tfvars\u6587\u4ef6\u4e2d\u8d4b\u503c<\/p>\n<p>\u6bd4\u5982<\/p>\n<table>\n<tr>\n<td>\n  variable &#8220;instance_name&#8221; {<\/p>\n<p>description = &#8220;Value of the Name tag for the EC2 instance&#8221;<\/p>\n<p>type\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = string<\/p>\n<p>default\u00a0\u00a0\u00a0\u00a0 =   &#8220;ExampleAppServerInstance&#8221;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/table>\n<p>\u5e76\u5728resource\u4e2d\u58f0\u660e\u4f7f\u7528<\/p>\n<table>\n<tr>\n<td>\n  resource &#8220;aws_instance&#8221;   &#8220;app_server&#8221; {<\/p>\n<p>ami\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =   &#8220;ami-08d70e59c07c61a3a&#8221;<\/p>\n<p>instance_type = &#8220;t2.micro&#8221;<\/p>\n<p>tags = {<\/p>\n<p>&#8211;\u00a0\u00a0\u00a0   Name = &#8220;ExampleAppServerInstance&#8221;<\/p>\n<p>+\u00a0\u00a0\u00a0   Name = var.instance_name<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/table>\n<p>\u540c\u65f6\u8fd8\u652f\u6301\u5728\u5176\u4e2dapply\u7684\u65f6\u5019\u5229\u7528-var \u6765\u8fdb\u884c\u8986\u76d6<\/p>\n<p>terraform apply -var &#8220;instance_name=YetAnotherName&#8221;<\/p>\n<p>\u5728terraform\u4e2d\uff0c\u8fd8\u652f\u6301\u4fdd\u5b58\u8f93\u51fa\uff0c\u65b9\u4fbf\u6211\u4eec\u548c\u4e91\u4e0a\u8d44\u6e90\u8fdb\u884c\u6821\u9a8c<\/p>\n<p>\u6bd4\u5982\u6211\u4eec\u5e0c\u671b\u83b7\u53d6\u5230resource\u4e2d\u5e38\u89c1\u7684ec2\u4e3b\u673a\u7684public_ip\u548cid\uff0c\u90a3\u4e48\u53ef\u4ee5\u5982\u4e0b\u5b9a\u4e49output<\/p>\n<table>\n<tr>\n<td>\n  resource &#8220;aws_instance&#8221;   &#8220;app_server&#8221; {<\/p>\n<p>ami\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =   &#8220;ami-08d70e59c07c61a3a&#8221;<\/p>\n<p>instance_type = &#8220;t2.micro&#8221;<\/p>\n<p>tags = {<\/p>\n<p>Name = var.instance_name<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/table>\n<table>\n<tr>\n<td>\n  output &#8220;instance_id&#8221; {<\/p>\n<p>description = &#8220;ID of the EC2 instance&#8221;<\/p>\n<p>value\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =   aws_instance.app_server.id<\/p>\n<p>}<\/p>\n<p>output &#8220;instance_public_ip&#8221; {<\/p>\n<p>description = &#8220;Public IP address of the EC2 instance&#8221;<\/p>\n<p>value\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 =   aws_instance.app_server.public_ip<\/p>\n<p>}<\/td>\n<\/tr>\n<\/table>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u67e5\u770b\u4e00\u4e9bterraform\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<p>\u5728\u4e92\u8054\u7f51\u4e0a\uff0c\u5173\u4e8eterraform\u7684\u4f7f\u7528\u4e94\u82b1\u516b\u95e8\uff0c\u6545terraform\u7684\u4f7f\u7528\u6ca1\u6709\u56fa\u5b9a\u65b9\u5f0f\uff0c\u800c\u662f\u9002\u5408\u4e8e\u81ea\u5df1\u7684\u9879\u76ee\u548c\u56e2\u961f\u5373\u53ef\u3002<\/p>\n<p>\u5c31\u6bd4\u5982\u4e00\u4e2a\u5c0f\u9879\u76ee\u5c31\u53ef\u4ee5\u53ea\u5305\u542b mian.tf, variables.tf, README.md<\/p>\n<p>\u5176\u4e2d main.tf\u662f\u4e3b\u8981\u5165\u53e3\u7c7b\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u6dfb\u52a0\u6240\u6709\u8d44\u6e90\uff0c<\/p>\n<p>variables.tf\u00a0\u4e2d\u5b9a\u4e49<a href=\"https:\/\/spacelift.io\/blog\/how-to-use-terraform-variables\">\u53d8\u91cf<\/a>\uff0c\u5e76\u5728terraform.tfvars\u00a0\u4e2d\u4e3a\u5b83\u4eec\u8d4b\u503c<\/p>\n<p>\u540c\u65f6<\/p>\n<p>\u800c\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5c31\u9700\u8981\u5b9a\u4e49\u4e0d\u540c\u7684\u6a21\u5757\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u6a21\u5757\u5b9a\u4e49\u5176\u8f93\u5165\u548c\u8f93\u51fa\u6765\u8fdb\u884c\u8bb0\u5f55\u3002<\/p>\n<p>\u800c\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u7ba1\u7406\u4e0d\u540c\u7684\u73af\u5883\u7684\u65b9\u6cd5\u4e5f\u5343\u5947\u767e\u602a\u3002\u5e38\u89c1\u7684\u65b9\u5f0f\u6709\u4e3a\u4e0d\u540c\u7684\u73af\u5883\u914d\u7f6e\u4e0d\u540c\u7684\u6a21\u5757\uff0c\u6216\u8005\u6240\u6709\u7684\u914d\u7f6e\u5212\u5206\u6587\u4ef6\uff0c\u6839\u636e\u73af\u5883\u53d8\u91cf\u521d\u59cb\u5316\u4e0d\u540c\u7684\u73af\u5883\u914d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u662fTerraform\u7684\u5b89\u88c5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5008"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5008"}],"version-history":[{"count":1,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5008\/revisions"}],"predecessor-version":[{"id":5009,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5008\/revisions\/5009"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5008"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}