{"id":35018,"date":"2024-10-01T09:39:31","date_gmt":"2024-10-01T02:39:31","guid":{"rendered":"http:\/\/jupitek.maudemo.vip\/index.php\/2024\/10\/01\/use-terraform-to-provision-infrastructure-on-linode\/"},"modified":"2024-10-01T09:39:31","modified_gmt":"2024-10-01T02:39:31","slug":"use-terraform-to-provision-infrastructure-on-linode","status":"publish","type":"post","link":"https:\/\/jupitek.maudemo.vip\/index.php\/2024\/10\/01\/use-terraform-to-provision-infrastructure-on-linode\/","title":{"rendered":"D\u00f9ng Terraform \u0111\u1ec3 cung c\u1ea5p h\u1ea1 t\u1ea7ng tr\u00ean Linode"},"content":{"rendered":"<p>C\u01a1 s\u1edf h\u1ea1 t\u1ea7ng d\u01b0\u1edbi d\u1ea1ng m\u00e3 (IaC) l\u00e0 m\u1ed9t ph\u01b0\u01a1ng ph\u00e1p ph\u00e1t tri\u1ec3n v\u00e0 v\u1eadn h\u00e0nh cho ph\u00e9p tri\u1ec3n khai m\u00e1y ch\u1ee7 v\u00e0 c\u1ea5u h\u00ecnh ph\u1ea7n m\u1ec1m \u0111\u01b0\u1ee3c th\u1ec3 hi\u1ec7n d\u01b0\u1edbi d\u1ea1ng m\u00e3. Ph\u01b0\u01a1ng ph\u00e1p n\u00e0y l\u00e0m gi\u1ea3m kh\u1ea3 n\u0103ng x\u1ea3y ra l\u1ed7i c\u1ee7a con ng\u01b0\u1eddi, gi\u00fap c\u00e1c h\u1ec7 th\u1ed1ng ph\u1ee9c t\u1ea1p d\u1ec5 qu\u1ea3n l\u00fd h\u01a1n, t\u1ea1o \u0111i\u1ec1u ki\u1ec7n thu\u1eadn l\u1ee3i cho s\u1ef1 h\u1ee3p t\u00e1c trong c\u00e1c d\u1ef1 \u00e1n k\u1ef9 thu\u1eadt h\u1ec7 th\u1ed1ng v\u00e0 mang l\u1ea1i m\u1ed9t s\u1ed1 l\u1ee3i \u00edch kh\u00e1c.<\/p>\n<p>Terraform l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 IaC t\u1eadp trung v\u00e0o vi\u1ec7c t\u1ea1o, s\u1eeda \u0111\u1ed5i v\u00e0 h\u1ee7y m\u00e1y ch\u1ee7, thay v\u00ec qu\u1ea3n l\u00fd ph\u1ea7n m\u1ec1m tr\u00ean c\u00e1c m\u00e1y ch\u1ee7 \u0111\u00f3. Terraform cung c\u1ea5p c\u00e1c plugin \u0111\u1ec3 giao ti\u1ebfp v\u1edbi c\u00e1c nh\u00e0 cung c\u1ea5p d\u1ecbch v\u1ee5 l\u01b0u tr\u1eef kh\u00e1c nhau v\u00e0 c\u00f3 s\u1eb5n&nbsp;<a href=\"https:\/\/www.terraform.io\/docs\/providers\/linode\/index.html\" target=\"_blank\" rel=\"noreferrer noopener\">plugin Linode ch\u00ednh th\u1ee9c<\/a>&nbsp;. H\u01b0\u1edbng d\u1eabn n\u00e0y s\u1ebd ch\u1ec9 cho b\u1ea1n c\u00e1ch b\u1eaft \u0111\u1ea7u v\u1edbi Terraform v\u00e0 Linode.<\/p>\n<p>C\u00e1c Linode \u0111\u01b0\u1ee3c t\u1ea1o b\u1eb1ng Terraform c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c c\u1ea5u h\u00ecnh th\u00eam b\u1eb1ng c\u00e1c h\u1ec7 th\u1ed1ng container nh\u01b0 Docker ho\u1eb7c b\u1eb1ng ph\u1ea7n m\u1ec1m qu\u1ea3n l\u00fd c\u1ea5u h\u00ecnh nh\u01b0 Salt, Puppet, Ansible ho\u1eb7c Chef.<\/p>\n<p class=\"has-background\" style=\"background-color:#f32a2a33\">Quan tr\u1ecdng: C\u1ea5u h\u00ecnh v\u00e0 l\u1ec7nh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong h\u01b0\u1edbng d\u1eabn n\u00e0y d\u1eabn \u0111\u1ebfn vi\u1ec7c nhi\u1ec1u Linode \u0111\u01b0\u1ee3c th\u00eam v\u00e0o t\u00e0i kho\u1ea3n c\u1ee7a b\u1ea1n. H\u00e3y \u0111\u1ea3m b\u1ea3o theo d\u00f5i ch\u1eb7t ch\u1ebd t\u00e0i kho\u1ea3n c\u1ee7a b\u1ea1n trong Linode Manager \u0111\u1ec3 tr\u00e1nh c\u00e1c kho\u1ea3n ph\u00ed kh\u00f4ng mong mu\u1ed1n.<\/p>\n<h2 id=\"before-you-begin\">Tr\u01b0\u1edbc khi b\u1ea1n b\u1eaft \u0111\u1ea7u<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#before-you-begin\"><\/a><\/h2>\n<ul>\n<li>H\u01b0\u1edbng d\u1eabn n\u00e0y ch\u1ec9 cho b\u1ea1n c\u00e1ch c\u00e0i \u0111\u1eb7t v\u00e0 s\u1eed d\u1ee5ng ph\u1ea7n m\u1ec1m m\u00e1y kh\u00e1ch Terraform t\u1eeb h\u1ec7 th\u1ed1ng Linux. Terraform c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c c\u00e0i \u0111\u1eb7t tr\u00ean c\u00e1c h\u1ec7 \u0111i\u1ec1u h\u00e0nh kh\u00e1c v\u00e0 h\u01b0\u1edbng d\u1eabn cho c\u00e1c n\u1ec1n t\u1ea3ng \u0111\u00f3 t\u01b0\u01a1ng t\u1ef1 nh\u01b0 c\u00e1c l\u1ec7nh \u0111\u01b0\u1ee3c tr\u00ecnh b\u00e0y trong h\u01b0\u1edbng d\u1eabn n\u00e0y.<\/li>\n<li>B\u1ea1n c\u1ea7n m\u00e3 th\u00f4ng b\u00e1o truy c\u1eadp c\u00e1 nh\u00e2n cho&nbsp;<a href=\"https:\/\/www.linode.com\/docs\/api\/\">API v4<\/a>&nbsp;c\u1ee7a Linode \u0111\u1ec3 s\u1eed d\u1ee5ng v\u1edbi Terraform. L\u00e0m theo h\u01b0\u1edbng d\u1eabn&nbsp;<a href=\"https:\/\/www.linode.com\/docs\/products\/tools\/api\/get-started\/#get-an-access-token\">B\u1eaft \u0111\u1ea7u v\u1edbi API Linode<\/a>&nbsp;\u0111\u1ec3 nh\u1eadn m\u00e3 th\u00f4ng b\u00e1o.<\/li>\n<\/ul>\n<p class=\"has-background\" style=\"background-color:#74f78c33\">Ghi ch\u00fa: B\u1ea5t k\u1ef3 M\u00e3 th\u00f4ng b\u00e1o truy c\u1eadp c\u00e1 nh\u00e2n n\u00e0o \u0111\u01b0\u1ee3c t\u1ea1o t\u1eeb Linode Manager tr\u01b0\u1edbc \u0111\u00f3 \u0111\u1ec1u l\u00e0 m\u00e3 th\u00f4ng b\u00e1o API v3 v\u00e0 kh\u00f4ng ho\u1ea1t \u0111\u1ed9ng v\u1edbi nh\u00e0 cung c\u1ea5p Linode c\u1ee7a Terraform.<\/p>\n<h2 id=\"install-terraform\">C\u00e0i \u0111\u1eb7t Terraform<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#install-terraform\"><\/a><\/h2>\n<p>1.\u0110\u1ec3 c\u00e0i \u0111\u1eb7t phi\u00ean b\u1ea3n Terraform m\u1edbi nh\u1ea5t tr\u00ean nhi\u1ec1u b\u1ea3n ph\u00e2n ph\u1ed1i Linux v\u00e0 macOS kh\u00e1c nhau, h\u00e3y s\u1eed d\u1ee5ng c\u00e1c l\u1ec7nh sau:<\/p>\n<p><strong>Ubuntu\/Debian<\/strong>&nbsp;:<\/p>\n<pre class=\"wp-block-code\"><code>wget -O- https:\/\/apt.releases.hashicorp.com\/gpg | sudo gpg --dearmor -o \/usr\/share\/keyrings\/hashicorp-archive-keyring.gpg\necho \"deb &#91;signed-by=\/usr\/share\/keyrings\/hashicorp-archive-keyring.gpg] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main\" | sudo tee \/etc\/apt\/sources.list.d\/hashicorp.list\nsudo apt update &amp;&amp; sudo apt install terraform<\/code><\/pre>\n<p><strong>CentOS\/RHEL<\/strong>&nbsp;:<\/p>\n<pre class=\"wp-block-code\"><code>sudo yum install -y yum-utils\nsudo yum-config-manager --add-repo https:\/\/rpm.releases.hashicorp.com\/RHEL\/hashicorp.repo\nsudo yum -y install terraform<\/code><\/pre>\n<p><strong>Fedora<\/strong>:<\/p>\n<pre class=\"wp-block-code\"><code>sudo dnf install -y dnf-plugins-core\nsudo dnf config-manager --add-repo https:\/\/rpm.releases.hashicorp.com\/fedora\/hashicorp.repo\nsudo dnf -y install terraform<\/code><\/pre>\n<p><strong>macOS<\/strong>&nbsp;:<\/p>\n<pre class=\"wp-block-code\"><code>brew tap hashicorp\/tap\nbrew install hashicorp\/tap\/terraform<\/code><\/pre>\n<p>2.X\u00e1c minh Terraform c\u00f3 th\u1ec3 ch\u1ea1y b\u1eb1ng c\u00e1ch ch\u1ec9 c\u1ea7n g\u1ecdi n\u00f3 m\u00e0 kh\u00f4ng c\u00f3 t\u00f9y ch\u1ecdn ho\u1eb7c \u0111\u1ed1i s\u1ed1:<\/p>\n<pre class=\"wp-block-code\"><code>terraform<\/code><\/pre>\n<pre class=\"wp-block-code\"><code>Usage: terraform &#91;-version] &#91;-help] &lt;command&gt; &#91;args]\n\nThe available commands for execution are listed below. The most common, useful commands are shown first, followed by less common or more advanced commands. If you're just getting started with Terraform, stick with the common commands. For the other commands, please read the help and docs before usage.\n\nCommon commands:\n    apply              Builds or changes infrastructure\n    console            Interactive console for Terraform interpolations\n    destroy            Destroy Terraform-managed infrastructure\n    env                Workspace management\n    fmt                Rewrites config files to canonical format\n    get                Download and install modules for the configuration\n    graph              Create a visual graph of Terraform resources\n    import             Import existing infrastructure into Terraform\n    init               Initialize a Terraform working directory\n    output             Read an output from a state file\n    plan               Generate and show an execution plan\n    providers          Prints a tree of the providers used in the configuration\n    push               Upload this Terraform module to Atlas to run\n    refresh            Update local state file against real resources\n    show               Inspect Terraform state or plan\n    taint              Manually mark a resource for recreation\n    untaint            Manually unmark a resource as tainted\n    validate           Validates the Terraform files\n    version            Prints the Terraform version\n    workspace          Workspace management\n\nAll other commands:\n    debug              Debug output management (experimental)\n    force-unlock       Manually unlock the terraform state\n    state              Advanced state management<\/code><\/pre>\n<h2 id=\"building-with-the-linode-provider\">X\u00e2y d\u1ef1ng v\u1edbi Nh\u00e0 cung c\u1ea5p Linode<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#building-with-the-linode-provider\"><\/a><\/h2>\n<p>Terraform s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p khai b\u00e1o trong \u0111\u00f3 c\u00e1c t\u1ec7p c\u1ea5u h\u00ecnh ch\u1ec9 \u0111\u1ecbnh tr\u1ea1ng th\u00e1i k\u1ebft th\u00fac mong mu\u1ed1n c\u1ee7a c\u01a1 s\u1edf h\u1ea1 t\u1ea7ng, do \u0111\u00f3 c\u00e1c v\u00ed d\u1ee5 trong h\u01b0\u1edbng d\u1eabn n\u00e0y s\u1ebd ch\u1ec9 li\u1ec7t k\u00ea c\u00e1c Linode m\u00e0 ch\u00fang ta mu\u1ed1n t\u1ea1o. Terraform c\u00f3 th\u1ec3 hi\u1ec3u hai lo\u1ea1i t\u1ec7p c\u1ea5u h\u00ecnh: JSON v\u00e0&nbsp;<a href=\"https:\/\/github.com\/hashicorp\/hcl\" target=\"_blank\" rel=\"noreferrer noopener\">Ng\u00f4n ng\u1eef c\u1ea5u h\u00ecnh HashiCorp<\/a>&nbsp;(HCL). H\u01b0\u1edbng d\u1eabn n\u00e0y s\u1eed d\u1ee5ng \u0111\u1ecbnh d\u1ea1ng HCL v\u00e0 c\u00e1c t\u1ec7p HCL k\u1ebft th\u00fac b\u1eb1ng&nbsp;<code>.tf<\/code>ph\u1ea7n m\u1edf r\u1ed9ng.<\/p>\n<p>1.T\u1ea1o t\u1ec7p&nbsp;<code>linode-terraform-web.tf<\/code>trong&nbsp;<code>~\/terraform<\/code>th\u01b0 m\u1ee5c c\u1ee7a b\u1ea1n v\u1edbi \u0111o\u1ea1n m\u00e3 b\u00ean d\u01b0\u1edbi. \u0110i\u1ec1n m\u00e3 th\u00f4ng b\u00e1o API Linode, kh\u00f3a SSH c\u00f4ng khai v\u00e0 m\u1eadt kh\u1ea9u g\u1ed1c mong mu\u1ed1n v\u00e0o n\u01a1i \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh. Ngo\u00e0i ra, h\u00e3y thay th\u1ebf nh\u00e0 cung c\u1ea5p Linode&nbsp;<code>version<\/code>th\u00e0nh&nbsp;<a href=\"https:\/\/registry.terraform.io\/providers\/linode\/linode\/\" target=\"_blank\" rel=\"noreferrer noopener\">m\u1edbi nh\u1ea5t<\/a>&nbsp;:<\/p>\n<pre class=\"wp-block-code\"><code>terraform {\n  required_providers {\n    linode = {\n      source = \"linode\/linode\"\n      version = \"2.5.2\"\n    }\n  }\n}\n\nprovider \"linode\" {\n  token = \"YOUR_LINODE_API_TOKEN\"\n}\n\nresource \"linode_instance\" \"terraform-web\" {\n        image = \"linode\/ubuntu18.04\"\n        label = \"Terraform-Web-Example\"\n        group = \"Terraform\"\n        region = \"us-east\"\n        type = \"g6-standard-1\"\n        authorized_keys = &#91; \"YOUR_PUBLIC_SSH_KEY\" ]\n        root_pass = \"YOUR_ROOT_PASSWORD\"\n}<\/code><\/pre>\n<p>\u0110o\u1ea1n m\u00e3 n\u00e0y t\u1ea1o ra m\u1ed9t Linode 2GB \u0111\u01b0\u1ee3c g\u1eafn nh\u00e3n&nbsp;<code>Terraform-Web-Example<\/code>trong&nbsp;<code>Terraform<\/code>nh\u00f3m Linodes. M\u1eb7c d\u00f9 ph\u1ea7n m\u1ec1m c\u1ee7a m\u00e1y ch\u1ee7 s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c c\u1ea5u h\u00ecnh trong h\u01b0\u1edbng d\u1eabn n\u00e0y, nh\u01b0ng ch\u00fang ta c\u00f3 th\u1ec3 h\u00ecnh dung r\u1eb1ng Linode ho\u1ea1t \u0111\u1ed9ng nh\u01b0 m\u1ed9t m\u00e1y ch\u1ee7 web.<\/p>\n<p>2.Kh\u1edfi t\u1ea1o c\u1ea5u h\u00ecnh Terraform:<\/p>\n<pre class=\"wp-block-code\"><code>terraform init<\/code><\/pre>\n<p>Terraform x\u00e1c nh\u1eadn kh\u1edfi t\u1ea1o th\u00e0nh c\u00f4ng:<\/p>\n<pre class=\"wp-block-code\"><code>Initializing provider plugins...\n- Checking for available provider plugins on https:\/\/releases.hashicorp.com...\n- Downloading plugin for provider \"linode\" (1.0.0)...\n\nThe following providers do not have any version constraints in configuration,\nso the latest version was installed.\n\nTo prevent automatic upgrades to new major versions that may contain breaking\nchanges, it is recommended to add version = \"...\" constraints to the\ncorresponding provider blocks in configuration, with the constraint strings\nsuggested below.\n\n* provider.linode: version = \"~&gt; 1.0\"\nTerraform has been successfully initialized!<\/code><\/pre>\n<p>Ghi ch\u00fa<\/p>\n<p>N\u1ebfu x\u1ea3y ra l\u1ed7i, h\u00e3y ch\u1ea1y l\u1ea1i l\u1ec7nh \u1edf ch\u1ebf \u0111\u1ed9 g\u1ee1 l\u1ed7i:<\/p>\n<pre class=\"wp-block-code\"><code>TF_LOG<strong>=<\/strong>debug terraform init<\/code><\/pre>\n<p>3.Ch\u1ea1y&nbsp;l\u1ec7nh&nbsp;<a href=\"https:\/\/www.terraform.io\/docs\/commands\/plan.html\" target=\"_blank\" rel=\"noreferrer noopener\">plan c\u1ee7a Terraform:<\/a><\/p>\n<pre class=\"wp-block-code\"><code>terraform plan<\/code><\/pre>\n<pre class=\"wp-block-code\"><code>Refreshing Terraform state in-memory prior to plan...\nThe refreshed state will be used to calculate this plan, but will not be\npersisted to local or remote state storage.\n\n\n------------------------------------------------------------------------\n\nAn execution plan has been generated and is shown below.\nResource actions are indicated with the following symbols:\n  + create\n\nTerraform will perform the following actions:\n\n  + linode_instance.terraform-web\n      id:                 &lt;computed&gt;\n      alerts.#:           &lt;computed&gt;\n      authorized_keys.#:  \"1\"\n      authorized_keys.0:  \"ssh-rsa ...\"\n      backups.#:          &lt;computed&gt;\n      backups_enabled:    &lt;computed&gt;\n      boot_config_label:  &lt;computed&gt;\n      group:              \"Terraform\"\n      image:              \"linode\/ubuntu18.04\"\n      ip_address:         &lt;computed&gt;\n      ipv4.#:             &lt;computed&gt;\n      ipv6:               &lt;computed&gt;\n      label:              \"web\"\n      private_ip_address: &lt;computed&gt;\n      region:             \"us-east\"\n      root_pass:          &lt;sensitive&gt;\n      specs.#:            &lt;computed&gt;\n      status:             &lt;computed&gt;\n      swap_size:          &lt;computed&gt;\n      type:               \"g6-standard-1\"\n      watchdog_enabled:   \"true\"\n\n\nPlan: 1 to add, 0 to change, 0 to destroy.\n\n------------------------------------------------------------------------\n\nNote: You didn't specify an \"-out\" parameter to save this plan, so Terraform\ncan't guarantee that exactly these actions will be performed if\n\"terraform apply\" is subsequently run.<\/code><\/pre>\n<p><code>terraform plan<\/code>s\u1ebd kh\u00f4ng th\u1ef1c hi\u1ec7n b\u1ea5t k\u1ef3 h\u00e0nh \u0111\u1ed9ng n\u00e0o ho\u1eb7c th\u1ef1c hi\u1ec7n b\u1ea5t k\u1ef3 thay \u0111\u1ed5i n\u00e0o tr\u00ean t\u00e0i kho\u1ea3n Linode c\u1ee7a b\u1ea1n. Thay v\u00e0o \u0111\u00f3, m\u1ed9t ph\u00e2n t\u00edch \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh h\u00e0nh \u0111\u1ed9ng n\u00e0o (t\u1ee9c l\u00e0 t\u1ea1o, x\u00f3a ho\u1eb7c s\u1eeda \u0111\u1ed5i phi\u00ean b\u1ea3n Linode) l\u00e0 c\u1ea7n thi\u1ebft \u0111\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c tr\u1ea1ng th\u00e1i \u0111\u01b0\u1ee3c m\u00f4 t\u1ea3 trong c\u1ea5u h\u00ecnh c\u1ee7a b\u1ea1n.<\/p>\n<p>Ghi ch\u00fa<\/p>\n<p>Ch\u1ebf \u0111\u1ed9 g\u1ee1 l\u1ed7i c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng cho l\u1ec7nh plan n\u1ebfu b\u1ea1n c\u1ea7n th\u1ef1c hi\u1ec7n kh\u1eafc ph\u1ee5c s\u1ef1 c\u1ed1:<\/p>\n<pre class=\"wp-block-code\"><code>TF_LOG<strong>=<\/strong>debug terraform plan<\/code><\/pre>\n<p>4.N\u1ebfu kh\u00f4ng c\u00f3 l\u1ed7i, h\u00e3y b\u1eaft \u0111\u1ea7u tri\u1ec3n khai:<\/p>\n<pre class=\"wp-block-code\"><code>terraform apply<\/code><\/pre>\n<p>B\u1ea1n \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u x\u00e1c nh\u1eadn h\u00e0nh \u0111\u1ed9ng. Nh\u1eadp&nbsp;<code>yes<\/code>v\u00e0 nh\u1ea5n&nbsp;<strong>Enter<\/strong>&nbsp;:<\/p>\n<pre class=\"wp-block-code\"><code>Do you want to perform these actions?\n  Terraform will perform the actions described above.\n  Only 'yes' will be accepted to approve.\n\n  Enter a value: yes\n\nlinode_instance.terraform-web: Creating...\n  alerts.#:           \"\" =&gt; \"&lt;computed&gt;\"\n  authorized_keys.#:  \"\" =&gt; \"1\"\n  authorized_keys.0:  \"\" =&gt; \"ssh-rsa ...\"\n  backups.#:          \"\" =&gt; \"&lt;computed&gt;\"\n  backups_enabled:    \"\" =&gt; \"&lt;computed&gt;\"\n  boot_config_label:  \"\" =&gt; \"&lt;computed&gt;\"\n  group:              \"\" =&gt; \"Terraform\"\n  image:              \"\" =&gt; \"linode\/ubuntu18.04\"\n  ip_address:         \"\" =&gt; \"&lt;computed&gt;\"\n  ipv4.#:             \"\" =&gt; \"&lt;computed&gt;\"\n  ipv6:               \"\" =&gt; \"&lt;computed&gt;\"\n  label:              \"\" =&gt; \"web\"\n  private_ip_address: \"\" =&gt; \"&lt;computed&gt;\"\n  region:             \"\" =&gt; \"us-east\"\n  root_pass:          \"&lt;sensitive&gt;\" =&gt; \"&lt;sensitive&gt;\"\n  specs.#:            \"\" =&gt; \"&lt;computed&gt;\"\n  status:             \"\" =&gt; \"&lt;computed&gt;\"\n  swap_size:          \"\" =&gt; \"&lt;computed&gt;\"\n  type:               \"\" =&gt; \"g6-standard-1\"\n  watchdog_enabled:   \"\" =&gt; \"true\"\nlinode_instance.terraform-web: Still creating... (10s elapsed)\nlinode_instance.terraform-web: Still creating... (20s elapsed)\nlinode_instance.terraform-web: Still creating... (30s elapsed)\nlinode_instance.terraform-web: Still creating... (40s elapsed)\nlinode_instance.terraform-web: Still creating... (50s elapsed)\nlinode_instance.terraform-web: Creation complete after 52s (ID: 10975739)\n\nApply complete! Resources: 1 added, 0 changed, 0 destroyed.<\/code><\/pre>\n<p>5.Truy c\u1eadp Tr\u00ecnh qu\u1ea3n l\u00fd Linode. B\u1ea1n s\u1ebd th\u1ea5y Linode&nbsp;<code>Terraform-Web-Example<\/code>\u0111\u00e3 \u0111\u01b0\u1ee3c th\u00eam v\u00e0o t\u00e0i kho\u1ea3n c\u1ee7a b\u1ea1n.<\/p>\n<h3 id=\"provision-additional-servers\">Cung c\u1ea5p th\u00eam m\u00e1y ch\u1ee7<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#provision-additional-servers\"><\/a><\/h3>\n<p>\u1ede b\u01b0\u1edbc tr\u01b0\u1edbc, b\u1ea1n \u0111\u00e3 s\u1eed d\u1ee5ng Terraform \u0111\u1ec3 cung c\u1ea5p m\u1ed9t Linode c\u00f3 th\u1ec3 ho\u1ea1t \u0111\u1ed9ng nh\u01b0 m\u1ed9t m\u00e1y ch\u1ee7 web. \u0110\u1ec3 minh h\u1ecda c\u00e1ch th\u00eam m\u1ed9t Linode kh\u00e1c th\u00f4ng qua Terraform, gi\u1ea3 s\u1eed b\u00e2y gi\u1edd b\u1ea1n c\u0169ng c\u1ea7n m\u1ed9t m\u00e1y ch\u1ee7 c\u01a1 s\u1edf d\u1eef li\u1ec7u ri\u00eang. \u0110\u1ec3 th\u1ef1c hi\u1ec7n vi\u1ec7c n\u00e0y, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t t\u1ec7p c\u1ea5u h\u00ecnh Terraform kh\u00e1c cho Linode th\u1ee9 hai.<\/p>\n<p class=\"has-background\" style=\"background-color:#74f78c33\">Ghi ch\u00fa: Khi tri\u1ec3n khai nhi\u1ec1u Linode v\u1edbi Terraform, h\u00e3y nh\u1edb r\u1eb1ng b\u1ea1n c\u1ea7n ph\u1ea3i ch\u1ec9 \u0111\u1ecbnh m\u1ed9t t\u00ean duy nh\u1ea5t cho m\u1ed7i Linode.Trong m\u00f4i tr\u01b0\u1eddng s\u1ea3n xu\u1ea5t, kh\u00f3a SSH v\u00e0 m\u1eadt kh\u1ea9u g\u1ed1c c\u1ee7a b\u1ea1n ph\u1ea3i l\u00e0 duy nh\u1ea5t cho m\u1ed7i t\u00e0i nguy\u00ean. Tuy nhi\u00ean, c\u00e1c Linode v\u00ed d\u1ee5 trong h\u01b0\u1edbng d\u1eabn n\u00e0y chia s\u1ebb kh\u00f3a v\u00e0 m\u1eadt kh\u1ea9u g\u1ed1c.<\/p>\n<p>1.T\u1ea1o m\u1ed9t t\u1ec7p kh\u00e1c c\u00f3 t\u00ean l\u00e0&nbsp;<code>linode-terraform-db.tf<\/code>. Thay th\u1ebf kh\u00f3a SSH v\u00e0 m\u1eadt kh\u1ea9u g\u1ed1c c\u1ee7a b\u1ea1n v\u00e0o n\u01a1i \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh.&nbsp;<strong>Kh\u00f4ng x\u00f3a<\/strong>&nbsp;<code>linode-terraform-web.tf<\/code>&nbsp;.<\/p>\n<pre class=\"wp-block-code\"><code>resource \"linode_instance\" \"terraform-db\" {\n  image = \"linode\/centos7\"\n  label = \"Terraform-Db-Example\"\n  group = \"Terraform\"\n  region = \"us-south\"\n  type = \"g6-standard-1\"\n  swap_size = 1024\n  authorized_keys = &#91; \"YOUR_PUBLIC_SSH_KEY\" ]\n  root_pass = \"YOUR_ROOT_PASSWORD\"\n}<\/code><\/pre>\n<p>B\u1ea1n c\u00f3 th\u1ec3 nh\u1eadn th\u1ea5y r\u1eb1ng nh\u00e0 cung c\u1ea5p Terraform kh\u00f4ng \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh trong t\u1ec7p n\u00e0y v\u00ec n\u00f3 n\u1eb1m trong&nbsp;<code>linode-terraform-web.tf<\/code>. Terraform t\u1ea3i v\u00e0o b\u1ed9 nh\u1edb v\u00e0 n\u1ed1i t\u1ea5t c\u1ea3 c\u00e1c t\u1ec7p c\u00f3 trong th\u01b0 m\u1ee5c l\u00e0m vi\u1ec7c c\u00f3&nbsp;<code>.tf<\/code>ph\u1ea7n m\u1edf r\u1ed9ng. \u0110i\u1ec1u n\u00e0y c\u00f3 ngh\u0129a l\u00e0 b\u1ea1n kh\u00f4ng c\u1ea7n ph\u1ea3i x\u00e1c \u0111\u1ecbnh l\u1ea1i nh\u00e0 cung c\u1ea5p trong&nbsp;<code>.tf<\/code>c\u00e1c t\u1ec7p m\u1edbi.<\/p>\n<p>2.Xem l\u1ea1i k\u1ebf ho\u1ea1ch Terraform:<\/p>\n<pre class=\"wp-block-code\"><code>terraform plan\n<\/code><\/pre>\n<p>Terraform bi\u1ebft r\u1eb1ng Terraform-Web-Example Linode c\u1ee7a b\u1ea1n v\u1eabn t\u1ed3n t\u1ea1i, do \u0111\u00f3 k\u1ebf ho\u1ea1ch ch\u1ec9 hi\u1ec3n th\u1ecb r\u1eb1ng Terraform-Db-Example Linode s\u1ebd \u0111\u01b0\u1ee3c t\u1ea1o:<\/p>\n<pre class=\"wp-block-code\"><code>An execution plan has been generated and is shown below.\nResource actions are indicated with the following symbols:\n  + create\n\nTerraform will perform the following actions:\n\n  + linode_instance.terraform-db\n      # &#91;...]\n\nPlan: 1 to add, 0 to change, 0 to destroy.\n\n# &#91;...]<\/code><\/pre>\n<p>3.\u00c1p d\u1ee5ng c\u1ea5u h\u00ecnh:<\/p>\n<pre class=\"wp-block-code\"><code>terraform apply<\/code><\/pre>\n<p>4.Ki\u1ec3m tra Tr\u00ecnh qu\u1ea3n l\u00fd Linode \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o Linode&nbsp;<code>Terraform-Db-Example<\/code>\u0111\u00e3 \u0111\u01b0\u1ee3c th\u00eam v\u00e0o t\u00e0i kho\u1ea3n c\u1ee7a b\u1ea1n.<\/p>\n<h3 id=\"destroy-servers\">Ph\u00e1 h\u1ee7y m\u00e1y ch\u1ee7<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#destroy-servers\"><\/a><\/h3>\n<p>Terraform bao g\u1ed3m l\u1ec7nh&nbsp;<a href=\"https:\/\/www.terraform.io\/docs\/commands\/destroy.html\" target=\"_blank\" rel=\"noreferrer noopener\">destroy<\/a>&nbsp;\u0111\u1ec3 x\u00f3a c\u00e1c m\u00e1y ch\u1ee7 do Terraform qu\u1ea3n l\u00fd. Tr\u01b0\u1edbc khi ch\u1ea1y l\u1ec7nh destroy, b\u1ea1n c\u00f3 th\u1ec3 ch\u1ea1y l\u1ec7nh plan v\u1edbi t\u00f9y&nbsp;<code>-destroy<\/code>ch\u1ecdn \u0111\u1ec3 xem m\u00e1y ch\u1ee7 n\u00e0o s\u1ebd b\u1ecb x\u00f3a:<\/p>\n<pre class=\"wp-block-code\"><code>terraform plan -destroy<\/code><\/pre>\n<pre class=\"wp-block-code\"><code># &#91;...]\n\nAn execution plan has been generated and is shown below.\nResource actions are indicated with the following symbols:\n  - destroy\n\nTerraform will perform the following actions:\n\n  - linode_instance.terraform-db\n\n  - linode_instance.terraform-web\n\n\nPlan: 0 to add, 0 to change, 2 to destroy.\n\n# &#91;...]<\/code><\/pre>\n<p>1.Ch\u1ea1y l\u1ec7nh destroy \u0111\u1ec3 x\u00f3a c\u00e1c m\u00e1y ch\u1ee7 kh\u1ecfi ph\u1ea7n cu\u1ed1i c\u00f9ng. X\u00e1c nh\u1eadn vi\u1ec7c x\u00f3a b\u1eb1ng&nbsp;<code>yes<\/code>khi \u0111\u01b0\u1ee3c nh\u1eafc:<\/p>\n<pre class=\"wp-block-code\"><code>terraform destroy<\/code><\/pre>\n<p>2.X\u00e1c minh r\u1eb1ng Linode \u0111\u00e3 \u0111\u01b0\u1ee3c x\u00f3a trong Linode Manager.<\/p>\n<p>3.X\u00f3a c\u00e1c t\u1eadp tin c\u1ea5u h\u00ecnh:<\/p>\n<pre class=\"wp-block-code\"><code>rm *.tf<\/code><\/pre>\n<h3 id=\"provision-multiple-servers-using-variables\">Cung c\u1ea5p nhi\u1ec1u m\u00e1y ch\u1ee7 b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng bi\u1ebfn<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#provision-multiple-servers-using-variables\"><\/a><\/h3>\n<p>Cho \u0111\u1ebfn th\u1eddi \u0111i\u1ec3m n\u00e0y, quy tr\u00ecnh th\u00eam m\u1ed9t n\u00fat m\u1edbi v\u00e0o c\u01a1 s\u1edf h\u1ea1 t\u1ea7ng c\u1ee7a b\u1ea1n l\u00e0 t\u1ea1o m\u1ed9t t\u1ec7p m\u1edbi v\u00e0 ch\u1ea1y&nbsp;<code>terraform apply<\/code>. C\u00f3 m\u1ed9t s\u1ed1 nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a c\u00e1ch ti\u1ebfp c\u1eadn n\u00e0y:<\/p>\n<ul>\n<li>B\u1ea1n c\u1ea7n ph\u1ea3i sao ch\u00e9p nhi\u1ec1u l\u1ea7n m\u1ed9t s\u1ed1 gi\u00e1 tr\u1ecb nh\u1ea5t \u0111\u1ecbnh trong m\u1ed7i t\u1ec7p, nh\u01b0 kh\u00f3a SSH.<\/li>\n<li>N\u1ebfu b\u1ea1n mu\u1ed1n thay \u0111\u1ed5i m\u1ed9t s\u1ed1 tham s\u1ed1 tr\u00ean nhi\u1ec1u m\u00e1y ch\u1ee7, nh\u01b0&nbsp;<code>group<\/code>thu\u1ed9c t\u00ednh c\u1ee7a Linodes, th\u00ec b\u1ea1n c\u1ea7n ph\u1ea3i thay \u0111\u1ed5i t\u1eebng t\u1ec7p.<\/li>\n<\/ul>\n<p>\u0110\u1ec3 gi\u1ea3i quy\u1ebft nh\u1eefng v\u1ea5n \u0111\u1ec1 n\u00e0y, Terraform cho ph\u00e9p b\u1ea1n khai b\u00e1o c\u00e1c bi\u1ebfn v\u00e0 ch\u00e8n gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c bi\u1ebfn \u0111\u00f3 v\u00e0o c\u1ea5u h\u00ecnh c\u1ee7a b\u1ea1n:<\/p>\n<p>1.T\u1ea1o m\u1ed9t t\u1ec7p m\u1edbi \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh t\u00ean bi\u1ebfn v\u00e0 c\u00e1c gi\u00e1 tr\u1ecb bi\u1ebfn m\u1eb7c \u0111\u1ecbnh t\u00f9y ch\u1ecdn. T\u1ec7p n\u00e0y c\u00f3 th\u1ec3 c\u00f3 b\u1ea5t k\u1ef3 t\u00ean n\u00e0o; \u0111\u1ed1i v\u1edbi v\u00ed d\u1ee5 n\u00e0y, h\u00e3y s\u1eed d\u1ee5ng&nbsp;<code>variables.tf<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code>variable \"token\" {}\nvariable \"authorized_keys\" {}\nvariable \"root_pass\" {}\nvariable \"region\" {\n  default = \"us-southeast\"\n}<\/code><\/pre>\n<p>2.T\u1ea1o t\u1ec7p&nbsp;<code>terraform.tfvars<\/code>\u0111\u1ec3 l\u01b0u tr\u1eef gi\u00e1 tr\u1ecb bi\u1ebfn c\u1ee7a b\u1ea1n. Thay th\u1ebf m\u00e3 th\u00f4ng b\u00e1o API, kh\u00f3a SSH v\u00e0 m\u1eadt kh\u1ea9u g\u1ed1c c\u1ee7a b\u1ea1n v\u00e0o n\u01a1i \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh.&nbsp;<strong>B\u1ea1n kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i t\u00ean t\u1ec7p n\u00e0y<\/strong>&nbsp;sau khi t\u1ea1o.<\/p>\n<pre class=\"wp-block-code\"><code>token = \"YOUR_LINODE_API_TOKEN\"\nauthorized_keys = \"YOUR_PUBLIC_SSH_KEY\"\nroot_pass =\"YOUR_ROOT_PASSWORD\"<\/code><\/pre>\n<p>3.T\u1ea1o m\u1ed9t t\u1ec7p c\u1ea5u h\u00ecnh m\u1edbi c\u00f3 t\u00ean l\u00e0&nbsp;<code>linode-terraform-template.tf<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code># Linode Provider definition\nterraform {\n  required_providers {\n    linode = {\n      source = \"linode\/linode\"\n      version = \"2.5.2\"\n    }\n  }\n}\n\nprovider \"linode\" {\n  token = var.token\n}\n\n# Example Web Server\nresource \"linode_instance\" \"terraform-web\" {\n        image = \"linode\/centos7\"\n        label = \"Terraform-Web-Example\"\n        group = \"Terraform\"\n        region = var.region\n        type = \"g6-standard-1\"\n        swap_size = 1024\n        authorized_keys = &#91;var.authorized_keys]\n        root_pass = var.root_pass\n}\n\n# Example Database Server\nresource \"linode_instance\" \"terraform-db\" {\n        image = \"linode\/ubuntu18.04\"\n        label = \"Terraform-Db-Example\"\n        group = \"Terraform\"\n        region = var.region\n        type = \"g6-standard-1\"\n        swap_size = 1024\n        authorized_keys = &#91;var.authorized_keys]\n        root_pass = var.root_pass\n}<\/code><\/pre>\n<p>4.Ki\u1ec3m tra l\u1ed7i trong l\u1ea7n tri\u1ec3n khai m\u1edbi c\u1ee7a b\u1ea1n:<\/p>\n<pre class=\"wp-block-code\"><code>terraform plan<\/code><\/pre>\n<p>5.\u00c1p d\u1ee5ng c\u1ea5u h\u00ecnh:<\/p>\n<pre class=\"wp-block-code\"><code>terraform apply<\/code><\/pre>\n<ol>\n<li>K\u1ebft qu\u1ea3 cu\u1ed1i c\u00f9ng s\u1ebd gi\u1ed1ng nh\u01b0 tr\u01b0\u1edbc.<\/li>\n<\/ol>\n<h3 id=\"modify-live-deployments\">S\u1eeda \u0111\u1ed5i tri\u1ec3n khai tr\u1ef1c ti\u1ebfp<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#modify-live-deployments\"><\/a><\/h3>\n<p>Terraform cho ph\u00e9p b\u1ea1n thay \u0111\u1ed5i t\u00ean, k\u00edch th\u01b0\u1edbc ho\u1eb7c c\u00e1c thu\u1ed9c t\u00ednh kh\u00e1c c\u1ee7a m\u00e1y ch\u1ee7 m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i ph\u00e1 h\u1ee7y v\u00e0 x\u00e2y d\u1ef1ng l\u1ea1i. Terraform x\u1eed l\u00fd vi\u1ec7c n\u00e0y th\u00f4ng qua c\u00e1c thay \u0111\u1ed5i \u0111\u1ed1i v\u1edbi c\u00e1c t\u1ec7p c\u1ea5u h\u00ecnh.<\/p>\n<p class=\"has-background\" style=\"background-color:#f32a2a33\">Quan tr\u1ecdng: Vi\u1ec7c thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc Linode c\u1ee7a b\u1ea1n s\u1ebd bu\u1ed9c m\u00e1y ch\u1ee7 c\u1ee7a b\u1ea1n ph\u1ea3i t\u1eaft ngu\u1ed3n v\u00e0 di chuy\u1ec3n \u0111\u1ebfn m\u1ed9t m\u00e1y ch\u1ee7 kh\u00e1c trong c\u00f9ng m\u1ed9t trung t\u00e2m d\u1eef li\u1ec7u. Vi\u1ec7c di chuy\u1ec3n \u0111\u0129a li\u00ean quan m\u1ea5t kho\u1ea3ng 1 ph\u00fat cho m\u1ed7i 3-5 gigabyte d\u1eef li\u1ec7u. Xem h\u01b0\u1edbng d\u1eabn&nbsp;T<a href=\"https:\/\/www.linode.com\/docs\/products\/compute\/compute-instances\/guides\/resize\/\">hay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc Linode<\/a>&nbsp;c\u1ee7a ch\u00fang t\u00f4i \u0111\u1ec3 bi\u1ebft th\u00eam th\u00f4ng tin.<\/p>\n<p>1.S\u1eeda \u0111\u1ed5i&nbsp;<code>linode-terraform-template.tf<\/code>v\u00e0 c\u1eadp nh\u1eadt&nbsp;<code>type<\/code>gi\u00e1 tr\u1ecb&nbsp;<code>g6-standard-4<\/code>cho&nbsp;<code>terraform-db<\/code>t\u00e0i nguy\u00ean.<\/p>\n<pre class=\"wp-block-code\"><code># &#91;...]\n\nresource \"linode_instance\" \"terraform-db\" {\n        # &#91;...]\n        type = \"g6-standard-4\"\n        # &#91;...]\n}<\/code><\/pre>\n<p>2.Xem l\u1ea1i k\u1ebf ho\u1ea1ch:<\/p>\n<pre class=\"wp-block-code\"><code>terraform plan<\/code><\/pre>\n<pre class=\"wp-block-code\"><code># &#91;...]\n\nAn execution plan has been generated and is shown below.\nResource actions are indicated with the following symbols:\n  ~ update in-place\n\nTerraform will perform the following actions:\n\n  ~ linode_instance.terraform-db\n      type: \"g6-standard-1\" =&gt; \"g6-standard-4\"\n\n\nPlan: 0 to add, 1 to change, 0 to destroy.\n\n# &#91;...]<\/code><\/pre>\n<p>3.\u00c1p d\u1ee5ng nh\u1eefng thay \u0111\u1ed5i c\u1ee7a b\u1ea1n:<\/p>\n<pre class=\"wp-block-code\"><code>terraform apply<\/code><\/pre>\n<p>4.X\u00e1c minh nh\u1eefng thay \u0111\u1ed5i trong Linode Manager.<\/p>\n<h2 id=\"terraform-modules\">M\u00f4-\u0111un Terraform<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#terraform-modules\"><\/a><\/h2>\n<p>Terraform s\u1eed d\u1ee5ng m\u1ed9t kh\u00e1i ni\u1ec7m g\u1ecdi l\u00e0&nbsp;<a href=\"https:\/\/www.terraform.io\/docs\/modules\/index.html\" target=\"_blank\" rel=\"noreferrer noopener\"><em>m\u00f4-\u0111un<\/em><\/a>&nbsp;\u0111\u1ec3 nh\u00f3m c\u00e1c y\u00eau c\u1ea7u v\u00e0 c\u1ea5u h\u00ecnh m\u00e1y ch\u1ee7 chung. H\u00e3y ngh\u0129 v\u1ec1 c\u00e1c m\u00f4-\u0111un t\u01b0\u01a1ng t\u1ef1 nh\u01b0&nbsp;<em>c\u00e1c h\u00e0m<\/em>&nbsp;trong ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh.<\/p>\n<p>V\u00ed d\u1ee5, gi\u1ea3 s\u1eed b\u1ea1n \u0111i\u1ec1u h\u00e0nh m\u1ed9t c\u00f4ng ty web v\u00e0 c\u1ea7n tri\u1ec3n khai c\u00e1c c\u1eb7p m\u00e1y ch\u1ee7 web v\u00e0 m\u00e1y ch\u1ee7 c\u01a1 s\u1edf d\u1eef li\u1ec7u gi\u1ed1ng h\u1ec7t nhau cho c\u00e1c m\u00e1y kh\u00e1ch kh\u00e1c nhau. \u0110\u1ec3 t\u1ea1o \u0111i\u1ec1u ki\u1ec7n thu\u1eadn l\u1ee3i cho vi\u1ec7c n\u00e0y, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t m\u00f4-\u0111un Terraform c\u00f3 th\u1ec3 t\u00e1i s\u1eed d\u1ee5ng m\u00f4 t\u1ea3 c\u1eb7p m\u00e1y ch\u1ee7 web v\u00e0 m\u00e1y ch\u1ee7 c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/p>\n<p>M\u00f4 t\u1ea3 c\u1ee7a m\u00f4-\u0111un cho ph\u00e9p thay th\u1ebf bi\u1ebfn c\u00e1c thu\u1ed9c t\u00ednh c\u00f3 li\u00ean quan (m\u1eadt kh\u1ea9u, kh\u00f3a, v.v.), gi\u1ed1ng nh\u01b0 trong c\u1ea5u h\u00ecnh t\u1eeb ph\u1ea7n tr\u01b0\u1edbc. Sau khi m\u00f4-\u0111un \u0111\u01b0\u1ee3c c\u1ea5u h\u00ecnh, c\u00e1c m\u00e1y ch\u1ee7 m\u1edbi c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o cho m\u1ed7i m\u00e1y kh\u00e1ch c\u1ee7a b\u1ea1n b\u1eb1ng c\u00e1ch k\u1ebft h\u1ee3p m\u00e3 m\u00f4-\u0111un v\u1edbi m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c gi\u00e1 tr\u1ecb bi\u1ebfn m\u1edbi.<\/p>\n<h3 id=\"basic-module-structure\">C\u1ea5u tr\u00fac m\u00f4-\u0111un c\u01a1 b\u1ea3n<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#basic-module-structure\"><\/a><\/h3>\n<p>C\u1ea5u tr\u00fac m\u00f4-\u0111un linh ho\u1ea1t, do \u0111\u00f3 b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng nhi\u1ec1u t\u1ec7p Terraform t\u00f9y theo nhu c\u1ea7u \u0111\u1ec3 m\u00f4 t\u1ea3 c\u01a1 s\u1edf h\u1ea1 t\u1ea7ng c\u1ee7a m\u00ecnh. V\u00ed d\u1ee5 n\u00e0y ch\u1ec9 ch\u1ee9a m\u1ed9t t\u1ec7p c\u1ea5u h\u00ecnh m\u00f4 t\u1ea3 m\u00e3 c\u00f3 th\u1ec3 t\u00e1i s\u1eed d\u1ee5ng.<\/p>\n<p>1.T\u1ea1o m\u1ed9t&nbsp;<code>modules\/app-deployment\/<\/code>th\u01b0 m\u1ee5c \u0111\u1ec3 l\u01b0u tr\u1eef c\u1ea5u h\u00ecnh m\u00f4-\u0111un:<\/p>\n<pre class=\"wp-block-code\"><code>cd ~\/terraform\nmkdir -p modules\/app-deployment<\/code><\/pre>\n<p>2.T\u1ea1o m\u1ed9t&nbsp;<code>main.tf<\/code>t\u1eadp tin c\u1ea5u h\u00ecnh b\u00ean trong&nbsp;<code>modules\/app-deployment\/<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code># Web Server\nresource \"linode_instance\" \"terraform-web\" {\n        image = \"linode\/ubuntu18.04\"\n        label = var.webserver_label\n        group = \"Terraform\"\n        region = var.region\n        type = \"g6-standard-1\"\n        swap_size = 1024\n        authorized_keys = var.authorized_keys\n        root_pass = var.root_pass\n}\n\n# Database Server\nresource \"linode_instance\" \"terraform-db\" {\n        image = \"linode\/centos7\"\n        label = var.dbserver_label\n        group = \"Terraform\"\n        region = var.region\n        type = var.db_type\n        swap_size = 1024\n        authorized_keys = var.authorized_keys\n        root_pass = var.root_pass\n}<\/code><\/pre>\n<p>3.C\u1ea5u h\u00ecnh tr\u00ean t\u00e1i t\u1ea1o c\u00e1c v\u00ed d\u1ee5 tr\u01b0\u1edbc b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c bi\u1ebfn. T\u1ec7p ti\u1ebfp theo ch\u1ee9a c\u00e1c \u0111\u1ecbnh ngh\u0129a bi\u1ebfn. G\u00e1n m\u1ed9t gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh cho m\u1ed7i bi\u1ebfn. Gi\u00e1 tr\u1ecb \u0111\u00f3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng n\u1ebfu b\u1ea1n kh\u00f4ng ghi \u0111\u00e8 khi g\u1ecdi m\u00f4-\u0111un.<\/p>\n<p>Thay th\u1ebf kh\u00f3a SSH v\u00e0 m\u1eadt kh\u1ea9u g\u1ed1c c\u1ee7a b\u1ea1n v\u00e0o n\u01a1i ch\u1ec9 \u0111\u1ecbnh:<\/p>\n<pre class=\"wp-block-code\"><code>variable \"webserver_label\" {\n    description = \"The name for the Web Server\"\n    default = \"default-web\"\n}\n\nvariable \"dbserver_label\" {\n    description = \"The name for the Database Server\"\n    default = \"default-db\"\n}\n\nvariable \"db_type\" {\n    description = \"The size (plan) for your Database Linode\"\n    default = \"g6-standard-1\"\n}\n\nvariable \"region\" {\n    description = \"The default Linode region to deploy the infrastructure\"\n    default = \"us-east\"\n}\n\nvariable \"authorized_keys\" {\n    description = \"The Public id_rsa.pub key used for secure SSH connections\"\n    default = &#91;\"default-ssh-public-key\"]\n}\n\nvariable \"root_pass\" {\n    description = \"The default root password for the Linode server\"\n    default = \"default-root-password\"\n}<\/code><\/pre>\n<h3 id=\"working-with-modules\">L\u00e0m vi\u1ec7c v\u1edbi c\u00e1c m\u00f4-\u0111un<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#working-with-modules\"><\/a><\/h3>\n<p>T\u1ea1o m\u1ed9t tri\u1ec3n khai cho m\u1ed9t m\u00e1y kh\u00e1ch t\u01b0\u1edfng t\u01b0\u1ee3ng:<\/p>\n<p>1.T\u1ea1o m\u1ed9t&nbsp;<code>client1<\/code>th\u01b0 m\u1ee5c:<\/p>\n<pre class=\"wp-block-code\"><code>cd ~\/terraform\nmkdir client1<\/code><\/pre>\n<p>2.T\u1ea1o m\u1ed9t&nbsp;<code>main.tf<\/code>t\u1ec7p c\u1ea5u h\u00ecnh b\u00ean trong&nbsp;<code>client1\/<\/code>s\u1eed d\u1ee5ng m\u00f4-\u0111un c\u1ee7a b\u1ea1n. M\u00f4-\u0111un \u0111\u01b0\u1ee3c tham chi\u1ebfu b\u1eb1ng c\u00e1ch cung c\u1ea5p \u0111\u01b0\u1eddng d\u1eabn \u0111\u1ebfn c\u1ea5u h\u00ecnh c\u1ee7a m\u00f4-\u0111un. Thay th\u1ebf m\u00e3 th\u00f4ng b\u00e1o API, kh\u00f3a SSH v\u00e0 m\u1eadt kh\u1ea9u g\u1ed1c c\u1ee7a b\u1ea1n v\u00e0o n\u01a1i \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh:<\/p>\n<pre class=\"wp-block-code\"><code># Client 1 Infrastructure\nterraform {\n  required_providers {\n    linode = {\n      source = \"linode\/linode\"\n      version = \"2.5.2\"\n    }\n  }\n}\nprovider \"linode\" {\n  token = \"YOUR_LINODE_API_TOKEN\"\n}\n\nmodule \"app-deployment\" {\n  source = \"..\/modules\/app-deployment\"\n\n# Variables Specific to this Deployment\nregion = \"us-east\"\nauthorized_keys = &#91; \"YOUR_PUBLIC_SSH_KEY\" ]\nroot_pass =\"YOUR_ROOT_PASSWORD\"\n\n# Variables Specific to Servers\nwebserver_label = \"client1-web\"\ndbserver_label = \"client1-db\"\ndb_type = \"g6-standard-8\"\n}<\/code><\/pre>\n<p>3.C\u1ea5u tr\u00fac t\u1ec7p cho m\u00f4-\u0111un v\u00e0 for c\u1ee7a b\u1ea1n&nbsp;<code>client1<\/code>b\u00e2y gi\u1edd s\u1ebd tr\u00f4ng nh\u01b0 sau. C\u1ea5u tr\u00fac n\u00e0y kh\u00f4ng b\u1eaft bu\u1ed9c b\u1edfi Terraform, nh\u01b0ng n\u00f3 h\u1eefu \u00edch nh\u01b0 m\u1ed9t v\u00ed d\u1ee5 \u0111\u01a1n gi\u1ea3n:<\/p>\n<pre class=\"wp-block-code\"><code>client1\n\u2514\u2500\u2500 main.tf\nmodules\n\u2514\u2500\u2500 app-deployment\n&amp;emsp;&amp;emsp;&amp;emsp;&amp;emsp;\u251c\u2500\u2500 main.tf\n&amp;emsp;&amp;emsp;&amp;emsp;&amp;emsp;\u2514\u2500\u2500 variables.tf<\/code><\/pre>\n<p>4.Kh\u1edfi t\u1ea1o c\u1ea5u h\u00ecnh Terraform cho m\u00e1y kh\u00e1ch, xem l\u1ea1i k\u1ebf ho\u1ea1ch v\u00e0 \u00e1p d\u1ee5ng:<\/p>\n<pre class=\"wp-block-code\"><code>cd ~\/terraform\/client1\/\nterraform init\nterraform plan\nterraform apply<\/code><\/pre>\n<h3 id=\"use-linode-object-storage-to-store-state\">S\u1eed d\u1ee5ng Linode Object Storage \u0111\u1ec3 l\u01b0u tr\u1eef tr\u1ea1ng th\u00e1i<a href=\"https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/#use-linode-object-storage-to-store-state\"><\/a><\/h3>\n<p><a href=\"https:\/\/developer.hashicorp.com\/terraform\/language\/state\" target=\"_blank\" rel=\"noreferrer noopener\">T\u1ec7p d\u1eef li\u1ec7u tr\u1ea1ng th\u00e1i<\/a>&nbsp;\u0111\u01b0\u1ee3c Terraform&nbsp;l\u01b0u tr\u1eef tr\u00ean m\u1ed9t&nbsp;<a href=\"https:\/\/developer.hashicorp.com\/terraform\/language\/settings\/backends\/configuration\" target=\"_blank\" rel=\"noreferrer noopener\">backend<\/a><code>terraform.tfstate<\/code>&nbsp;\u0111\u1ec3 ghi nh\u1eadt k\u00fd v\u00e0 theo d\u00f5i si\u00eau d\u1eef li\u1ec7u, \u00e1nh x\u1ea1 t\u00e0i nguy\u00ean v\u00e0o c\u1ea5u h\u00ecnh v\u00e0 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t. Theo m\u1eb7c \u0111\u1ecbnh, tr\u1ea1ng th\u00e1i \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef c\u1ee5c b\u1ed9 trong t\u1ec7p.<\/p>\n<p>S\u1eed d\u1ee5ng c\u1ea5u h\u00ecnh b\u00ean d\u01b0\u1edbi v\u1edbi&nbsp;<code>backend<\/code>kh\u1ed1i, b\u1ea1n c\u00f3 th\u1ec3 thi\u1ebft l\u1eadp Terraform \u0111\u1ec3 s\u1eed d\u1ee5ng Linode Object Storage \u0111\u1ec3 l\u01b0u tr\u1eef tr\u1ea1ng th\u00e1i t\u1eeb xa.&nbsp;<code>backend<\/code>Kh\u1ed1i ph\u1ea3i \u0111\u01b0\u1ee3c l\u1ed3ng v\u00e0o&nbsp;<code>terraform<\/code>kh\u1ed1i nh\u01b0 \u0111\u00e3 l\u01b0u \u00fd trong&nbsp;<a href=\"https:\/\/developer.hashicorp.com\/terraform\/language\/settings\/backends\/s3\" target=\"_blank\" rel=\"noreferrer noopener\">t\u00e0i li\u1ec7u ch\u00ednh th\u1ee9c v\u1ec1 backend c\u1ee7a Hashicorp<\/a>&nbsp;. Trong h\u01b0\u1edbng d\u1eabn n\u00e0y,&nbsp;<code>terraform<\/code>kh\u1ed1i n\u1eb1m trong&nbsp;<code>main.tf<\/code>t\u1ec7p c\u1ea5u h\u00ecnh.<\/p>\n<p>L\u01b0u \u00fd r\u1eb1ng m\u00f4-\u0111un n\u00e0y gi\u1ea3 \u0111\u1ecbnh r\u1eb1ng m\u1ed9t th\u00f9ng l\u01b0u tr\u1eef \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u00e3 t\u1ed3n t\u1ea1i tr\u00ean t\u00e0i kho\u1ea3n c\u1ee7a b\u1ea1n. Thay th\u1ebf c\u00e1c gi\u00e1 tr\u1ecb b\u1eb1ng th\u00f9ng v\u00e0 th\u00f4ng tin kh\u00f3a c\u1ee7a b\u1ea1n:<\/p>\n<pre class=\"wp-block-code\"><code># Backend Configuration\nbackend \"s3\" {\n    bucket = \"YOUR-BUCKET-NAME\"   # The bucket name created on your account to which your access_key and secret_key can read and write\n    key = \"tf\/tfstate\"  # The folder (tf) and object (tfstate) in your bucket where you want to write state to\n    region = \"us-southeast-1\"  # The region where your object storage bucket is at which is the same as the ClusterID Here https:\/\/techdocs.akamai.com\/cloud-computing\/docs\/access-buckets-and-files-through-urls#cluster-url-s3-endpoint\n    access_key = \"OBJ-ACCESS-KEY\"  # You can put your value here inline or add it as an environment variable AWS_ACCESS_KEY_ID  see more here https:\/\/developer.hashicorp.com\/terraform\/language\/settings\/backends\/s3#credentials-and-shared-configuration\n    secret_key = \"OBJ-SECRET-KEY\"  # You can put your value here inline or add it as an environment variable AWS_SECRET_ACCESS_KEY see more here https:\/\/developer.hashicorp.com\/terraform\/language\/settings\/backends\/s3#credentials-and-shared-configuration\n    skip_region_validation = true  # All of these skip_* arguements are used since our object storage doesn't implement these additional endpoints\n    skip_credentials_validation = true\n    skip_requesting_account_id = true\n    skip_s3_checksum = true\n    endpoints = {\n      s3 = \"https:\/\/us-southeast-1.linodeobjects.com\"  # The endpoint for the s3 API based on the region your bucket is located https:\/\/techdocs.akamai.com\/cloud-computing\/docs\/access-buckets-and-files-through-urls#cluster-url-s3-endpoint\n    }\n  }<\/code><\/pre>\n<p>Ngu\u1ed3n: https:\/\/www.linode.com\/docs\/guides\/how-to-build-your-infrastructure-using-terraform-and-linode\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C\u01a1 s\u1edf h\u1ea1 t\u1ea7ng d\u01b0\u1edbi d\u1ea1ng m\u00e3 (IaC) l\u00e0 m\u1ed9t ph\u01b0\u01a1ng ph\u00e1p ph\u00e1t tri\u1ec3n v\u00e0 v\u1eadn h\u00e0nh cho ph\u00e9p tri\u1ec3n khai m\u00e1y ch\u1ee7 v\u00e0 c\u1ea5u h\u00ecnh ph\u1ea7n m\u1ec1m \u0111\u01b0\u1ee3c th\u1ec3 hi\u1ec7n d\u01b0\u1edbi d\u1ea1ng m\u00e3. Ph\u01b0\u01a1ng ph\u00e1p n\u00e0y l\u00e0m gi\u1ea3m kh\u1ea3 n\u0103ng x\u1ea3y ra l\u1ed7i c\u1ee7a con ng\u01b0\u1eddi, gi\u00fap c\u00e1c h\u1ec7 th\u1ed1ng ph\u1ee9c t\u1ea1p d\u1ec5 qu\u1ea3n<\/p>\n","protected":false},"author":1,"featured_media":36205,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-35018","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-terraform"],"_links":{"self":[{"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/posts\/35018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/comments?post=35018"}],"version-history":[{"count":0,"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/posts\/35018\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/media\/36205"}],"wp:attachment":[{"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/media?parent=35018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/categories?post=35018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jupitek.maudemo.vip\/index.php\/wp-json\/wp\/v2\/tags?post=35018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}