Non interactive shell of an EC2 using aws golang sdk v2

Viewed 198

With bash I type this command:

ssh -i key.pem ubuntu@ec2-instance ls

And the result will be something like:

file1
file2
file3

Question:

Can I do the same thing with AWS go SDK v2?

What I need:

  • Use AWS systems manager
  • Do not use ssh.
  • Get the output directly in my go code to print it to the console
  • Don't need an interactive shell
1 Answers

The API allows this. I'm not familar with the go SDK though.

here's a link to the JavaScript SDK (I assume functionality is the same) : https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SSM.html#sendCommand-property

It can be done by the command line as well:

aws ssm send-command --instance-ids "i-INSTANCEID" --document-name "AWS-RunShellScript" --comment "List" --parameters commands=ls --region "ap-northeast-1"

you get the result with list-command-invocations

aws ssm list-command-invocations --command-id "4f65c2da-NNNN-JJJJ-LLLL-6efc67e6cd5d" --details --region "ap-northeast-1"

The user/role used needs access to ssm:SendCommand and ssm:ListCommandInvocations - e.g.

   {
        "Effect": "Allow",
        "Action": [
            "ssm:SendCommand",
            "ssm:ListCommandInvocations"
        ],
        "Resource": "*"
    }
Related