首页 > 解决方案 > 使用 pip 在实例上安装 aws cli

问题描述

我在启动期间使用云形成模板在我的实例上安装 CLI,但是当我尝试运行任何具有 cli 命令的 shell 脚本时,它会显示“aws configure”错误。由于权限问题,我无法将任何角色或 IAM 用户附加到实例。

如果还有其他实现相同的方法,请提供帮助。

外壳脚本

#!/bin/bash
my_eth1=`aws ec2 describe-network-interfaces --filters "Name=status,Values=available" "Name=tag:Name,Values=MyENI" "Name=subnet-id,Values=subnet-03754b9bf4816284e" --output text --query "NetworkInterfaces[0].PrivateIpAddress"`
my_eth1_id=`aws ec2 describe-network-interfaces --filters "Name=status,Values=available" "Name=tag:Name,Values=MyENI" "Name=subnet-id,Values=subnet-03754b9bf4816284e" --output text --query "NetworkInterfaces[0].NetworkInterfaceId" | grep -o 'eni-[a-z0-9]*'`
ec2_id=`curl http://169.254.169.254/latest/meta-data/instance-id`

aws ec2 attach-network-interface --network-interface-id $my_eth1_id --instance-id $ec2_id --device-index 1
GATEWAY=`ip route | awk '/default/ { print $3 }'`

printf "\nGATEWAYDEV=eth0\n" >>/etc/sysconfig/network
printf "\nBOOTPROTO=dhcp\nDEVICE=eth1\nONBOOT=yes\nTYPE=Ethernet\nUSERCTL=no\n" >/etc/sysconfig/network-scripts/ifcfg-eth1
ifup eth1

ip route add default via $GATEWAY dev eth1 tab 2

ip rule add from $my_eth1/32 tab 2 priority 600 

云的形成

"UserData": { "Fn::Base64" : { "Fn::Join" : ["", [
                "#!/bin/bash -xe\n",
                "echo 'Downloading PIP'\n",
                "curl -O https://bootstrap.pypa.io/get-pip.py\n",
                "echo 'Executing PIP using Python'\n",
                "python get-pip.py --user\n",
                "echo 'Setting PIP Classpath'\n",
                "echo 'export PATH=~/.local/bin:$PATH' >> ~/.bash_profile\n",
                "echo 'Reloading bash_profile'\n",
                "source ~/.bash_profile\n",
                "echo 'Installing AWS CLI'\n",
                "pip install awscli --upgrade --user\n"
            ]]}}

标签: amazon-web-servicesamazon-cloudformation

解决方案


推荐阅读