{"id":15708,"date":"2023-08-17T06:36:52","date_gmt":"2023-08-17T06:36:52","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15708"},"modified":"2023-08-17T06:42:05","modified_gmt":"2023-08-17T06:42:05","slug":"terrafrom-example-code-for-aws-ec2-instance-and-remote-exec-provisioner","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terrafrom-example-code-for-aws-ec2-instance-and-remote-exec-provisioner\/","title":{"rendered":"Terrafrom Tutorials &#8211; remote-exec provisioner using AWS &#038; Azure providers"},"content":{"rendered":"\n<p>the <code>remote-exec<\/code> provisioner in Terraform is used to execute commands remotely on a target resource after it&#8217;s been created. This is often used for post-deployment configuration or initialization tasks on virtual machines, containers, or other infrastructure resources.<\/p>\n\n\n\n<p>Here&#8217;s a general example of how you can use the <code>remote-exec<\/code> provisioner:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">resource <span class=\"hljs-string\">\"aws_instance\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  ami           = <span class=\"hljs-string\">\"ami-12345678\"<\/span>  <span class=\"hljs-comment\"># Replace with your desired AMI ID<\/span>\n  instance_type = <span class=\"hljs-string\">\"t2.micro\"<\/span>\n}\n\nprovisioner <span class=\"hljs-string\">\"remote-exec\"<\/span> {\n  inline = &#91;\n    <span class=\"hljs-string\">\"echo This is a remote command.\"<\/span>,\n    <span class=\"hljs-string\">\"echo You can execute multiple commands here.\"<\/span>,\n    <span class=\"hljs-string\">\"echo Remember that each command is executed independently.\"<\/span>,\n  ]\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/a90d0bfd77b2be624bb76cc9d504863e.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>The remote-exec provisioner requires a connection block to specify how Terraform will connect to the remote resource. The connection block can be defined in the same resource block as the remote-exec provisioner, or it can be defined in a separate resource block.<\/p>\n\n\n\n<p>The remote-exec provisioner has the following arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>inline: A list of commands to be executed. The provisioner uses a default shell unless you specify a shell as the first command (eg., #!\/bin\/bash).<\/li>\n\n\n\n<li>script: A path to a local script that will be copied to the remote resource and then executed.<\/li>\n\n\n\n<li>scripts: A list of paths to local scripts that will be copied to the remote resource and then executed, one after the other.<\/li>\n\n\n\n<li>timeout: The maximum amount of time to wait for the commands to complete, in seconds.<\/li>\n\n\n\n<li>on_failure: The action to take if the commands fail. Valid values are &#8220;continue&#8221; and &#8220;fail&#8221;.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">resource <span class=\"hljs-string\">\"aws_instance\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\r\n  ...\r\n}\r\n\r\nresource <span class=\"hljs-string\">\"null_resource\"<\/span> <span class=\"hljs-string\">\"bootstrap\"<\/span> {\r\n  depends_on = &#91;<span class=\"hljs-string\">\"aws_instance.example\"<\/span>]\r\n\r\n  provisioner <span class=\"hljs-string\">\"remote-exec\"<\/span> {\r\n    connection {\r\n      instance_id = aws_instance.example.id\r\n      user = <span class=\"hljs-string\">\"ubuntu\"<\/span>\r\n    }\r\n\r\n    inline = &#91;<span class=\"hljs-string\">\"sudo apt-get update\"<\/span>, <span class=\"hljs-string\">\"sudo apt-get install -y terraform\"<\/span>]\r\n    timeout = <span class=\"hljs-number\">600<\/span>\r\n    on_failure = <span class=\"hljs-string\">\"fail\"<\/span>\r\n  }\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Remote Exec in Windows<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">  provisioner <span class=\"hljs-string\">\"remote-exec\"<\/span> {\n    on_failure = <span class=\"hljs-string\">\"continue\"<\/span>\n    inline = &#91;\n      <span class=\"hljs-string\">\"powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command \\\"Write-Output 'Hello from Terraform'\\\"\"<\/span>,\n    ]\n  }\n\n  connection {\n    type        = <span class=\"hljs-string\">\"winrm\"<\/span>\n    user        = <span class=\"hljs-string\">\"Administrator\"<\/span>\n    password    = <span class=\"hljs-string\">\"your_password\"<\/span>\n    host        = <span class=\"hljs-keyword\">self<\/span>.public_ip_address\n    https       = <span class=\"hljs-keyword\">false<\/span>\n    port        = <span class=\"hljs-number\">5985<\/span>\n    timeout     = <span class=\"hljs-string\">\"10m\"<\/span>\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">  connection {\n    type     = <span class=\"hljs-string\">\"winrm\"<\/span>\n    user     = <span class=\"hljs-string\">\"Administrator\"<\/span>\n    password = <span class=\"hljs-string\">\"your_password\"<\/span>\n    host     = <span class=\"hljs-keyword\">self<\/span>.public_ip_address\n    https    = <span class=\"hljs-keyword\">false<\/span>\n    port     = <span class=\"hljs-number\">5985<\/span>\n    timeout  = <span class=\"hljs-string\">\"10m\"<\/span>\n  }\n\n  provisioner <span class=\"hljs-string\">\"remote-exec\"<\/span> {\n    on_failure = <span class=\"hljs-string\">\"continue\"<\/span>\n    inline = &#91;\n      <span class=\"hljs-string\">\"cmd \/c echo Hello from Terraform\"<\/span>\n    ]\n  }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">provider <span class=\"hljs-string\">\"azurerm\"<\/span> {\n  features {}\n}\n\nresource <span class=\"hljs-string\">\"azurerm_virtual_machine\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  <span class=\"hljs-comment\"># ... other VM configuration ...<\/span>\n\n  connection {\n    type        = <span class=\"hljs-string\">\"winrm\"<\/span>\n    user        = <span class=\"hljs-string\">\"Administrator\"<\/span>\n    password    = <span class=\"hljs-string\">\"your_password\"<\/span>\n    host        = <span class=\"hljs-keyword\">self<\/span>.public_ip_address\n    https       = <span class=\"hljs-keyword\">false<\/span>\n    port        = <span class=\"hljs-number\">5985<\/span>\n    timeout     = <span class=\"hljs-string\">\"10m\"<\/span>\n  }\n}\n\nresource <span class=\"hljs-string\">\"null_resource\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  triggers = {\n    instance_id = azurerm_virtual_machine.example.id\n  }\n\n  provisioner <span class=\"hljs-string\">\"local-exec\"<\/span> {\n    command = <span class=\"hljs-string\">\"echo Remote execution completed\"<\/span>\n  }\n\n  provisioner <span class=\"hljs-string\">\"remote-exec\"<\/span> {\n    on_failure = <span class=\"hljs-string\">\"continue\"<\/span>\n    inline = &#91;\n      <span class=\"hljs-string\">\"powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command \\\"Write-Output 'Hello from Terraform'\\\"\"<\/span>,\n    ]\n  }\n\n  depends_on = &#91;azurerm_virtual_machine.example]\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_97089\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_62997\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_97089\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>the remote-exec provisioner in Terraform is used to execute commands remotely on a target resource after it&#8217;s been created. This is often used for post-deployment configuration or initialization tasks on virtual machines, containers, or other infrastructure resources. Here&#8217;s a general example of how you can use the remote-exec provisioner: The remote-exec provisioner requires a connection&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5633],"tags":[],"class_list":["post-15708","post","type-post","status-publish","format-standard","hentry","category-aws"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15708"}],"version-history":[{"count":6,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15708\/revisions"}],"predecessor-version":[{"id":38354,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15708\/revisions\/38354"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}