{"id":4742,"date":"2025-12-14T18:47:07","date_gmt":"2025-12-14T18:47:07","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=4742"},"modified":"2025-12-14T18:47:07","modified_gmt":"2025-12-14T18:47:07","slug":"using-ssm-command-from-terraform","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=4742","title":{"rendered":"Using SSM COMMAND from Terraform"},"content":{"rendered":"\n<p>So you have an EC2 instance you&#8217;ve deployed from Terraform, but as part of the run of your template you want to run some other commands. Sure, you can use the &#8220;user data&#8221; section to run commands at deployment time, but what if you want to run something later on? Well, you can use a &#8220;Local-Exec&#8221; provisioner, in fact you can use this for all sorts of things. <\/p>\n\n\n\n<p>In the example below we are just running a command on the EC2 instance, for this we first need to ensure that we have the instance ID of our EC2 instance. In my example the EC2 instance is known as &#8220;mgmt_host&#8221; within the Terraform templates, so the command<strong> aws_instance.mgmt_host.id<\/strong> will determine this ID automatically.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>resource \"null_resource\" \"run_ssm_command_unzip\" {\n  provisioner \"local-exec\" {\n    command = &lt;&lt;-EOT\n      aws ssm send-command \\\n        --instance-ids ${aws_instance.mgmt_host.id} \\\n        --document-name \"AWS-RunShellScript\" \\\n        --parameters commands=\"echo 'Hello!'\" \\\n        --region ${var.aws_region}  \n    EOT\n  }\n\n  #depends_on = &#91;null_resource.run_ssm_command_download]\n}<\/code><\/pre>\n\n\n\n<p>When putting the commands in, you may have some &#8220;fun&#8221; with speech marks, and need to ensure that the these are not ending the command early.<\/p>\n\n\n\n<p>You can troubleshoot the operation of the commands that you are running remotely by looking at the log file on the EC2 instance which can be found in: <strong>\/var\/log\/amazon\/ssm\/amazon-ssm-agent.log<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So you have an EC2 instance you&#8217;ve deployed from Terraform, but as part of the run of your template you want to run some other commands. Sure, you can use the &#8220;user data&#8221; section to run commands at deployment time, but what if you want to run something later on? Well, you can use a &#8230; <a title=\"Using SSM COMMAND from Terraform\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=4742\" aria-label=\"Read more about Using SSM COMMAND from Terraform\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":4373,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,45],"tags":[],"class_list":["post-4742","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","category-terraform"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4742"}],"version-history":[{"count":2,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4742\/revisions"}],"predecessor-version":[{"id":4775,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4742\/revisions\/4775"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/4373"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}