简体   繁体   English

如何使用 AWS CLI 在 EC2 中执行 linux 命令

[英]How can I execute linux command inside EC2 using AWS CLI

I have some EC2 instances running on AWS.我有一些在 AWS 上运行的 EC2 实例。 I am able to get the instance details and start and stop the instance using the AWS CLI command.我可以使用 AWS CLI 命令获取实例详细信息并启动和停止实例。 I was looking for a way to execute a few Linux commands inside the AWS EC2 instances using CLI.我正在寻找一种使用 CLI 在 AWS EC2 实例中执行一些 Linux 命令的方法。

Actually, I am creating a Jenkins pipeline which stops and starts the EC2 instance using AWS CLI.实际上,我正在创建一个 Jenkins 管道,它使用 AWS CLI 停止和启动 EC2 实例。 But I also need to execute a few commands after the EC2 is started.但是我还需要在 EC2 启动后执行一些命令。

I tried using the aws ssm send-command but it seems this command is not working for me.我尝试使用aws ssm send-command ,但似乎该命令对我不起作用。 I tried the below example command and got the error.我尝试了下面的示例命令并得到了错误。

aws ssm send-command \
    --instance-ids "instance-ID" \
    --document-name "AWS-RunShellScript" \
    --comment "IP config" \
    --parameters commands=ifconfig \
    --output text

Error:错误:

An error occurred (InvalidInstanceId) when calling the SendCommand operation: Instances [[i-xxxx]] not in a valid state for account XXXXXXXX

Is there any alternate approach to executing commands using AWS CLI?是否有任何替代方法可以使用 AWS CLI 执行命令?

Is your EC2 instance viewable in Systems Manager?您的 EC2 实例是否可以在 Systems Manager 中查看? You won't be able to use aws ssm send-command if your EC2 instance can't talk to Systems Manager.如果您的 EC2 实例无法与 Systems Manager 对话,您将无法使用aws ssm send-command

Make sure your EC2 instance has the following roles:确保您的 EC2 实例具有以下角色:

  • AmazonSSMManagedInstanceCore` AmazonSSMManagedInstanceCore`

Make sure you EC2 can access the following internet domains (or VPC endpoints):确保您的 EC2 可以访问以下 Internet 域(或 VPC 终端节点):

  • ssm.[region].amazonaws.com ssm.[地区].amazonaws.com
  • ec2messages.[region].amazonaws.com ec2messages.[区域].amazonaws.com
  • ssmmessages.[region].amazonaws.com ssmmessages.[地区].amazonaws.com

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

相关问题 通过 EC2 内的元数据授权 AWS CLI 的用户权限 - User permissions to authorize AWS CLI via metadata inside EC2 如何在Amazon EC2 linux上安装lxml依赖项? - How can I install lxml dependencies on Amazon EC2 linux? 如何从命令行在Linux(ec2 / ubuntu)上运行/执行adobe AIR文件(没有gui) - How to run/execute an adobe AIR file on Linux (ec2/ubuntu) from command line only (no gui) 如何修复 aws ec2“statuscheckfailed”问题? - how can i fix aws ec2 “statuscheckfailed” problem? 如何在不使用空间的情况下在 linux 终端中执行命令? - How i can execute command in linux terminal without using space? 如何使用 AWS CLI 获取具有特定标签的 Amazon EC2 自动扩展组中的实例? - How to get the instances in the Amazon EC2 autoscaling group with specific tags using AWS CLI? 如何使用 AWS Cli 中的标签从现有 ec2 创建 AMI? - How to create AMIfrom existing ec2 with tags from AWS Cli? SSH进入EC2并且无法执行git clone命令 - SSH into EC2 and can't execute git clone command 我们如何在AWS EC2 Linux实例的不同端口上安装多个apache服务器 - How can we install multiple apache servers at different ports in aws ec2 linux instance 如何在 AWS EC2 Linux 实例上运行编译和运行 c 文件? - How do I run compile and run a c file on an AWS EC2 Linux Instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM