Amazon web services 如何在部署了CloudFormation的自动化文档中的EC2实例中执行bash代码

Amazon web services 如何在部署了CloudFormation的自动化文档中的EC2实例中执行bash代码,amazon-web-services,amazon-ec2,amazon-cloudformation,aws-systems-manager,Amazon Web Services,Amazon Ec2,Amazon Cloudformation,Aws Systems Manager,我试图通过aws:runcommand执行bash代码。我已将下面的片段从 下面看到的是通过CloudFormation堆栈部署的。形成一个AWS::SSM::Document对象,传递各种输入。这是我的自动化文档的主要步骤之一。我正在尝试更新实例的操作系统 { "name": "updateOSSoftware", "action": "aws:runCommand", "maxAttempts": 3, "timeoutSeconds": 3600,

我试图通过aws:runcommand执行bash代码。我已将下面的片段从

下面看到的是通过CloudFormation堆栈部署的。形成一个AWS::SSM::Document对象,传递各种输入。这是我的自动化文档的主要步骤之一。我正在尝试更新实例的操作系统

{

   "name": "updateOSSoftware",

   "action": "aws:runCommand",

   "maxAttempts": 3,

   "timeoutSeconds": 3600,

   "onFailure": "Abort",

   "inputs": {

       "DocumentName": "AWS-RunShellScript",

       "InstanceIds": [

           "{{startInstances.InstanceIds}}"

       ],

       "Parameters": {

           "commands": [

               "export https_proxy=http://myproxy.com:myport",

                "export https_proxy='{{OutBoundProxy}}'",

                "set -e",

               "[ -x \"$(which wget)\" ] && get_contents='wget $1 -O -'",

               "[ -x \"$(which curl)\" ] && get_contents='curl -s -f $1'",

               "eval $get_contents https://aws-ssm-downloads-eu-west-1.s3.amazonaws.com/scripts/aws-update-linux-instance > /tmp/aws-update-linux-instance",

               "chmod +x /tmp/aws-update-linux-instance",

               "/tmp/aws-update-linux-instance --pre-update-script '{{PreUpdateScript}}' --post-update-script '{{PostUpdateScript}}' --include-packages '{{IncludePackages}}' --exclude-packages '{{ExcludePackages}}' 2>&1 | tee /tmp/aws-update-linux-instance.log"

           ]

       }

   }

}
一旦我从Systems Manager执行了该文档,我就SSH到EC2实例并尝试和
echo$http_proxy
,该变量就被取消设置,显示代码没有启动


如何运行bash代码?

使用“export”命令导出的环境变量仅适用于当前shell。 如果您通过ssh登录到主机,它们将位于不同的shell中,我认为您无法看到通过“RunCommand”设置的env变量

解决方法是在bash概要文件中添加命令。e、 g


echo“导出https\u代理=http://myproxy.com:myport“>>~/.bash\u profile

您是否考虑过创建一个AMI并改用它?一旦更新了操作系统,我想停止实例,然后使用操作创建一个AMI:
aws:createimage