Import Existing Resources Into Terraform code

Import Existing Resources Into Terraform code

Share Everywhere

Table of contents

Step by step guide to import resources into Terraform code:

Confirm the state before import:

The first thing that you have to do is uing terraform plan command to confirm the current state.

Result:

Step by step guide to import resources into Terraform code:

To make our system’s state be stable, we have to keep the change to the state as small as possible. As you can see in the above image, there are 3 resources to add and 2 resources that have to be change.

Our task now is to make changes to the state in tfstate file of 2 following resources:

  • outer_elb
  • outer_elb[1]

by import 2 resources below in to the terraform code:

  • front_access_sg
  • front_access_vendor_sg

Importing resources:

Step 1/ Remove 2 objects in tfstate file:

You can search for the location of the two in terraform.tfstate.prd file by keywords:

"name": "front_a_sg" và "name": "front_b_sg"

Step 2/ Import the resources:

  • Log in to the console and find the resources wanting to import.
  • Copy the ids of the resources that you want to import. (in this case, the id of front_a_sg is sg-036ec74316xxxxx and that of front_b_sg is sg-0eeda2xxxx)
  • Run terraform import command to import existing resources to the state file:
  • Import front_a_sg with id sg-036ec74316xxxx:
/usr/local/src/terraform/terraform_0.12 import -var-file=./tfvars/prd.tfvars -state=./tfstate/terraform.tfstate.prd --state-out=./tfstate/terraform.tfstate.prd.new aws_security_group.front_a_sg sg-036ec74316xxxx
  • Import front_b_sg with id: sg-0eeda2xxxx:
/usr/local/src/terraform/terraform_0.12 import -var-file=./tfvars/prd.tfvars -state=./tfstate/terraform.tfstate.prd.new --state-out=./tfstate/terraform.tfstate.prd.new aws_security_group.front_b_sg sg-0eeda2xxxx

Step 3/ Confirm the result by running terraform plan command again.

You should notice that the state change of system now is equal to 0.

Memo: Nếu có index:

/usr/local/src/terraform/terraform_0.14 import -var-file=./tfvars/stg.tfvars -state=./tfstate/terraform.tfstate.stg --state-out=./tfstate/terraform.tfstate.stg aws_db_parameter_group.rds_parameter_group[\"rds_instance_001\"] web-rds-pg

⇒ escaping the double quotes fixes that.

Syntax to use:

terraform import aws_iam_user.api_users[\\"foo\\"] foo
Bạn thấy bài viết này như thế nào?
0 reactions

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Image CAPTCHA
Enter the characters shown in the image.

Bài viết liên quan

Cùng mình đập hộp xem bên trong có gì nhé.

Câu chuyện học AWS nên học từ đâu và trở thành AWS Community Builder

Sau 2 tháng đăng ký làm thành viên của AWS Community Builder thì hôm nay mình đã nhận được quà của AWS từ Singapore.
Ngoài ra còn có rất nhiều câu chuyện và chia sẻ kỹ thuật hữu ích nữa

Ngoài ra còn có rất nhiều câu chuyện và chia sẻ kỹ thuật hữu ích nữa

Năm nay mình có dịp trở lại với Singapore, một đất nước rất xanh, sạch, và đẹp, ở đâu cũng thấy mọi người tập thể dục ngoài trời
Democratize analytics and machine learning with no-code AWS services

Democratize analytics and machine learning with no-code AWS services

Access to all data for fast analytics at scale is key for 360-degree projects involving data engineers
Train ML models quickly and cost-effectively with Amazon SageMaker

Train ML models quickly and cost-effectively with Amazon SageMaker

Training machine learning models at scale often requires significant investments
Go beyond insights to predictive analytics with Amazon Redshift ML and Amazon SageMaker Canvas

Go beyond insights to predictive analytics with Amazon Redshift ML and Amazon SageMaker Canvas

Organizations are managing more data than ever before, and data use is only continuing to expand.