简体   繁体   English

AWS cloudformation 嵌套堆栈因模板 URL 而失败

[英]AWS cloudformation nested stacks failed by template URL

I'm new with cloudformation but on currently project have a template that have all resources in a unique file and we try to separate in multiple files with nested stacks option.我是 cloudformation 的新手,但在当前项目中有一个模板,该模板在一个唯一文件中包含所有资源,我们尝试使用嵌套堆栈选项将多个文件分开。 When I try to deploy templates, execution failed by next message:当我尝试部署模板时,下一条消息执行失败:

$ aws s3 cp testing-substack.yml s3://gitlab-cicd

upload: ./testing-substack.yml to s3://gitlab-cicd/testing-substack.yml


$ aws cloudformation package --template-file testing-mainstack.yml --s3-bucket gitlab-cicd --output-template testing-packstack.yaml

Unable to upload artifact substack-amp.yml referenced by TemplateURL parameter of SubstackA resource.
TemplateURL parameter of SubstackA resource is invalid. It must be a S3 URL or path to CloudFormation template file. Actual: /builds/project-0/substack-amp.yml

Next include both template (lambda functions only have a "Hello world":接下来包括两个模板(lambda 函数只有一个“Hello world”:

testing-mainstack.yml测试-mainstack.yml

testing A测试A

Resources:
  SubstackA:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      TemplateURL: testing-substack.yml

testing B测试 B

Resources:
  SubstackA:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      TemplateURL: s3://gitlab-cicd/testing-substack.yml

substack template: testing-substack.yml子栈模板:testing-substack.yml

  TestingSubLambda:
    Type: AWS::Serverless::Function
    Properties:
      Description: "Testing lambda inside substack"
      CodeUri: ./
      Handler: lambda-two.lambda_handler
      Runtime: python3.8
      FunctionName: TestingSubLambda
      # Role: arn:aws:iam::000365055762:role/lambda-essential-role
      Timeout: 480
      # Events:
      #   B2bCImportOrdersApiEvent:
      #     Type: Api
      #     Properties:
      #       Path: /b2b-channels/import-orders
      #       Method: GET
      #       RestApiId: !Ref B2bCAPIDev

How can I identify which is the correct method to define the substack into main stack?如何确定将子堆栈定义为主堆栈的正确方法?

TemplateURL should be written as the URL in S3, therefore https://... . TemplateURL在 S3 中应写为 URL,因此https://...

In this doc , written as:这个文档中,写成:

TemplateURL: https://s3.amazonaws.com/cloudformation-templates-us-east-1/S3_Bucket.template

I Fix the problem with @Shimo response, additionally, use SAM client.我修复了@Shimo 响应的问题,另外,使用 SAM 客户端。

Substack A子堆栈 A

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Resources:
  TestingSubLambdaA:
    Type: AWS::Serverless::Function
    Properties:
      Description: "Testing lambda mnain substack"
      CodeUri: lambda-one/
      Handler: lambda-one.lambda_handler
      Runtime: python3.8
      FunctionName: TestingSubLambdaA
      Timeout: 480

Substack B子栈 B

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Resources:
  TestingSubLambdaB:
    Type: AWS::Serverless::Function
    Properties:
      Description: "Testing lambda secondar substack"
      CodeUri: lambda-two/
      Handler: lambda-two.lambda_handler
      Runtime: python3.8
      FunctionName: TestingSubLambdaB
      Timeout: 480

Main Stack主堆栈

Resources:
  SubstackA:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      TemplateURL: https://<S3-BUCKET>.s3.<AWS-REGION>.amazonaws.com/test-subkstack-a.yml
      TimeoutInMinutes: 5
      
  SubstackB:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      TemplateURL: https://<S3-BUCKET>.s3.<AWS-REGION>.amazonaws.com/test-subkstack-b.yml
      TimeoutInMinutes: 5

Finally, I package substacks template before send to S3 container.最后,在发送到 S3 容器之前,我将 package 子堆栈模板。

sam package -t testing-substack-a.yml --s3-bucket <S3-BUCKET> --output-template-file test-subkstack-a.yml --region ap-northeast-1

sam package -t testing-substack-b.yml --s3-bucket <S3-BUCKET> --output-template-file test-subkstack-b.yml --region <AWS-REGION>

aws s3 cp test-subkstack-a.yml s3://<S3-BUCKET>

aws s3 cp test-subkstack-b.yml s3://<S3-BUCKET>

sam package -t testing-mainstack.yml --s3-bucket <S3-BUCKET> --output-template-file testing-packstack.yml --region <AWS-REGION>

sam deploy --template-file testing-packstack.yml --stack-name TestingStackDeploy --region <AWS-REGION> --capabilities CAPABILITY_AUTO_EXPAND CAPABILITY_IAM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM