在StackName之前添加Cloudformation资源

时间:2019-04-15 15:28:26

标签: amazon-web-services amazon-cloudformation naming

我希望基于一个CloudFormation模板具有多个堆栈,但是我遇到命名冲突。解决此问题的最简单方法似乎是将StackName附加(或附加)到每个重复的资源,例如我的lambda函数或角色。

AWS在'Template Reference' section of the documentation中讨论了AWS::StackName,但是没有明确的演示方法。

如何将StackName放在CloudFormation资源的前面?

MyLambdaFunction
  Type: "AWS:Serverless::Function"
  Properties:
    FunctionName: AWS::StackName + "-myLambdaFunction"

3 个答案:

答案 0 :(得分:5)

我更喜欢使用Fn::Sub,因为我认为它比替代方法更容易阅读:

import React, { Component } from "react";

import Dropzone from "react-dropzone";

const MaxSize = 1000000000; //
class DragAndDrop extends Component {
  handleOnDrop = (files, rejectedFiles) => {
    console.log(files);
    console.log("rejected files are:", rejectedFiles);
    if (rejectedFiles && rejectedFiles.length > 0) {
      const currentRejectFile = rejectedFiles[0];
      const currentRejectFileType = currentRejectFile.type;
      const currentRejectFileSize = currentRejectFile.size;
      if (currentRejectFileSize > MaxSize) {
        alert(
          "This file is not allowed. " +
            currentRejectFileSize +
            currentRejectFileType +
            " too large"
        );
      }
    }

    if (files && files.length > 0) {
      const currentFile = files[0];
      const currentFileType = currentFile.type;
      const currentFileSize = currentFile.size;
      if (currentFileSize > MaxSize) {
        alert(
          "This file is not allowed. " +
            currentFileSize +
            currentFileType +
            " too large"
        );
      }
    }
  };

  render() {
    return (
      <div>
        <h1>Drop </h1>
        <Dropzone
          onDrop={() => this.handleOnDrop()}
          multiple={false}
          maxSize={MaxSize}
        >
          Drop image here or click to upload
        </Dropzone>
      </div>
    );
  }
}

export default DragAndDrop;

答案 1 :(得分:2)

您需要Ref伪参数并使用Fn::Join方法来构造名称

MyLambdaFunction
  Type: "AWS:Serverless::Function"
  Properties:
    FunctionName: !Join [ "", [ {"Ref": "AWS::StackName"}, "-myLambdaFunction" ]]

答案 2 :(得分:0)

YAML 版本:

RoleName: !Sub ${AWS::StackName}-InstanceRole