skip to Main Content

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.

2

Answers


  1. you can use workspaces:
    create your resources, for example:

    # Define provider AWS
    provider "aws" {
      region = "your_aws_region"
    }
    
    # Create an EC2 instance
    resource "aws_instance" "staging_instance" {
      ami           = "ami-xxxxxxxxxxxxxx"   # Enter your desired AMI ID
      instance_type = "t2.large"
      subnet_id     = "your_subnet_id"       # Enter your subnet ID
      key_name      = "your_key_pair_name"   # Enter your key pair name
      tags = {
        Name = "staging_instance"
        Environment = "staging"
      }
    }
    
    

    initialize Terroform:

    terraform init
    

    create a workspace:

    terraform workspace new staging
    

    plan and apply as usual.

    Login or Signup to reply.
  2. 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:

    resource "aws_instance" "this" {
      .
      .
      .
      instance_type = terraform.workspace == "staging" ? "t2.large" : "m5.large"
      .
      . 
      .
    
      tags = {
        Name        = "${terraform.workspace}_instance"
        Environment = terraform.workspace
      }
    }
    

    You could also create local variables that would help you control what gets deployed:

    locals {
      instance_type = terraform.workspace == "staging" ? "t2.large" : "m5.large"
      tags = {
        Environment = terraform.workspace
        Name        = "${terraform.workspace}_instance"
      }
    }
    
    resource "aws_instance" "this" {
      .
      .
      .
      instance_type = local.instance_type
      .
      . 
      .
    
      tags = local.tags
    }
    

    If you are going to use workspaces, make sure to switch between them when applying to the environment you want:

    terraform workspace select <name of the workspace>
    

    You would of course have to create the workspace first:

    terraform workspace new <name of the workspace>
    

    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:

    variable "env" {
      type        = string
      description = "Environment to deploy the code into."
    }
    
    variable "instance_type" {
      type        = string
      description = "EC2 instance type to deploy."
    }
    

    And then have a terraform.tfvars file in each of the directories with the following values:

    # staging directory
    env           = "staging"
    instance_type = "t2.large"
    
    # production directory
    env           = "production"
    instance_type = "m5.large"
    

    The code for the instance would look like this:

    resource "aws_instance" "this" {
      .
      .
      .
      instance_type = var.instance_type
      .
      . 
      .
    
      tags = {
        Name        = "${var.env}_instance"
        Environment = var.env
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search