1

I'm creating template that defines our prod environment. In our system we will use many Lambda and it produce huge Cloudformation template file with many (even hounded) entries like this presented below. Is possible to split one template file to several separate files (e.g one file for one function or at least one for one service). I know that there is substack mechanism but in substack I cannot store functions definitions in local files (I can only give template URL) and I'm not sure if I can pass argument to substack. As you from example below there are many parameters and references to other resources.

  APILambadFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ../lambda_functions/
      Handler: getUserInfo.lambda_handler
      FunctionName: !Sub ${CreatorUsername}-getUserInfo
      Runtime: python3.7
      VpcConfig:
        SecurityGroupIds:
          - !Ref SecurityGroupLamda
        SubnetIds:
          - !Ref PrivateSubnet1
          - !Ref PrivateSubnet2
      Role:
        Fn::GetAtt: [ RoleLamdaRestAPI, Arn ]   # Rola dla wszystkich Lamd restowych
      Environment:
        Variables:
          DB_HOST: !GetAtt 'PostgresDB.Endpoint.Address'
          DB_PORT: !GetAtt 'PostgresDB.Endpoint.Port'
          DB_NAME: !Sub '{{resolve:ssm:/${CreatorUsername}/${EnvType}/PostgresSQL/DBName:1}}'
          DB_USERNAME: !Sub '{{resolve:ssm:/${CreatorUsername}/${EnvType}/PostgresSQL/Username:1}}'
          CREATOR_USERNAME: !Ref CreatorUsername
          ENV_TYPE: !Ref EnvType
      Events:
        GetUserInfo:
          Type: Api
          Properties:
            Path: /user
            Method: get
            RestApiId: !Ref ApiGatewayApi
  • Unlike other languages, I'd actually recommend sticking to one large file to take best advantage of local tooling like the [Linter](https://github.com/aws-cloudformation/cfn-python-lint/) and [IDE integration](https://github.com/aws-cloudformation/aws-cfn-lint-visual-studio-code) and avoid the limitations of nested stacks like lacking [change sets](https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/142) and [drift detection](https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/143) – Pat Myron Sep 02 '20 at 16:25
  • But as I read there is a limit for number of resources around 200 , am I right? – Michal Szymanski Sep 02 '20 at 16:37
  • [There is](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html). Unfortunately not much choice now if you're hitting that limit, but [that limit is being re-evaluated](https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/360) – Pat Myron Sep 02 '20 at 17:05
  • Does this answer your question? [how to include multiple resource files in SAM template.yml](https://stackoverflow.com/questions/60551046/how-to-include-multiple-resource-files-in-sam-template-yml) – petey Sep 08 '20 at 16:24

1 Answers1

6

You could use the 'Nested Stack' functionality of CloudFormation.

In your 'main' template.yaml you define resources as follows:

Resources:
  NestedStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: ./any/directory/infrastructure.yml
      Parameters:
        Environment: Staging

You must run aws cloudformation package (docs) on this template to be able to use a local path.

Here you can learn more about nested stacks.

Josh
  • 901
  • 8
  • 29
Robert Kossendey
  • 6,733
  • 2
  • 12
  • 42
  • The documentation states "The URL must point to a template (max size: 460,800 bytes) that's located in an Amazon S3 bucket". So this will not work with local files unfortunately. – Josh Nov 01 '21 at 15:25
  • It works, you just have to use the package command. https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html – Robert Kossendey Nov 01 '21 at 15:33