aws / aws-toolkit-vscode

Amazon Q, CodeCatalyst, Local Lambda debug, SAM/CFN syntax, ECS Terminal, AWS resources
https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.amazon-q-vscode
Apache License 2.0
1.48k stars 413 forks source link

Cannot resolve the correct AWS Region locally. #2380

Open kunal-stagepilot opened 2 years ago

kunal-stagepilot commented 2 years ago

Describe the bug ${AWS::Region} always resolved to us-east-1 when debugging SAM application locally via VS Code AWS Toolkit.

My lambda function has a layer

Layers:
        - !Sub "arn:aws:lambda:${AWS::Region}:898466741470:layer:psycopg2-py38:1"

While this works when I deploy my application stack to my AWS instance, it incorrectly resolves the region to us-east-1 when I debug locally. My default profile region is set to eu-west-1.

I added this to my debug configuration file, but ${AWS::Region} still resolved to us-east-1

            "aws": {
                "credentials": "profile:default",
                "region": "eu-west-1"
            },

Next, I tried adding buildArguments and localArguments to the debug config file:

            "sam": {
                "containerBuild": false,
                "skipNewImageCheck": false,
                "buildArguments": [
                    "--region eu-west-1"
                ],
                "localArguments": [
                    "--region eu-west-1"
                ]
            },

When debugging, I get this error Error: no such option: --region eu-west-1 It fails on this command: Note: I've truncated paths for brevity

2021-12-16 12:45:33 [INFO]: Running command: (not started) [(path to sam)\sam.cmd build --build-dir (user path)\aws-toolkit-vscode\vsctk6pwmOB\output --template (repo path)/virtual-events-admin/template.yaml --manifest (user path)\aws-toolkit-vscode\vsctk6pwmOB\debug-requirements.txt --region eu-west-1]

I can copy/paste and run the command above in terminal and it builds without any issues. It builds successfully.

To Reproduce

  1. In your SAM template, include an resource ARN which contains ${AWS::Region}.
  2. Debug the SAM application locally, you will notice ${AWS::Region} resolves to us-east-1 regardless of which region you set.

Expected behavior

  1. ${AWS::Region} should resolve to the region set aws/region key in the SAM debug config file.
  2. If region build or local arguments are included, in the SAM debug config file, local debugging shouldn't throw an error

Computer (please complete the following information):

kunal-stagepilot commented 2 years ago

Workaround Hard code the ARN with the desired region in the template file locally.

awschristou commented 2 years ago

Hi @kunal-stagepilot are you working with the AWS Toolkit for Visual Studio or the AWS Toolkit for Visual Studio Code?

awschristou commented 2 years ago

It looks like you are referring to the VS Code toolkit, I will move the issue to that repo.

kunal-stagepilot commented 2 years ago

Thanks Christopher!

JadenSimon commented 2 years ago

Hey @kunal-stagepilot ,

Looks like this may be two separate bugs. I'll see what I can replicate on my end and report back.

I can copy/paste and run the command above in terminal and it builds without any issues. It builds successfully.

Now that is interesting. I'm wondering if the toolkit is doing something that causes --region to become an invalid option.

JadenSimon commented 2 years ago

Can confirm that AWS::Region is resolved to 'us-east-1' when local debugging despite setting a region and profile in the aws section. A minimal test case is to ref the region apart of an environment variable:

    Environment:
      Variables:
        MY-REGION: !Ref AWS::Region

Also, buildArguments/localArguments should be passed in without delimiters:

      "buildArguments": [
          "--region", "eu-west-1"
      ],
      "localArguments": [
          "--region", "eu-west-1"
      ]

That's why there was an error. We're passing in the arguments as-is with no shell. Using the above correctly set my environment variable to 'eu-west-1'. I didn't notice this the first read-through since the Toolkit's output makes it look like the command is correct when it actually isn't.

Of course, the fact that the aws parameters don't do anything is also confusing. It looks like we only use those fields inside the lambda as opposed to invoking SAM CLI. We may need to rethink how this is done since this behavior is clearly counterintuitive.

justinmk3 commented 2 years ago

Related: https://github.com/aws/aws-toolkit-vscode/issues/2011