serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Feature request: enable more modular swagger.yaml and template.yaml

Open syang opened this issue 9 years ago • 9 comments

Goal: To have more modular code base for my AWS SAM solution

Current Reality: Definitions belonging to template.yaml must be in a single file (e.g., template.yaml).

Problems:

  • If we have 50 different Lambda functions, then the template.yaml file is a huge file, so readability quickly decreases
  • If I want my team members to work on different categories of sub features, I can't have them work on different problems without interfering others.

Feature request (idea for discussion): It would be great if we can have the following directory:

samdir/template.yaml            /action/action_logic.yaml            /payment/payment_logic.yaml

--- inside template.yaml -----

 ...
 ...
 - include {./action/action_logic.yaml}
 - include {./payment/payment_logic.yaml}

Actually, the above mentioned capability (to achieve composibility and modularization) is needed for the swagger.yaml file as well for a large project, I think.

syang avatar May 19 '17 21:05 syang

The OpenAPI specification already supports this functionality, but I have not tried using it within the context of CloudFormation to see if I can make it work: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#referenceObject

It might be worth a shot just to see if it's possible.

lafiosca avatar May 22 '17 14:05 lafiosca

Did you checkout http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/create-reusable-transform-function-snippets-and-add-to-your-template-with-aws-include-transform.html? Seems like something you want.

sanathkr avatar Jun 28 '17 08:06 sanathkr

Did you have to do anything special to get the AWS::Include transform to work? I've just tried it like this:

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Beta
      DefinitionBody:
        'Fn::Transform':
          Name: 'AWS::Include'
          Parameters:
            Location: s3://some-bucket/swagger.yml

However, that fails with:

Failed to create the changeset:
  Waiter ChangeSetCreateComplete failed:
    Waiter encountered a terminal failure state Status: FAILED. 
Reason:
  Template Error: Encountered unsupported function: Fn::Transform
  Supported functions are: [Fn::Base64, Fn::GetAtt, Fn::GetAZs, Fn::ImportValue, Fn::Join, Fn::Split, Fn::FindInMap, Fn::Select, Ref, Fn::Equals, Fn::If, Fn::Not, Condition, Fn::And, Fn::Or, Fn::Contains, Fn::EachMemberEquals, Fn::EachMemberIn, Fn::ValueOf, Fn::ValueOfAll, Fn::RefAll, Fn::Sub]

wjbuys avatar Jul 10 '17 21:07 wjbuys

wjbuys@ If all you want to do is use a swagger file from s3, you can look at the following example:

https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_swagger_cors/template.yaml

Check the 'DefinitionUri' property.

vikrambhatt avatar Jul 10 '17 21:07 vikrambhatt

Nope, I specifically want to be able to use Fn::Sub with CloudFormation parameters to parametrize the uri field in my swagger definition, just like @dinvlad in #8:

x-amazon-apigateway-integration:
  passthroughBehavior: "when_no_match"
  httpMethod: "POST"
  type: "aws_proxy"
  uri:
    Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${FrontendHandler.Arn}/invocations"

wjbuys avatar Jul 10 '17 21:07 wjbuys

@wjbuys, it's strange that Fn::Transform doesn't work for you. I use the exact same lines as you suggested in https://github.com/awslabs/serverless-application-model/issues/137#issuecomment-314244693, and it works just fine. Perhaps it's the version of the cli? Mine is aws-cli/1.11.68 (haven't upgraded recently though). If you're running it through CodeBuild then I believe they may use an outdated version; try upgrading the version explicitly there with pip install -U awscli.

dinvlad avatar Jul 11 '17 05:07 dinvlad

@wjbuys I am trying to do the same thing, same files/lines but doing it in the console. I get Internal Failure. Rollback requested by user.

larkinrobert avatar Aug 29 '17 23:08 larkinrobert

We have an example to show how this works:

Swagger: https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_swagger_cors/swagger.yaml#L24

SAM with AWS::Include: https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_swagger_cors/template.yaml#L11

Closing this issue because the question has been addressed. Re-open if you have more questions

sanathkr avatar Jan 10 '18 20:01 sanathkr

@sanathkr Both those samples are 404 now and I'm struggling to use Fn::Transform with a local openapi.yaml file. My transformed template includes the contents, but none of the Fn::Sub or Fn::Ref get parsed, they're just passed through as properties with the unparsed string. Gist with before/after templates: https://gist.github.com/rainabba/bf62daddd028fcb98f9959d3ff90fdfd

rainabba avatar Mar 09 '21 21:03 rainabba