'Sh command to create EC2 instance from return statement of ActiveChoiceParam in Jenkins DSL pipeline

Below are the parameters where it should create a new EC2 instance through the return statement of activeChoiceReactiveParam, if I select New_ENV

Here, I've added the SH command to create AWS EC2 but it's not working, any help to resolve the same is appreciated

pipelineJob("${DSL_JOB}") {
definition {
    cps { 
     script(template)
    }
}
parameters {
  activeChoiceParam('SELECT_ENV') {
   description('select environment to deploy')
   choiceType('SINGLE_SELECT')
   groovyScript {
       script('return["Existing_ENV","New_ENV"]')
       fallbackScript('return ["error"]')
   }
 }
 activeChoiceReactiveParam('Select ENV to deploy') {
   choiceType('SINGLE_SELECT')
   groovyScript {
       script(' 
          if(SELECT_ENV.equals("Existing_ENV")) { 
              return ["dev1", "dev2", "dev3", "dev4" ]
          } 
          else { 
              return ["sh 'aws ec2 run-instances --image-id <image_ami_id> --count 1 --instance-type t2.micro  --key-name <key_name>' "] 
          } 
       ')
       fallbackScript('return ["error"]')
   }
   referencedParameter('SELECT_ENV')
  }
 }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source