In this tutorial, you will create several AWS resources in a single configuration file and then divide them into compute and security group modules. Then, you will use the moved block to refactor your configuration and update resource IDs, and review the corresponding state changes before you apply the new configuration.
terraform init
terraform apply
Test your instance availability with the curl command.
curl $(terraform output -raw public_ip):8080
- Remove the AMI data source, the instance resource, and security group resource.
mkdir -p modules/compute
cd modules/compute
- Create a new
main.tf
file in thecompute
directory. - Then, create a new
outputs.tf
file to capture your instance IP address.
- Next, create a new directory for your
security_group
module. mkdir ../security_group
cd ../security_group
- Create a new
main.tf
file in thesecurity_group
directory. - Create a new
outputs.tf
file to capture your security group ID.
- Navigate to the root of your project directory.
cd ../..
- Open the
main.tf
file and add the new module blocks to the end of your configuration. - Update the
outputs.tf
file in the root directory.
- Next, reinitialize your configuration to install the newly created modules.
- Your module changes would delete and recreate your resources, which causes service interruptions in your infrastructure.
- In your root
main.tf
file, add moved configuration blocks. - Re-apply your configuration to move your resources into your new modules.
- You can also use the moved configuration block to rename existing resources.
- Rename your
vpc
module, and update the references to it in the rest of your configuration. - Add the
moved
block for your VPC changes at the bottom of the file. terraform init
terraform apply
https://learn.hashicorp.com/tutorials/terraform/move-config?in=terraform/modules