{"id":19,"date":"2022-03-18T10:43:00","date_gmt":"2022-03-18T15:43:00","guid":{"rendered":"https:\/\/34.82.124.6\/?p=19"},"modified":"2022-04-29T10:44:23","modified_gmt":"2022-04-29T15:44:23","slug":"use-terraformer-to-import-aws-resources-in-linux","status":"publish","type":"post","link":"https:\/\/cloudlearning365.com\/?p=19","title":{"rendered":"Use terraformer to import AWS resources in linux"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I\u2019ve come to a situation to import existing resources into terraform, and this tool seems to be interesting:<br><a href=\"https:\/\/github.com\/GoogleCloudPlatform\/terraformer\">https:\/\/github.com\/GoogleCloudPlatform\/terraformer<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following example is in Linux, and my terraform installation is at \/user\/bin\/terraform<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ which terraform\n\/usr\/bin\/terraform<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By looking up the executable in following link:<br><a href=\"https:\/\/github.com\/GoogleCloudPlatform\/terraformer\/releases\">https:\/\/github.com\/GoogleCloudPlatform\/terraformer\/releases<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since I\u2019m running Linux and need to import resources in AWS, I\u2019ve downloaded:<br>terraformer-aws-linux-amd64 to \/user\/bin<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/usr\/bin\nsudo wget https:\/\/github.com\/GoogleCloudPlatform\/terraformer\/releases\/download\/0.8.19\/terraform\ner-aws-linux-amd64<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Verified that I do have AWS credentials in environment variables<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat ~\/.profile\n\n# environment variables\nexport AWS_ACCESS_KEY_ID='****'\nexport AWS_SECRET_ACCESS_KEY='****'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new folder for terraform files<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir ~\/terraformer<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create providers.tf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform {\n  required_providers {\n    aws = {\n      source  = \"hashicorp\/aws\"\n      version = \"~&gt; 4.0\"\n    }\n  }\n}\n\n# Configure the AWS Provider\nprovider \"aws\" {\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run terraform init<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ terraform init\n\nInitializing the backend...\n\nInitializing provider plugins...\n- Finding hashicorp\/aws versions matching \"~&gt; 4.0\"...\n- Installing hashicorp\/aws v4.5.0...\n- Installed hashicorp\/aws v4.5.0 (signed by HashiCorp)\n\nTerraform has made some changes to the provider dependency selections recorded\nin the .terraform.lock.hcl file. Review those changes and commit them to your\nversion control system if they represent changes you intended to make.\n\nTerraform has been successfully initialized!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">While I\u2019m trying to use Terraformer, I\u2019m getting an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ terraformer-aws-windows-amd64.exe import aws --resources=s3\n2022\/03\/18 11:57:29 aws importing default region\n2022\/03\/18 11:57:30 aws importing... s3\n2022\/03\/18 11:57:34 aws error initializing resources in service s3, err:<strong> no EC2 IMDS role found<\/strong>, operation error ec2imds: GetMetadata, exceeded maximum number of attempts, 3, request send failed, Get \"http:\/\/169.254.169.254\/latest\/meta-data\/iam\/security-credentials\/\": dial tcp 169.254.169.254:80: connectex: A socket operation was attempted to an unreachable network.\n2022\/03\/18 11:57:34 aws Connecting....<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After&nbsp;<a href=\"https:\/\/github.com\/GoogleCloudPlatform\/terraformer\/issues\/940\">some research&nbsp;<\/a>and the workaround is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ terraformer-aws-windows-amd64.exe import aws --resources=s3 --regions=us-west-1 <strong>--profile=\"\"<\/strong>\n\n2022\/03\/18 11:59:11 aws importing region us-west-1\n2022\/03\/18 11:59:13 aws importing... s3\n2022\/03\/18 11:59:16 aws done importing s3\n2022\/03\/18 11:59:16 Number of resources for service s3: 3\n2022\/03\/18 11:59:16 Refreshing state... aws_s3_bucket.tfer--pan-bootstrap-jye\n2022\/03\/18 11:59:16 Refreshing state... aws_s3_bucket.tfer--cf-templates-6vv0zllrvrqt-us-west-1\n2022\/03\/18 11:59:16 Refreshing state... aws_s3_bucket.tfer--bootstrap-dfa5077a6c367b5a\n2022\/03\/18 11:59:22 Filtered number of resources for service s3: 3\n2022\/03\/18 11:59:22 aws Connecting....\n2022\/03\/18 11:59:22 aws save s3\n2022\/03\/18 11:59:22 aws save tfstate for s3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">new folders gets created with generated terraform code and state file:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cloudlearning365.com\/wp-content\/uploads\/2022\/03\/image-5.png\" alt=\"\" class=\"wp-image-15\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve come to a situation to import existing resources into terraform, and this tool seems to be interesting:https:\/\/github.com\/GoogleCloudPlatform\/terraformer The following example is in Linux, and my terraform installation is at \/user\/bin\/terraform By looking up the executable in following link:https:\/\/github.com\/GoogleCloudPlatform\/terraformer\/releases Since &hellip; <a href=\"https:\/\/cloudlearning365.com\/?p=19\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,8],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-aviatrix","category-terraform"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19"}],"version-history":[{"count":1,"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":20,"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions\/20"}],"wp:attachment":[{"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudlearning365.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}