Having a separate state file each for infrastructure and application makes sense.
Is there any existing tool (built-in or not) to help with this? Sort of like terraform state mv but between tfstates?
No, as far as I can tell. Move out shared parts (e.g. ECS clusters, ALB, network configuration, iam roles etc.) into a separate project/repository.
When using S3 as state backend you can define different paths for your infrastructure and application states, for example:
/infrastructure/nonprod/terraform.tfstate
/infrastructure/prod/terraform.tfstate
/apps/app1/test/terraform.tfstate
/apps/app1/uat/terraform.tfstate
/apps/app1/prod/terraform.tfstate
When you want to deploy your application to TEST or UAT you simply call terraform init
before terraform apply
in your infrastructure project by providing the path to your non-prod S3 state. Then call terraform init
on your app terraform config by providing the path to your TEST or UAT path.
Ideally, you can create your own shell scripts to provision and deploy your apps. Then in your favourite CI you can create a pipeline to provision infrastructure and deploy apps as you wish. Make sure you parameterize those scripts so you can pass which environment you want to provision or which app you want to deploy, for example:
./my-shared-infrastructure/provision-infrastructure.sh nonprod
./my-app-1/deploy-application.sh uat v1.0
terraform state list
and then loop over withrm
+import
... – munchybunch