在 cloudformation cfn-init 中使用 linux 环境变量

Using linux environment variables in cloudformation cfn-init

在命令部分的 cloudformation 模板中,$SWARM_TOKEN 变量未被 aws cli 命令拾取为字符串值。这个想法是将令牌存储在参数存储中以备后用。

我在配置集中使用的代码:

AWS::CloudFormation::Init:....
      
get_and_store_swarm_token:
                  commands:
                    get_and_store_token:
                      command: !Sub 
                        SWARM_TOKEN=$(sudo docker swarm join-token -q worker)
                        aws ssm put-parameter --name "SwarmToken" --type "SecureString" --value $SWARM_TOKEN --overwrite --region ${AWS::Region}

当我通过 ssh 进入 EC2 linux 主机时,我可以执行命令并且它们可以工作。 所以我认为 cfn-init 命令以某种方式启动了一个会话,其中 $SWARM_TOKEN 变量不被视为字符串。关于如何使 cli 命令工作以便识别 $SWARM_TOKEN 变量的任何想法?

So i think that the cfn-init command somehow starts a session where the $SWARM_TOKEN variable is not seen as a string.

你是对的。对于普通用户,您必须使用 cfn-init~/.bashrc/etc/profile 更新为 export 您的 SWARM_TOKEN