{"id":4777,"date":"2025-12-19T17:33:31","date_gmt":"2025-12-19T17:33:31","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=4777"},"modified":"2025-12-19T17:33:32","modified_gmt":"2025-12-19T17:33:32","slug":"terraform-ec2-instance-wait-to-be-ready","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=4777","title":{"rendered":"Terraform &#8211; EC2 Instance Wait to be Ready"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Although there are a few ways to do it, here is an example of how you can use the null_resource to make Terraform wait for an EC2 instance to be ready before continuning.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example we have an EC2 instance called <strong>mgmt_host<\/strong> which will using the &#8220;trigger&#8221; set the Terraform into a loop to wait. It uses the local-exec provisioner, which once the EC2 instance is deployed is started. Then let&#8217;s say you want to run an SSM command afterwards you just make that dependent on the null_resource.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you were to try this without the wait_for_instance null_resource, what would happen is the SSM command would run immediately after the EC2 instance is deployed; i.e. before it is ready and able to be used by SSM.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>resource \"null_resource\" \"wait_for_instance\" {\n\u00a0 provisioner \"local-exec\" {\n\u00a0 \u00a0 command = &lt;&lt;EOT\n\u00a0 \u00a0 while &#91; $(aws ec2 describe-instance-status --instance-ids ${aws_instance.mgmt_host.id} --query \"InstanceStatuses&#91;].InstanceStatus.Status\" --output text) != \"ok\" ]\n\u00a0 \u00a0 do\n\u00a0 \u00a0 \u00a0 echo \"Waiting for instance to be ready....\"\n\u00a0 \u00a0 \u00a0 sleep 2\n\u00a0 \u00a0 done\n\u00a0 \u00a0 echo \"Instance ready!\"\n\u00a0 \u00a0 EOT\n\u00a0 }\n\n\u00a0 triggers = {\n\u00a0 \u00a0 instance_id = aws_instance.mgmt_host.id\n\u00a0 }\n}\n\nresource \"null_resource\" \"run_ssm_command_download\" {\n\u00a0 provisioner \"local-exec\" {\n\u00a0 \u00a0 command = &lt;&lt;-EOT\n\u00a0 \u00a0 \u00a0 aws ssm send-command \\\n\u00a0 \u00a0 \u00a0 \u00a0 --instance-ids ${aws_instance.mgmt_host.id} \\\n\u00a0 \u00a0 \u00a0 \u00a0 --document-name \"AWS-RunShellScript\" \\\n\u00a0 \u00a0 \u00a0 \u00a0 --parameters commands=\"echo HELLO!\" \\\n\u00a0 \u00a0 \u00a0 \u00a0 --region eu-west-2\n\u00a0 \u00a0 EOT\n\u00a0 }\n\n\u00a0 depends_on = &#91;null_resource.wait_for_instance]\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The aws ec2 describe-instance-status is waiting for the instance not only to be &#8220;started&#8221;, but also for the instance to be &#8220;ready&#8221;, which in the AWS Web GUI will show as &#8220;2 Checks Passed&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Information<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/rpadovani.com\/terraform-cloudinit\">https:\/\/rpadovani.com\/terraform-cloudinit<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although there are a few ways to do it, here is an example of how you can use the null_resource to make Terraform wait for an EC2 instance to be ready before continuning. In this example we have an EC2 instance called mgmt_host which will using the &#8220;trigger&#8221; set the Terraform into a loop to &#8230; <a title=\"Terraform &#8211; EC2 Instance Wait to be Ready\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=4777\" aria-label=\"Read more about Terraform &#8211; EC2 Instance Wait to be Ready\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":4335,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-4777","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-terraform"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4777","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=4777"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4777\/revisions"}],"predecessor-version":[{"id":4778,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4777\/revisions\/4778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/4335"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}