Suppose I have two environment Production and Staging and Now I want to provision t2.large in Staging and m5.large in Production. How to write a terraform script without using any conditional format.
Question posted in Amazon Web Sevices
The official Amazon Web Services documentation can be found here.
The official Amazon Web Services documentation can be found here.
2
Answers
you can use workspaces:
create your resources, for example:
initialize Terroform:
create a workspace:
plan and apply as usual.
This can be controlled by using the terraform workspace feature. The code would look similar to what Mohammed Ehab posted, but with some slight changes:
You could also create local variables that would help you control what gets deployed:
If you are going to use workspaces, make sure to switch between them when applying to the environment you want:
You would of course have to create the workspace first:
Alternatively, as workspaces come with their set of limitations, you could just create two different directories, define a variable and use that, e.g. for staging:
And then have a
terraform.tfvars
file in each of the directories with the following values:The code for the instance would look like this: