- Newest
- Most votes
- Most comments
This is a real behavior with Set-DefaultAWSRegion in AWS Tools for PowerShell (especially v4.x and above), and it has caught a few folks off guard recently.
Set-DefaultAWSRegion us-west-2 -->some commands like Get-EC2SecurityGroup may still default to us-east-1 unless:
You explicitly pass -Region Or the session context is retained properly.
This isn’t technically a “bug”, but it’s inconsistent behavior due to how region context is managed internally between module versions and cmdlet families (EC2, S3, etc.).
Try with the below steps:
-
Use Initialize-AWSDefaultConfiguration (Recommended) Instead of Set-DefaultAWSRegion, try: a). Initialize-AWSDefaultConfiguration -Region us-west-2 This sets the region globally for the current PowerShell session, and all AWS cmdlets will respect it (including EC2-related ones like Get-EC2SecurityGroup). a) This is the officially supported method in AWS Tools for PowerShell v4+.
-
If you prefer an explicit region: Get-EC2SecurityGroup -Region us-west-2 But of course, that defeats the point of setting a session-wide default.
-
Double-check: You're using the AWSPowerShell.NetCore module (not the legacy AWSPowerShell) Your AWS profile (if used) doesn't override region settings. Your PowerShell session isn't resetting between commands (e.g., in VSCode terminals or background jobs)
Initialize-AWSDefaultConfiguration -Region us-west-2 Get-EC2SecurityGroup You should now get your security groups from us-west-2 without needing -Region.
answered a year ago
Do you use named profile. If so, can you check if a default region has been set in the named profile. It could be the default region in the named profile is taking precedence. The named profiles are in this location C:\Users\{user_id}\.aws\config.
Also, when you setting your default region via powershell, can you try running - Get-DefaultAWSRegion - and confirm if it's returning the correct region.
It appears that you're experiencing an unexpected behavior with the Get-EC2SecurityGroup cmdlet in the AWS Tools for PowerShell. This behavior, where the cmdlet seems to ignore the default region set by Set-DefaultAWSRegion, is not the intended functionality. Typically, AWS Tools for PowerShell should respect the default region setting.
To address this issue, you can try the following steps:
-
Verify that the default region is set correctly: After using Set-DefaultAWSRegion, you can confirm the current default region by running Get-DefaultAWSRegion.
-
Explicitly specify the region in your command: While this isn't ideal, you can use the -Region parameter with Get-EC2SecurityGroup to ensure it uses the correct region. For example: Get-EC2SecurityGroup -Region us-west-2
-
Check your AWS CLI configuration: Ensure that your AWS CLI configuration (usually in ~/.aws/config) doesn't have conflicting region settings.
-
Verify your IAM permissions: Make sure your IAM user or role has the necessary permissions to describe security groups in the us-west-2 region.
-
Clear and reset your AWS credentials: Try clearing your AWS credentials and setting them again using Set-AWSCredential.
If the issue persists after trying these steps, it may indeed be a bug in the current version of the AWS Tools for PowerShell. In that case, you might want to report this issue to AWS support or on the AWS developer forums. You could also try rolling back to a previous version of the module where you remember it working correctly.
As a workaround, you can create a function that wraps Get-EC2SecurityGroup and always includes the -Region parameter based on your default region:
function Get-EC2SecurityGroupWithRegion { $region = (Get-DefaultAWSRegion).Region Get-EC2SecurityGroup -Region $region @args }
This function will use the default region you've set and pass any additional arguments to Get-EC2SecurityGroup.
Sources
Select a Region for your Amazon EC2 resources - Amazon Elastic Compute Cloud
Amazon EC2 examples using Tools for PowerShell - AWS SDK Code Examples
answered a year ago
Well, I'm sorry about that. My mistake. We're using federated Ids and SSO and when I configured that in the 'config' file, I made a mistake and had the wrong account number. So, nevermind. This is working now.
answered a year ago
Relevant content
asked a year ago
asked 2 years ago
asked 5 years ago
- AWS OFFICIALUpdated 3 years ago

I do have a region=us-west-2 line in my config file for the profile in question. Get-DefaultAWSRegion does return the expected region (with 3 properties actually). So, I did something and now even "Get-EC2SecurityGroup -region us-west-2" does not return anything. I can see in the web console that there are 92 security groups in us-west-2. I have even rebooted my computer. So, I'll have to figure that out. And, apparently I forgot to mention originally, the command Get-EC2ManagedPrefixList does work and return a bunch of prefixes. Also, "Initialize-AWSDefaultConfiguration -Region us-west-2" does not seem to make any difference. It looks like I need to start uninstalling and installing older versions of the module to work toward a working state again.