Aws-sam-cli: Inline DefinitionBody always returns "Specify either 'DefinitionUri' or 'DefinitionBody' property and not both"

Created on 16 May 2018  路  8Comments  路  Source: aws/aws-sam-cli

Same issue as: https://github.com/awslabs/serverless-application-model/issues/432, copying most of the content as I have the same exact issue.

Description:

Whenever DefinitionBody is used I seem to always get a validation error saying to Specify either 'DefinitionUri' or 'DefinitionBody' property and not both.

The template defines only a single instance of DefinitionBody, there is no reference to DefinitionUri.

Steps to reproduce the issue:

  1. Go to the example given in this repo: https://github.com/awslabs/serverless-application-model/blob/develop/examples/2016-10-31/inline_swagger/template.yaml
  2. Run sam validate

Observed result:
Error: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyApi] is invalid. Specify either 'DefinitionUri' or 'DefinitionBody' property and not both

Expected result:
Pass validation

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

$ aws --version
aws-cli/1.15.20 Python/2.7.9 Windows/8 botocore/1.10.20
$ sam --version
SAM CLI, version 0.3.0
arevalidate contributorgood-first-issue priorit2-important stagin-review typbug

Most helpful comment

Merged, yet to be released

All 8 comments

@mariotacke Looks like we missed a case here. Looks we just need to check if DefinitionBody is in the Properties before trying to override the URI.

Updating labels but @mariotacke would you like to take a stab at it?

@jfuss, I tried for about an hour but Python is not my strong suit. I don't think I have the expertise to build a proper unit test for this scenario. Would love to have someone with more experience look at this instead.

This is what I tried, though I'm not sure if that's the right way to go:

if resource_type == "AWS::Serverless::Api":
    if "DefinitionBody" in resource_dict:
        SamTemplateValidator._update_to_s3_uri("DefinitionBody", resource_dict)
    else:
        SamTemplateValidator._update_to_s3_uri("DefinitionUri", resource_dict)

@mariotacke It is close! You don't need to replace the value for DefinitionBody with an S3 URI. You can condense this to

if resource_type == "AWS::Serverless::Api":
    if not "DefinitionBody" in resource_dict:
        SamTemplateValidator._update_to_s3_uri("DefinitionUri", resource_dict)

So I will give some context on why that is and maybe it will clear things up. Customers have two ways to define their swagger in an AWS::Serverless::Api, (DefinitionBody or DefinitionUri). The DefinitionBody is where you would inline your swagger. So from a local point of view, you don't need to say where the file is on your machine. On the other hand, DefinitionUri is an S3 uri to tell SAM where to find the swagger definition. In this case, it makes sense for this to be a path to the file on your machine. But for SAM CLI, we do not require the DefinitionUri to be in the template and if it is not, we want to make sure the template can pass the transform on SAM (locally). So in order to do that we need to add the key or update it to be a what looks like a valid S3 Uri.

If you don't feel you have the expertise or want to do it, don't. You won't hurt anyone's feelings. We do appreciate the attempt in fixing it. I am happy to guide and help, if you do choose to continue otherwise I can work on getting this into an upcoming sprint for us.

Thank you for the explanation. Please prioritize it for your upcoming sprints.

facing same issue.. tried

generate next stage yaml file

aws cloudformation package \
sed -i '' '/DefinitionUri/d' packaged-template.yaml\
--template-file template.yaml \
--output-template-file build/output.yaml \
--s3-bucket $BUCKET

the actual deployment step

aws cloudformation deploy \
--template-file build/output.yaml \
--stack-name $PROJECT \
--capabilities CAPABILITY_IAM \
--parameter-overrides Environment=$STAGE \
but no progress.
have any body been able to solve this issue.

@workforfilmsGithub This issue is not related to package and deploy commands. It is isolated only to the validate command.

is there a way to solve it on validate command.

Merged, yet to be released

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dschu-lab picture dschu-lab  路  3Comments

terlar picture terlar  路  3Comments

cmccoypdx picture cmccoypdx  路  3Comments

chestongo picture chestongo  路  3Comments

rhlsthrm picture rhlsthrm  路  4Comments