aws-sam-cli
aws-sam-cli copied to clipboard
Running sam validate produces an AttributeError
Description:
Steps to reproduce the issue:
- sam validate
Observed result:
File "/usr/local/bin/sam", line 11, in <module>
load_entry_point('aws-sam-cli==0.47.0', 'console_scripts', 'sam')()
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/click/decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/samcli/lib/telemetry/metrics.py", line 96, in wrapped
raise exception # pylint: disable=raising-bad-type
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/samcli/lib/telemetry/metrics.py", line 62, in wrapped
return_value = func(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/samcli/commands/validate/validate.py", line 27, in cli
do_cli(ctx, template_file) # pragma: no cover
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/samcli/commands/validate/validate.py", line 47, in do_cli
validator.is_valid()
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/samcli/commands/validate/lib/sam_template_validator.py", line 60, in is_valid
template = sam_translator.translate(sam_template=self.sam_template, parameter_values={})
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/samtranslator/translator/translator.py", line 88, in translate
sam_parameter_values.add_pseudo_parameter_values()
File "/usr/local/Cellar/aws-sam-cli/0.47.0/libexec/lib/python3.7/site-packages/samtranslator/sdk/parameter.py", line 73, in add_pseudo_parameter_values
if region.startswith("cn-"):
AttributeError: 'NoneType' object has no attribute 'startswith'
Expected result: The function to to tell me whether or not my yml file is valid.
Template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
recipe-monster
Sample SAM Template for recipe-monster
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
RecipeMonsterFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: src/
Handler: app.lambda_handler
Runtime: python3.8
Events:
RecipeMonster:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /recipes
Method: POST
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
RecipeMonsterApi:
Description: "API Gateway endpoint URL for Prod stage for Recipe Monster function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
RecipeMonsterFunction:
Description: "Recipe Monster Lambda Function ARN"
Value: !GetAtt RecipeMonsterFunction.Arn
RecipeMonsterFunctionIamRole:
Description: "Implicit IAM Role created for Recipe Monster function"
Value: !GetAtt RecipeMonsterFunctionRole.Arn
@brukhabtu SAM expects a region to be set in the boto3 or aws configuration in order to do validation. If you run aws configure and set a region, this validate call should work.
agree with @keetonian comment, but will keep this open so that the an appropriate error message is thrown instead.
@brukhabtu SAM expects a region to be set in the boto3 or aws configuration in order to do validation. If you run
aws configureand set a region, this validate call should work.
I had the exact same problem with the sam cli even though I already had region=us-east-1 set in my ~/.aws/credentials and the aws cli was able to reach other services on AWS. I also tried adding --region us-east-1 at the end of sam validate --profile MyProfile.
I'm on SAM CLI, version 0.49.0
UPDATE: When I ran export AWS_DEFAULT_REGION=us-east-1 before re-running sam validate, the error went away.
Same issue, and same hacky fix that @ystoneman describes let me continue working.
Running: sam validate --profile personal
Get the same error as the OP shared
SAM CLI, version 0.51.0
I have no default profile, using a named profile which has region configured in my config file and otherwise works just fine:
[personal] region = eu-west-2
It seems that the syntax in .aws/config is a little quirky. I've got same error as everyone else with:
[home]
region = us-west-2
but the it worked with:
[profile home]
region = us-west-2
This is different from the convention in .aws/credentials, where "profile" is not used.
In my case was set the file .aws/config to
[default]
region = ap-southeast-2
and then sam validate worked
SAM CLI version 1.2.0
First error in the output for me was on line 33 instead of 11 as stated in OPs post.
Can confirm the region not being set was also my issue. Also, I keep multiple profiles on my box.
What does NOT work is setting export AWS_REGION=<your region> even after running export AWS_PROFILE=<your profile>
How to fix w/ multiple profiles...
In ~/.aws/config:
[profile yourProfileName]
region = your-region-here
output = json (this is optional of course)
[profile otherProfile]
...
In ~/.aws/credentials:
[yourProfileName] <-- note w/o the word "profile"
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
[otherProfile]
...
Then run export AWS_PROFILE=yourProfileName
Lastly, you can finally run sam validate
For LOLz, I just started trying to learn SAM-CLI just after running brew upgrade which updated a ton of stuff, including Python ... so to see python errors start to roll around right after ... (╯ಠ_ಠ)╯︵ ┻━┻
@brukhabtu SAM expects a region to be set in the boto3 or aws configuration in order to do validation. If you run
aws configureand set a region, this validate call should work.I had the exact same problem with the sam cli even though I already had
region=us-east-1set in my~/.aws/credentialsand the aws cli was able to reach other services on AWS. I also tried adding--region us-east-1at the end ofsam validate --profile MyProfile.I'm on SAM CLI, version 0.49.0
UPDATE: When I ran
export AWS_DEFAULT_REGION=us-east-1before re-runningsam validate, the error went away.
I had the exact same problem. I have multiple profiles that defined in .aws/config and .aws/credentials. The weird thing is, it worked pretty well on my local macbook while I have the same versions of SAM CLI on my macbook and my gitlab server. I feel sam failed to retrieve the region value from the --region parameter. Anyways, export AWS_DEFAULT_REGION=$var_region
I am on 4.14.186-146.268.amzn2.x86_64 and SAM CLI, version 1.6.2
sam version 1.15.0, AWS CLI version aws-cli/2.1.13 Python/3.7.4 Darwin/19.6.0 exe/x86_64 prompt/off
this didn't worked
sam validate -t template.yaml --profile private_user1 --region eu-central-1 --debug
but when I export AWS_DEFAULT_REGION=eu-central-1 worked.
setting it from aws config --profile private_user1 also didn't help
SAM expects a region to be set in the boto3 or aws configuration in order to do validation. If you run aws configure and set a region, this validate call should work.
@sriram-mv , @keetonian , sam deploy works differently with respect to the region parameter.
The expectation here is that is that sam validate work as sam deploy does.
Instead it seems that the validate subcommand is ignoring SAM's built-in ways to set the region.
Here's an example.
I'm working with SAM version 1.15.0.
$ sam --version
SAM CLI, version 1.15.0
My AWS CLI config does not set the region (sso_region is something else).
[profile saa-tfb]
sso_start_url = https://d-1234567890.awsapps.com/start
sso_region = eu-west-1
sso_account_id = 123456789012
sso_role_name = AdministratorAccess
The region does get set in my samconfig.toml.
version=0.1
[default.deploy.parameters]
stack_name = "TerraformBackend"
region = "us-west-1"
config_changeset = true
capabilities = "CAPABILITY_IAM"
tags = "ApplicationTagValue=TerraformBackend"
With this config a vanilla AWS CLI command that needs regional context will indeed fail.
$ aws cloudformation describe-stacks --profile saa-tfb
You must specify a region. You can also configure your region by running "aws configure".
But the sam deploy command picks up the region from samconfig.toml and the deployment initiates.
$ sam deploy --profile saa-tfb
Deploying with following values
===============================
Stack name : TerraformBackend
Region : us-west-1
Confirm changeset : False
Deployment s3 bucket : None
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {}
Signing Profiles : {}
I would expect sam validate to pick up the region in the same way.
Instead it dumps an ugly traceback because an internal region variable becomes None where it isn't expected.
$ sam validate --profile saa-tfb
Traceback (most recent call last):
File "/home/isme/.local/bin/sam", line 8, in <module>
sys.exit(cli())
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/click/decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/samcli/lib/telemetry/metrics.py", line 148, in wrapped
raise exception # pylint: disable=raising-bad-type
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/samcli/lib/telemetry/metrics.py", line 114, in wrapped
return_value = func(*args, **kwargs)
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/samcli/commands/validate/validate.py", line 32, in cli
do_cli(ctx, template_file) # pragma: no cover
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/samcli/commands/validate/validate.py", line 52, in do_cli
validator.is_valid()
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/samcli/commands/validate/lib/sam_template_validator.py", line 61, in is_valid
template = sam_translator.translate(sam_template=self.sam_template, parameter_values={})
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/samtranslator/translator/translator.py", line 95, in translate
sam_parameter_values.add_pseudo_parameter_values()
File "/home/isme/.local/pipx/venvs/aws-sam-cli/lib/python3.8/site-packages/samtranslator/sdk/parameter.py", line 73, in add_pseudo_parameter_values
if region.startswith("cn-"):
AttributeError: 'NoneType' object has no attribute 'startswith'
As others here have observed, the traceback occurs even when setting the --region option explcitly.
I faced the same issue with:
aws-cli/1.18.69 Python/3.8.5 Linux/5.4.0-65-generic botocore/1.16.19
SAM CLI, version 1.18.1
Basically I created a new set of IAM credentials which I stored in ~/.aws/credentials but I didn't create a corresponding profile entry in ~/.aws/config. Then I exported AWS_PROFILE=adam (that matched the section in ~/.aws/credentials) and the result was:
Traceback (most recent call last):
File "samcli/__main__.py", line 12, in <module>
File "click/core.py", line 829, in __call__
File "click/core.py", line 782, in main
File "click/core.py", line 1259, in invoke
File "click/core.py", line 1066, in invoke
File "click/core.py", line 610, in invoke
File "click/decorators.py", line 73, in new_func
File "click/core.py", line 610, in invoke
File "samcli/lib/telemetry/metric.py", line 152, in wrapped
File "samcli/lib/telemetry/metric.py", line 121, in wrapped
File "samcli/lib/utils/version_checker.py", line 42, in wrapped
File "samcli/commands/validate/validate.py", line 34, in cli
File "samcli/commands/validate/validate.py", line 54, in do_cli
File "samcli/commands/validate/lib/sam_template_validator.py", line 61, in is_valid
File "samtranslator/translator/translator.py", line 95, in translate
File "samtranslator/sdk/parameter.py", line 73, in add_pseudo_parameter_values
AttributeError: 'NoneType' object has no attribute 'startswith'
[422965] Failed to execute script __main__
A solution was either to:
- export
AWS_DEFAULT_REGION=eu-west-1, or - create the appropriate section for the profile to be used in
~/.aws/config, like[profile adam] region = eu-west-1 output = json
Hi there,
This error (no region is been configured) has been gracefully handled at the moment with following output;
sam validate
2023-01-10 12:00:25 Attaching import module proxy for analyzing dynamic imports
2023-01-10 12:00:25 Loading policies from IAM...
2023-01-10 12:00:30 Finished loading policies from IAM.
Error: AWS Region was not found. Please configure your region through a profile or --region option
Resolving this issue for now.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.