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

Nested stack issue

Open jamakase opened this issue 9 years ago • 60 comments

I am trying to make stack with nested stacks, but when i have Transform in nested stack, i get an error: CreateStack cannot be used with templates containing Transforms. If this error is expected, this should be added somewhere to docs, that only parent stack should have Transforms.

In case i delete Transforms from nested stacks, i get Circular dependency between resources: [ApiGatewayApi, InviteUserFunction, LambdaInvokePermission], which i do not have if i create this stack separately.

Here is part of nested stack template

AWSTemplateFormatVersion: 2010-09-09
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      DefinitionUri: ./../target/swagger.yaml
      StageName: Prod
      Variables:
        InviteUserFunctionName: !Ref InviteUserFunction
  InviteUserFunction:
    Type: AWS::Serverless::Function
    Properties:
      Timeout: 30
      Role: !GetAtt LambdaFunctionIAMRole.Arn
      Handler: com.MyHandler
      Runtime: java8
      CodeUri: ./../target/some.jar
      Events:
        GetRequest:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /invite
            Method: POST
  LambdaInvokePermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        !GetAtt InviteUserFunction.Arn
      Principal: apigateway.amazonaws.com
      SourceArn:
        Fn::Join:
        - ''
        - - 'arn:aws:execute-api:'
          - Ref: AWS::Region
          - ":"
          - Ref: AWS::AccountId
          - ":"
          - Ref: ApiGatewayApi
          - "/*"

jamakase avatar Feb 11 '17 12:02 jamakase

You should use create-change-set and execute-change-set APIs to create a stack using with Transforms. create-stack API won't work

sanathkr avatar Feb 13 '17 19:02 sanathkr

@sanathkr how would you do that with a nested stack? There's no ChangeSet resource.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html

ataeoalii avatar Mar 30 '17 23:03 ataeoalii

+1

schmitzcc avatar Apr 05 '17 12:04 schmitzcc

+1

windlessuser avatar Apr 19 '17 19:04 windlessuser

Came here to mention something specifically about the Change Set requirement and Nested stacks. Currently Change Sets do not traverse Nested stacks, so I'm going to operate under the assumption that you can not nest with SAM which is a major pitfall.

dejonghe avatar Apr 21 '17 20:04 dejonghe

Only the reverse seems to work, i.e. you can have a SAM enabled template as the parent template just not the child, which is quite a bummer and limiting.

alanwill avatar Apr 24 '17 05:04 alanwill

This really should be tagged as a bug not a question @sanathkr

alanwill avatar Jun 01 '17 19:06 alanwill

How about including Transform: 'AWS::Serverless-2016-10-31' to the parent template even if it doesn't use SAM resources. AFAIK, the nested templates should get transformed as well when they are processed.

If not, this is a bug that needs to be fixed.

sanathkr avatar Jun 28 '17 06:06 sanathkr

@sanathkr tried that before and just did it again to be sure, same error CreateStack cannot be used with templates containing Transforms. Thanks for marking this as a bug, hope it can get addressed soon.

alanwill avatar Jun 29 '17 01:06 alanwill

Same here CreateStack cannot be used with templates containing Transforms. I hit the 200 resource limit in the SAM template, so it has to be split into nested stacks. Otherwise the development is stalled at the moment. Besides, that would be better for organizing these many resources.

EDIT: well, there are other resources that we can nest as @alanwill mentioned, which alleviates the problem in the short term but that's not the point. Organizing stacks by functionality would be good practice, but right now we're forced to put all of the functions into the same SAM template, and split off their supporting resources into nested stacks, even though logically we'd group functions by their resource domain, so to speak.

EDIT2: as mentioned by @dejonghe, this comes down to a bigger question of adding CloudFormation support to create change sets for nested stacks (which would be good by itself). Without that, I don't see how the current SAM implementation could solve this issue (i.e transforms require change sets).

