{"id":371,"date":"2023-04-21T07:29:16","date_gmt":"2023-04-21T07:29:16","guid":{"rendered":"https:\/\/santosh-shah.com\/blog\/?p=371"},"modified":"2023-04-24T05:18:59","modified_gmt":"2023-04-24T05:18:59","slug":"upload-files-into-aws-ec2-using-aws-cli","status":"publish","type":"post","link":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/","title":{"rendered":"Upload files into AWS EC2 using aws cli"},"content":{"rendered":"\n<p>We all have heard about codepipeline which simply triggered by a source commit and start a pipeline to build application then deploy automatically to ec2 servers. Upload files to aws ec2. Well this article is not about code pipeline but a simple script that will make a build in your system and upload directly to ec2 machine. It is for developer use only who has access to AWS cli. If you don&#8217;t have AWS cli configure then you need to configure one with AWS secret ID. Upload files into AWS EC2 using aws cli.<\/p>\n\n\n\n<p>We will be using SCP command which state as Secure Copy in order to transfer files from local computer to ec2 machine. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Upload files into AWS EC2 using aws cli using SCP<\/h2>\n\n\n\n<p>Let&#8217;s create a file sandbox.sh file or any file name you wish to begin.  The code is preety straight forward. So you can customized according to your needs. First we run a command to make build and if the build is successfull we show message. Then we attempt to connect ec2 machine using SCP command which accept pem file then source and then remote ssh public dns ip with folder name where you need to copy files.<\/p>\n\n\n\n<p>This command will upload a file source folder in your local computer of your laptop to folder destination folder on an Amazon instance. Note you still need to use the private key you used to connect to the Amazon instance with ssh. (In this example, it is the sandboxlinux.pem file inside the assets folder.<\/p>\n\n\n\n<p>Note: The pem file need to be protected and should not be accessible publicy other wise it will not connect. So before running the script run <code>chmod 400 .\/yourpath\/sandboxlinux.pem<\/code> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SOURCE_DIR=.\/dist\/projectName\/*\n\necho \"Building test\"\nif npm run build:prod:ssr ; then\n   echo \"Sandbox SSR Build Successful\"\nelse\n  echo \"exiting..\"\n  exit 1\nfi\n\necho \"Attempting to upload site ..\"\nscp -i .\/assets\/sandboxlinux.pem ${SOURCE_DIR} ssh_public_dns:~\/folder\/\n\n#your public dns would look like this.\n#ec2-00-000-000-000.compute-1.amazonaws.com\n\n# echo \"Invalidating cloudfrond distribution to get fresh cache\"\n# aws cloudfront create-invalidation --distribution-id=replace_with_your_distribution_ID --paths \/ --profile=myawsprofile\n\necho \"Deployment complete\" \n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"564\" src=\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image-1024x564.png\" alt=\"\" class=\"wp-image-375\" srcset=\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image-1024x564.png 1024w, https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image-300x165.png 300w, https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image-768x423.png 768w, https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png 1090w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This above script is to make build is for Angular based project. However you can customized the build command according to your needs.<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/34869580\/how-to-upload-local-system-files-to-amazon-ec2-using-ssh\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Credit source<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackabuse.com\/using-scp-to-copy-and-securely-transfer-files-and-folders\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Credit source 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all have heard about codepipeline which simply triggered by a source commit and start a pipeline to build application then deploy automatically to ec2 servers. Upload files to aws ec2. Well this article is not about code pipeline but a simple script that will make a build in your system and upload directly to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":375,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,1],"tags":[27,28,29],"class_list":["post-371","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","category-uncategorized","tag-aws","tag-ec2","tag-file-transfer-from-local-to-ec2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Upload files into AWS EC2 using aws cli<\/title>\n<meta name=\"description\" content=\"Upload files into AWS EC2 using aws cli. So let&#039;s create a file sandbox.sh file or any file name you wish to begin.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Upload files into AWS EC2 using aws cli\" \/>\n<meta property=\"og:description\" content=\"Upload files into AWS EC2 using aws cli. So let&#039;s create a file sandbox.sh file or any file name you wish to begin.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/\" \/>\n<meta property=\"og:site_name\" content=\"Santosh Kumar Shah\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-21T07:29:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-24T05:18:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1090\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Santosh Kumar Shah\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Santosh Kumar Shah\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/\",\"url\":\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/\",\"name\":\"Upload files into AWS EC2 using aws cli\",\"isPartOf\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png\",\"datePublished\":\"2023-04-21T07:29:16+00:00\",\"dateModified\":\"2023-04-24T05:18:59+00:00\",\"author\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\"},\"description\":\"Upload files into AWS EC2 using aws cli. So let's create a file sandbox.sh file or any file name you wish to begin.\",\"breadcrumb\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#primaryimage\",\"url\":\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png\",\"contentUrl\":\"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png\",\"width\":1090,\"height\":600},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/santosh-shah.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Upload files into AWS EC2 using aws cli\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\",\"url\":\"https:\/\/santosh-shah.com\/blog\/\",\"name\":\"Santosh Kumar Shah\",\"description\":\"JavaScript Developer\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\",\"name\":\"Santosh Kumar Shah\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"caption\":\"Santosh Kumar Shah\"},\"description\":\"I am JavaScript developer.\",\"sameAs\":[\"https:\/\/santosh-shah.com\/blog\"],\"url\":\"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Upload files into AWS EC2 using aws cli","description":"Upload files into AWS EC2 using aws cli. So let's create a file sandbox.sh file or any file name you wish to begin.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/","og_locale":"en_US","og_type":"article","og_title":"Upload files into AWS EC2 using aws cli","og_description":"Upload files into AWS EC2 using aws cli. So let's create a file sandbox.sh file or any file name you wish to begin.","og_url":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/","og_site_name":"Santosh Kumar Shah","article_published_time":"2023-04-21T07:29:16+00:00","article_modified_time":"2023-04-24T05:18:59+00:00","og_image":[{"width":1090,"height":600,"url":"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png","type":"image\/png"}],"author":"Santosh Kumar Shah","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Santosh Kumar Shah","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/","url":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/","name":"Upload files into AWS EC2 using aws cli","isPartOf":{"@id":"https:\/\/santosh-shah.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#primaryimage"},"image":{"@id":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#primaryimage"},"thumbnailUrl":"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png","datePublished":"2023-04-21T07:29:16+00:00","dateModified":"2023-04-24T05:18:59+00:00","author":{"@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f"},"description":"Upload files into AWS EC2 using aws cli. So let's create a file sandbox.sh file or any file name you wish to begin.","breadcrumb":{"@id":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#primaryimage","url":"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png","contentUrl":"https:\/\/santosh-shah.com\/blog\/wp-content\/uploads\/2023\/04\/image.png","width":1090,"height":600},{"@type":"BreadcrumbList","@id":"https:\/\/santosh-shah.com\/blog\/upload-files-into-aws-ec2-using-aws-cli\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/santosh-shah.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Upload files into AWS EC2 using aws cli"}]},{"@type":"WebSite","@id":"https:\/\/santosh-shah.com\/blog\/#website","url":"https:\/\/santosh-shah.com\/blog\/","name":"Santosh Kumar Shah","description":"JavaScript Developer","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f","name":"Santosh Kumar Shah","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","caption":"Santosh Kumar Shah"},"description":"I am JavaScript developer.","sameAs":["https:\/\/santosh-shah.com\/blog"],"url":"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/"}]}},"_links":{"self":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/comments?post=371"}],"version-history":[{"count":3,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":377,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/371\/revisions\/377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/media\/375"}],"wp:attachment":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}