1

I'm trying to execute bash code via aws:runcommand. I have taken and adapted the below snippet from the AWS Repo to deploy a Golden Image pipeline

What you see below is deployed via a CloudFormation Stack. An AWS::SSM::Document object is formed, passing various inputs. This is one of the mainSteps of my automation document. I'm trying to update the OS of my instance.

{

   "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"

           ]

       }

   }

}

Once I execute the document from the Systems Manger, I SSH into the EC2 instance and trying and echo $http_proxy, the variable is unset, showing the code wasn't launched.

How can I run bash code?

2
  • have you considered creating an AMI and using that instead? Commented Mar 5, 2019 at 19:14
  • once updated the OS I want to stop the instance then create an AMI using action: aws:createimage Commented Mar 5, 2019 at 19:19

1 Answer 1

2

The environment variable exported using 'export' command is for the current shell only. If you login to the hosts via ssh, they will be in a different shell, I don't think you could see the env variable set via 'RunCommand'.

A workaround could be add the command in bash profile. e.g.

echo "export https_proxy=http://myproxy.com:myport" >> ~/.bash_profile

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.