In this blog post, I’ll be walking you through how to create Production and Development environments on Amazon’s Web Service. If you would like an introduction or a refresher on Terraform, please refer to my previous blog post. Without further ado, let’s get into this demo.
First, download the starter kit from Github and cd to the root directory:
git clone https://github.com/hashicorp/learn-terraform-code-organization
These are the files that are visible in your directory:
Terraform requires unique identifiers – in this case, prod or dev for each s3 resource – to create separate resources of the same type.
Next, edit the file “terraform.tfvars.example” with the following:
region = "us-east-1" (depends on your location) prod_prefix = "prod" dev_prefix = "dev"
Remove “.example” from the file name then run the follow commands:
terraform init terraform apply terraform destroy
Defining multiple environments in the same “main.tf” file may become hard to manage as you add more resources.
So now we are going to organize the current configuration by splitting it into two separate files
First lets make a copy of the main file for dev
cp main.tf dev.tf
Then we will rename main to prod
mv main.tf prod.tf
Then go through the separate prod & dev files and remove all references of prod in dev and all references of dev in prod.
Although our environments are in separate configurations, our “variables.tf” and “terraform.tfvars” files contain all the variable declarations and definitions for both environments. So we now have resources split between environments in “prod.tf” and “dev.tf” and our environments have unique identifiers to distinguish the region we are deploying to.
Terraform loads all configuration files within a directory and appends them together, which means that any resources or providers with the same name in the same directory will cause a validation error. So if we were to run a terraform command now, our “random_pet” resource and provider block would cause errors.
To fix this we’ll comment out provider and first resource in prod file. With prod’s shared resources commented out, our production environment will still inherit the value of the “random_pet” resource from our “dev.tf” file.
Lastly, we’ll update dev’s resource slightly:
Then run the files:
terraform apply terraform destroy
However, if we want to truly separate our environments we also need to separate the states as well. We can do by using directories or workspaces. For this webinar, we’ll be using directories because it is more practical.
Create a dev directory
mkdir prod && mkdir dev
Move the “dev.tf” file to the dev directory and rename it
mv dev.tf dev/main.tf
Next we’ll copy the “varibles.tf”, “terraform.tfvars” and “outputs.tf” files to the dev directory
cp outputs.tf terraform.tfvars variables.tf dev/
The last step would be to update the “content” path in “main.tf” since the assets folder is one level above now
The final step is to go through your dev directory and remove all references of prod in each file.
Now we’ll do the exact same thing for the prod directory. Create a prod directory.
mv prod.tf prod/main.tf
Next we’ll move the “variables.tf”, “terraform.tfvars” and “outputs.tf” files to the prod directory
mv outputs.tf terraform.tfvars variables.tf prod/
Un-comment the “provider” & “resource” declarations in “main.tf” from before
Now just cd into each directory and deploy the environments
terraform init terraform apply
Congratulations, you have successfully created a Production and Development environment on AWS using terraform. Feel free to visit your deployed app and play a few rounds of Tetris. When you are done, don’t forget to use “terraform destroy” to shut down your environments.
Cassandra.Link is a knowledge base that we created for all things Apache Cassandra. Our goal with Cassandra.Link was to not only fill the gap of Planet Cassandra but to bring the Cassandra community together. Feel free to reach out if you wish to collaborate with us on this project in any capacity.
We are a technology company that specializes in building business platforms. If you have any questions about the tools discussed in this post or about any of our services, feel free to send us an email!