{"id":658,"date":"2024-08-24T14:44:09","date_gmt":"2024-08-24T21:44:09","guid":{"rendered":"http:\/\/184.72.63.26\/?p=658"},"modified":"2024-10-01T14:14:13","modified_gmt":"2024-10-01T21:14:13","slug":"adopt-infrastructure-as-code-iac-to-manage-aws-resources-with-terraform","status":"publish","type":"post","link":"https:\/\/www.wallacel.com\/index.php\/2024\/08\/24\/adopt-infrastructure-as-code-iac-to-manage-aws-resources-with-terraform\/","title":{"rendered":"Managing Infrastructure as Code (IaC) With Terraform"},"content":{"rendered":"\n<p>When managing AWS resources, it&#8217;s common to start with resources created manually or by other tools. As you begin adopting Infrastructure as Code (IaC) with Terraform, importing these existing resources becomes necessary. In this blog, we\u2019ll explore how to import AWS Systems Manager State Manager association into Terraform, and then how to update the EC2 instance IDs within the Terraform-managed resource.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisite<\/h2>\n\n\n\n<p>To use Terraform you will need to install a binary package for your platform (e.g. macOS or Windows). For macOS, you can install the Homebrew packages using the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">brew tap hashicorp\/tap\nbrew install hashicorp\/tap\/terraform<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Importing AWS Systems Manager State Manager Association<\/h2>\n\n\n\n<p>AWS Systems Manager State Manager is a powerful tool that helps manage the configuration of your instances. To manage existing State Manager associations with Terraform, you&#8217;ll first need to import them.<\/p>\n\n\n\n<p>First, create a Terraform configuration file (e.g. ssm.tf) with a place holder for your SSM association. You don&#8217;t need to fill in all the arguments, just create a basic structure. Here\u2019s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">terraform {\n  required_providers {\n    aws = {\n      source  = \"hashicorp\/aws\"\n      version = \"5.63.1\"\n    }\n  }\n}\n# Configure the AWS Provider\nprovider \"aws\" {\n  region = \"us-west-2\"\n}\nresource \"aws_ssm_association\" \"myEC2FreeSpace\" {\n  name = \"myEC2FreeSpace\"\n}<\/code><\/pre>\n\n\n\n<p>Before any import, you need to initialize your Terraform working directory. It downloads the necessary provider plugins, setup backend, and prepares the environment for running other Terraform commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">terraform init<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"867\" height=\"246\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/08\/init.png\" alt=\"\" class=\"wp-image-669\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/init.png 867w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/init-300x85.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/init-768x218.png 768w\" sizes=\"auto, (max-width: 867px) 100vw, 867px\" \/><\/figure>\n\n\n\n<p>Use the <code>terraform import<\/code> command to import the existing association into Terraform:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">terraform import aws_ssm_association.myEC2FreeSpace &lt;association-id&gt;<\/code><\/pre>\n\n\n\n<p>Replace <code>&lt;association-id&gt;<\/code> with the actual ID of the State Manager association. After importing, Terraform will manage this resource, and you can use <code>terraform plan<\/code> to see the current configuration.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"870\" height=\"151\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/08\/import-ssm.png\" alt=\"\" class=\"wp-image-659\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/import-ssm.png 870w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/import-ssm-300x52.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/import-ssm-768x133.png 768w\" sizes=\"auto, (max-width: 870px) 100vw, 870px\" \/><\/figure>\n\n\n\n<p>After importing, Terraform will now have the association in its state, but the resource block you initially created will likely be incomplete. Run the following command to see what Terraform detects as different between your configuration and the actual state:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">terraform plan<\/code><\/pre>\n\n\n\n<p>Terraform will show the differences, and you should update your <code>aws_ssm_association<\/code> resource block in your <code>ssm.tf<\/code> file to match the actual state configuration. This may include adding parameters, targets, document version, etc.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"878\" height=\"394\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/08\/terraform-plan.png\" alt=\"\" class=\"wp-image-660\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-plan.png 878w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-plan-300x135.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-plan-768x345.png 768w\" sizes=\"auto, (max-width: 878px) 100vw, 878px\" \/><\/figure>\n\n\n\n<p>Run the show command and copy the current terraform state to the configuration for editing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">terraform show<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"885\" height=\"478\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/08\/terraform-show.png\" alt=\"\" class=\"wp-image-661\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-show.png 885w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-show-300x162.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-show-768x415.png 768w\" sizes=\"auto, (max-width: 885px) 100vw, 885px\" \/><\/figure>\n\n\n\n<p>Edit the configuration until it matches the existing state.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">#SSM State Manager Association\nresource \"aws_ssm_association\" \"myEC2FreeSpace\" {\n    association_name                 = \"myEC2FreeSpace\"\n    document_version                 = \"$LATEST\"\n    name                             = \"CheckFreeDiskSpace\"\n    schedule_expression              = \"cron(0 *\/30 * * * ? *)\"\n    compliance_severity              = \"UNSPECIFIED\"\n    tags                             = {}       \n    targets {\n        key    = \"tag:InstanceIds\"\n        values = [\n            \"i-0abcd1234efgh5678\",\n        ]\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"881\" height=\"101\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/08\/terraform-plan2.png\" alt=\"\" class=\"wp-image-662\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-plan2.png 881w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-plan2-300x34.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-plan2-768x88.png 768w\" sizes=\"auto, (max-width: 881px) 100vw, 881px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Modify EC2 Instance ID in State Manager Association<\/h2>\n\n\n\n<p>To update the instance ID in the State Manager association using Terraform, modify your Terraform configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">#SSM State Manager Association\nresource \"aws_ssm_association\" \"myEC2FreeSpace\" {\n    association_name                 = \"myEC2FreeSpace\"\n    document_version                 = \"$LATEST\"\n    name                             = \"CheckFreeDiskSpace\"\n    schedule_expression              = \"cron(0 *\/30 * * * ? *)\"\n    compliance_severity              = \"UNSPECIFIED\"\n    tags                             = {}     \n    targets {\n        key    = \"tag:InstanceIds\"\n        values = [\n            \"i-0abcd1234efgh5678\",\n        ]\n    }\n}<\/code><\/pre>\n\n\n\n<p>Replace <code>\"i-0abcd1234efgh5678\"<\/code> with the new EC2 instance ID.<\/p>\n\n\n\n<p>Once you\u2019ve made the necessary updates to your configurations, verify the changes with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">terraform plan<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"387\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/08\/terraform-changeid.png\" alt=\"\" class=\"wp-image-663\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-changeid.png 875w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-changeid-300x133.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-changeid-768x340.png 768w\" sizes=\"auto, (max-width: 875px) 100vw, 875px\" \/><\/figure>\n\n\n\n<p>Apply the changes with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">terraform apply<\/code><\/pre>\n\n\n\n<p>This command will update the existing State Manager association with the new EC2 instance ID.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"535\" src=\"http:\/\/184.72.63.26\/wp-content\/uploads\/2024\/08\/terraform-apply.png\" alt=\"\" class=\"wp-image-664\" srcset=\"https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-apply.png 880w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-apply-300x182.png 300w, https:\/\/www.wallacel.com\/wp-content\/uploads\/2024\/08\/terraform-apply-768x467.png 768w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Final Thoughts<\/h2>\n\n\n\n<p>Managing AWS resources using Iac offers several key benefits. It greatly reduces the time required to deploy infrastructure and allows you to track changes in your configurations through version control, making it easier to maintain and audit. Once you have your environment setup using Terraform, you can easily replicate another (e.g. staging, production) by applying the same configurations, ensuring consistency across different environments.<\/p>\n\n\n\n<p>Thank you for reading and I hope you like my blog.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When managing AWS resources, it&#8217;s common to start with resources created manually or by other tools. As you begin adopting Infrastructure as Code (IaC) with Terraform, importing these existing resources becomes necessary. In this blog, we\u2019ll explore how to import AWS Systems Manager State Manager association into Terraform, and then how to update the EC2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":681,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[39,40,22],"class_list":["post-658","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","tag-iac","tag-state-manager","tag-terraform"],"_links":{"self":[{"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/posts\/658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/comments?post=658"}],"version-history":[{"count":10,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/posts\/658\/revisions"}],"predecessor-version":[{"id":684,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/posts\/658\/revisions\/684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/media\/681"}],"wp:attachment":[{"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/media?parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/categories?post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wallacel.com\/index.php\/wp-json\/wp\/v2\/tags?post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}