简体   繁体   English

使用 aws-cli 在 ec2 上远程运行命令

[英]Run a command remotely on ec2 using aws-cli

I'd like to create a bash script using aws cli that我想使用 aws cli 创建一个 bash 脚本

  • start an ec2 image启动一个 ec2 映像
  • clone a github repo克隆一个 github 存储库
  • build a docker image构建 docker 映像
  • stops the ec2 image.停止 ec2 图像。

I managed to make the start and stop using aws ec2 start-instances and aws ec2 stop-instances But I am struggling to run the other bash commands.我设法使用 aws ec2 start-instances 和 aws ec2 stop-instances 启动和停止但我正在努力运行其他 bash 命令。 I saw this: Run a command remotely on ec2 That explains how to run a bash command.我看到了这个: 在 ec2 上远程运行命令这解释了如何运行 bash 命令。

So I tried:所以我尝试了:

aws ssm send-command \
--instance-ids "i-02ae********" \
--region "us-east-2"\
--document-name "AWS-RunShellScript" \
--parameters commands="git clone https://myrepo.git" \
--output text

But I get as output但我得到 output

COMMAND 77821510-9094-4d3d-b7c0-99a9d0c46716            0       0       AWS-RunShellScript      $DEFAULT        0       2022-08-07T01:04:19.705000+02:00        50      0                       us-east-2       2022-08-06T23:04:19.705000+02:00                Pending Pending 1       3600
CLOUDWATCHOUTPUTCONFIG          False
INSTANCEIDS     i-02aef7e********
NOTIFICATIONCONFIG              
COMMANDS        git clone https://github.com/myrepo.git

And if I ssh into the ec2 instance, I don't see any effect of the command.如果我 ssh 进入 ec2 实例,我看不到该命令的任何效果。 Can someone give me some hint on how to proceed?有人可以给我一些关于如何进行的提示吗?

For the start and stop portion...对于开始和停止部分...

If you want to run a script every time that an Amazon EC2 instance starts, simply place the script in:如果您想在每次Amazon EC2 实例启动时运行脚本,只需将脚本放入:

/var/lib/cloud/scripts/per-boot/

Any script in that directory will run every time that the instance boots.每次实例启动时,该目录中的任何脚本都会运行。

When the script has finished processing, run:脚本完成处理后,运行:

sudo shutdown now -h

This will shutdown the instance from within the instance, rather than having to call an AWS API.这将从实例内部关闭实例,而不必调用 AWS API。

For more details, see: Auto-Stop EC2 instances when they finish a task - DEV Community有关更多详细信息,请参阅: EC2 实例完成任务时自动停止 - DEV 社区

I suggest you to do it this way: https://aws.amazon.com/blogs/mt/creating-packer-images-using-system-manager-automation/我建议你这样做: https://aws.amazon.com/blogs/mt/creating-packer-images-using-system-manager-automation/

I assume that you need to create an ec2 image.我假设您需要创建一个 ec2 映像。 This is a batter way than scriptingit with bash and cli.这比使用 bash 和 cli 编写脚本更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM