unable to get default aws region using powershell in azure pipelines

Viewed 26

I use AWS Tools for Powershell to set and get the Default AWS region using modules. I used Set-DefaultAWSRegion -Region us-east-2 to set the defaultAWSRegion in azure-pipelines.yaml using task. However, when trying to read the value using Get-DefaultAWSRegion I get no response.

If I write a PowerShell script and run the script in azure-pipelines.yaml, I get the region value.

Here is the yaml file:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
variables:
  - group: app-one
  - name: region
    value: 'us-east-2'
  - name: profile
    value: 'PSProfile'
trigger:
  - install-awstools
  
pool:
    vmImage: ubuntu-latest
  
steps:
  - task: PowerShell@2
    displayName: 'Check and install AWS.Tools.Installer and AWS.Tools.Common modules'
    inputs:
      filePath: '$(build.sourcesdirectory)/install-aws-tools-powershell/InstallAWSTools.ps1'
  - task: PowerShell@2
    displayName: 'Set AWS Credentail'
    inputs:
      targetType: 'inline'
      script: 'Set-AWSCredential -AccessKey $(access_key) `
                -SecretKey $(secret_key) `
                -StoreAs $(profile)'
  - task: PowerShell@2
    displayName: 'Run region test'
    inputs:
      filePath: '$(build.sourcesdirectory)/install-aws-tools-powershell/regiontest.ps1'
  - task: PowerShell@2
    displayName: 'Set AWS DefaultRegion'
    inputs:
      targetType: 'inline'
      script: 'Set-DefaultAWSRegion -Region $(region)'
  - task: PowerShell@2
    displayName: 'Get AWS DefaultRegion'
    inputs:
      targetType: 'inline'
      script: 'Get-DefaultAWSRegion'
    continueOnError: true

However, when I call the functions via a powershell script like:

Set-DefaultAWSRegion -Region us-east-2
Get-DefaultAWSRegion

I get the desired behavior.

Region    Name           IsShellDefault
------    ----           --------------
us-east-2 US East (Ohio)           True

What am I missing?

After some more research: The below code snippet works:

  - task: PowerShell@2
    displayName: 'Set AWS DefaultRegion'
    inputs:
      targetType: 'inline'
      script: |
        Set-DefaultAWSRegion -Region $(region)
        $RegionValue = (Get-DefaultAWSRegion) | Out-String
        Write-Host "Value of AWS DefaultRegion: $RegionValue"

But when I break the script into two tasks, as shown below, it does not work:

  - task: PowerShell@2
    displayName: 'Set AWS DefaultRegion'
    inputs:
      targetType: 'inline'
      script: |
        Set-DefaultAWSRegion -Region $(region)

  - task: PowerShell@2
    displayName: 'Get AWS DefaultRegion'
    inputs:
      targetType: 'inline'
      script: |
        $RegionValue = (Get-DefaultAWSRegion) | Out-String
        Write-Host "Value of AWS DefaultRegion: $RegionValue"
    continueOnError: true

The DefaultAWSRegion value does not persist beyond a task.

0 Answers
Related