AWS EC2 启动模板:使用 cloud-config 执行 bash 脚本

问题描述 投票:0回答:0

我的 EC2 实例配置了 AWS::EC2::LaunchTemplate,它在 launchtemplate 的 UserData 部分使用 cloud-config。

UserData:
          # Parts of cloud-config file are executed in the same order. Order if fixed by cloud-init.
          # Documentation : https://cloudinit.readthedocs.io/en/latest/index.html
          Fn::If:
            - DatadogAgentEnabled
            - Fn::Base64: !Sub |
                #cloud-config
                repo_update: true

                write_files:
                ......
                .......
                packages:
                  - amazon-efs-utils
                  - jq
                  - nfs-utils
              
                package_update: true
                package_upgrade: true
                 .......
                 .......
                runcmd:
                  - [ mkdir, /efs ]
                  - [ mount, /efs ]
                 .......
                 .......

但是现在我需要执行一个近百行代码的shell脚本。 如何在 cloud-config 下执行整个脚本?

请推荐

***更新: 我尝试将脚本放在用户数据之后作为测试。 cloudformation 更新成功,但 cloud-config 下面的命令在部署期间无法执行。我的检查方式是:在 runcmd 部分,我正在安装 codedeploy 代理。但是在下面的 CFN 模板更新之后,我尝试启动一个新实例,但部署失败了。 然后我删除了示例脚本 [mkdir /home/test] 并部署成功。

UserData:
          # Parts of cloud-config file are executed in the same order. Order if fixed by cloud-init.
          # Documentation : https://cloudinit.readthedocs.io/en/latest/index.html
          Fn::If:
            - DatadogAgentEnabled
            - Fn::Base64: !Sub |
                #!/bin/bash
                mkdir  /home/test
                #cloud-config
                repo_update: true
amazon-web-services amazon-ec2 aws-cloudformation
© www.soinside.com 2019 - 2024. All rights reserved.