简体   繁体   English

区分 Shell 变量和 Cloudformation 模板参数

[英]Differentiate between Shell Variables and Cloudformation template parameters

I have come across a usecase where i am seeing conflict between Shell Variables and Cloudformation Parameters .我遇到了一个用例,我看到Shell VariablesCloudformation Parameters之间存在冲突。

Example:例子:

I am writing AWS::ImageBuilder::Component我正在写AWS::ImageBuilder::Component

Parameters:
  cloudVersion:
    Type: String
    Default: "1.10.11"
    Description: Cloud Version to release
Resources:
  CloudInstallComponent:
    Type: AWS::ImageBuilder::Component
    Properties:
      Name: CloudAMI
      Version: !Ref cloudVersion
      Description: Install the latest cloud.
      ChangeDescription: Cloud First version
      Platform: Linux
      Data: !Sub |
        name: DeployCloudComponents
        description: This is to deploy cloud components.
        schemaVersion: 1.0
        phases:
          - name: build
            steps:
              - name: CreateUser
                action: ExecuteBash
                inputs:
                  commands:
                    - "Creating New User cloud"
                    - export GroupID=7560
                    - export UserID=7560
                    - export USER=cloud
                    - sudo groupadd --gid ${GroupID} ${USER}
                    - sudo useradd --uid ${UserID} --gid ${GroupID} --create-home --shell /bin/bash ${USER}
                    - sudo usermod -G wheel ${USER}
                    - |-
                        echo "${USER}    ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
                    - sudo wget -O a.tgz 'abc.net/mg/api/v1/help/${cloudVersion}/on/download/a.tgz'

Now in above code snippet GroupID , UserID and USER are shell variables while cloudVersion is cloudformation parameter.现在在上面的代码片段中GroupIDUserIDUSER是 shell 变量,而cloudVersion是 cloudformation 参数。

Question is how to differentiate between them?问题是如何区分它们?

I am seeing errors in cfn-linter like below我在 cfn-linter 中看到如下错误

Error   Cfn-Lint    Parameter GroupID for Fn::Sub not found at Resources/CloudInstallComponent/Properties/Data/Fn::Sub  160:7
Error   Cfn-Lint    Parameter USER for Fn::Sub not found at Resources/K2CloudInstallComponent/Properties/Data/Fn::Sub   160:7
Error   Cfn-Lint    Parameter UserID for Fn::Sub not found at Resources/K2CloudInstallComponent/Properties/Data/Fn::Sub 160:7

I can solve the ambiguity by taking three shell variables as cloudformation parameters.我可以通过将三个 shell 变量作为 cloudformation 参数来解决歧义。

But if anyone can suggest an existing way to differentiate between both kind of variables would be good.但是,如果有人可以提出一种现有的方法来区分这两种变量,那就太好了。

Thanks in advance.提前致谢。

You have to escape your variables using ${!} notation:您必须使用${!}符号转义变量:

Parameters:
  cloudVersion:
    Type: String
    Default: "1.10.11"
    Description: Cloud Version to release
Resources:
  CloudInstallComponent:
    Type: AWS::ImageBuilder::Component
    Properties:
      Name: CloudAMI
      Version: !Ref cloudVersion
      Description: Install the latest cloud.
      ChangeDescription: Cloud First version
      Platform: Linux
      Data: !Sub |
        name: DeployCloudComponents
        description: This is to deploy cloud components.
        schemaVersion: 1.0
        phases:
          - name: build
            steps:
              - name: CreateUser
                action: ExecuteBash
                inputs:
                  commands:
                    - "Creating New User cloud"
                    - export GroupID=7560
                    - export UserID=7560
                    - export USER=cloud
                    - sudo groupadd --gid ${!GroupID} ${!USER}
                    - sudo useradd --uid ${!UserID} --gid ${!GroupID} --create-home --shell /bin/bash ${!USER}
                    - sudo usermod -G wheel ${!USER}
                    - |-
                        echo "${!USER}    ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
                    - sudo wget -O a.tgz 'abc.net/mg/api/v1/help/${cloudVersion}/on/download/a.tgz'

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

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