Search code examples
amazon-web-servicespowershellaws-codepipelineaws-code-deploy

How to update variable values of a powershell script using codedeploy


I'm using AWS CodeDeploy and codepipeline for my deployment,and I have a PowerShell script (bootstrap-base.ps1) which will be part of ami, resides with-in D:\scripts and I need to update variables of the script during the deployment process. Here's a simplified version of my script:

# bootstrap-base.ps1
$bootstrapRepository = "testcd" ;
$environment = "QA" ;

param(
  [String]
  $bootstrapRepository,
  
  [ValidateSet("QA","PROD","PREPROD","TRAIN","DR","OPS", IgnoreCase = $false)]
  [String]
  $environment,

  # Other parameters...
)
# Rest of the script...

I need to dynamically update the values of $bootstrapRepositoryand $environment during the deployment. Is there any option to update the values using appspec.yamlwith AWS CodeDeploy, or do I need to follow a different approach.

how to update PowerShell script variables during an AWS CodeDeploy deployment


Solution

  • If I were you I would write the powershell script such that it can work with variables. You can use command line arguments this way -

    # Access command-line arguments
    $env = $args[0] -or "dev"
    $ec2ip = $args[1]
    
    Write-Host "environment: $env"
    Write-Host "ec2 ip: $ec2ip"
    

    And run the script inside my cicd pipeline or where ever you want -

    powershell script_name.ps1 "stage" "172.168.10.23"