EDIT3: the 200 limit above was actually reached after template "expansion" by transform. I have just 100 resources in total, including 54 SAM functions. Each function currently gets expanded into a "regular" CloudFormation function plus a function role resource, and if we bind them to an API (which is a crucial piece of the whole model), each trigger also gets 2 AWS::Lambda::Permission resources, one for the Test stage (which is created automatically by SAM for some reason), and one for the stage that we specify. So each function gets expanded into ~3 resources on average. That explains why I hit the limit: 54 functions x 3 + 46 other resources ~ 200.

So this places an upper limit on the total number of functions in SAM of ~ 70, if there are no other resources. A reasonable API can have more than that (and there are plenty of functions that can comprise the backend without directly serving the API).

One workaround for us besides making space for ~15 more functions (which we will hit pretty soon), is to use regular functions, which sort of defeats the purpose of SAM. Another alternative is creating multiple independent SAM stacks, and deploying them through changesets in a script, but that may be more cumbersome than simply using nested stacks.

Could we at least request that the 200 resource limit is increased globally by the CloudFormation team? I'm not sure what the proper channel for that is, as the official Support request doesn't allow to specify this limit (only the one for the total number of stacks). This would at least allow most teams to continue their work without interruption, until this is resolved (which may take a while, if I understood correctly).

(Sorry if that's beyond the scope of this issue)

dinvlad avatar Jul 13 '17 01:07 dinvlad

Any thoughts on this @sanathkr I now have a workaround to a workaround just to try to get around this. The workaround was to put all the SAM resources in the parent stack, but then that go too large in a single stack so I have another workaround to that. Any guidance you can provide here would be super helpful.

alanwill avatar Jul 25 '17 03:07 alanwill

What I've done is create a CFN Template launcher in python with Boto that goes through all the steps. I have a Yaml file that defines stacks and can grab output from one stack and pass it as a parameter to another.

dejonghe avatar Jul 25 '17 13:07 dejonghe

@sanathkr Any news regarding this issue? It'd be so great to have that fixed.

Dunedan avatar Aug 09 '17 08:08 Dunedan

Having the same issue, any update @sanathkr?

rasika-jay avatar Aug 31 '17 08:08 rasika-jay

We moved away from SAM due to this problem. At the cost of more verbose templates and an extra packaging step, we also got more flexibility with 'native' resources.

dinvlad avatar Aug 31 '17 21:08 dinvlad

Even further than that, this issue was a factor in my team dropping CloudFormation in favour of Terraform.

At least with Terraform, we can fork and fix issues ourselves, rather than just waiting.

Widdershin avatar Aug 31 '17 21:08 Widdershin

@dinvlad could you share what your template looks like?

aehlke avatar Sep 15 '17 16:09 aehlke

@aehlke serverless functions can be replaced with 3 resources: AWS::Lambda::Function, AWS::IAM::Role and AWS::Lambda::Permission, then you just customize them as needed:

  Function:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.get
      Environment:
        Variables:
          VAR_NAME: !Ref ...
      Runtime: nodejs6.10
      Code:
        S3Bucket: !Ref LambdaBucket
        S3Key: !Ref LambdaPackageKey
      Role: !GetAtt FunctionRole.Arn

  FunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
        - PolicyName: FunctionRole
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - service:Action
                Resource:
                  - !GetAtt Resource.Arn

  FunctionRolePermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref Function
      Action: lambda:InvokeFunction
      Principal: apigateway.amazonaws.com
      SourceArn:
        !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiId}/${ApiStage}/GET/...

Before execution of the stack, you also need to upload lambda package as a zip file to s3://${LambdaBucket}/${LambdaPackageKey}.

dinvlad avatar Sep 16 '17 03:09 dinvlad

Turns out this is a CloudFormation feature that doesn't exist today. We are working internally with the team to this feature implemented.

Keeping the issue open for tracking

sanathkr avatar Jan 10 '18 20:01 sanathkr

Any news, on this one?

lfdesousa avatar Jan 26 '18 14:01 lfdesousa