我无法使用boto3工作获取python脚本。我试图在windows,python 2.7和boto3中使用实例userdata脚本启动它。
我已经确认调用了脚本,最后我得到一个日志文件,表明执行的最后一行是:
s3 = boto3.resource('s3')
不幸的是,当我查看任务监视器,并查看powershell和python脚本进程时,它们处于空闲状态,并且脚本没有继续进行,并且没有发生明显错误。
有没有理由以这种方式使用boto3是一个坏主意?我无所畏惧地搞清楚这个问题,没有错误和空闲的过程。我已经确认通过RDP在同一个ec2实例上从命令提示符运行相同的命令确实按预期工作(意味着IAM角色没问题),所以这与使用userdata运行有什么关系?
我启用了完整的botocore调试,我的日志如下所示:
11-28 21:39 root INFO startup
11-28 21:39 root INFO creating boto3 s3 resource
11-28 21:39 botocore.loaders DEBUG Loading JSON file: C:\python27\lib\site-packages\boto3-1.4.1-py2.7.egg\boto3\data\s3\2006-03-01\resources-1.json
11-28 21:39 botocore.credentials DEBUG Skipping environment variable credential check because profile name was explicitly set.
11-28 21:39 botocore.credentials DEBUG Looking for credentials via: env
11-28 21:39 botocore.credentials DEBUG Looking for credentials via: assume-role
11-28 21:39 botocore.credentials DEBUG Looking for credentials via: shared-credentials-file
11-28 21:39 botocore.credentials DEBUG Looking for credentials via: config-file
11-28 21:39 botocore.credentials DEBUG Looking for credentials via: ec2-credentials-file
11-28 21:39 botocore.credentials DEBUG Looking for credentials via: boto-config
11-28 21:39 botocore.credentials DEBUG Looking for credentials via: container-role
11-28 21:39 botocore.credentials DEBUG Looking for credentials via: iam-role
11-28 21:39 botocore.vendored.requests.packages.urllib3.connectionpool INFO Starting new HTTP connection (1): 169.254.169.254
11-28 21:39 botocore.vendored.requests.packages.urllib3.connectionpool DEBUG "GET /latest/meta-data/iam/security-credentials/ HTTP/1.1" 200 21
11-28 21:39 botocore.vendored.requests.packages.urllib3.connectionpool INFO Starting new HTTP connection (1): 169.254.169.254
11-28 21:39 botocore.vendored.requests.packages.urllib3.connectionpool DEBUG "GET /latest/meta-data/iam/security-credentials/fullAccessToS3_catcfs HTTP/1.1" 200 882
11-28 21:39 botocore.credentials INFO Found credentials from IAM Role: fullAccessToS3_catcfs
11-28 21:39 botocore.loaders DEBUG Loading JSON file: C:\python27\lib\site-packages\botocore-1.4.70-py2.7.egg\botocore\data\endpoints.json
我通过userdata运行的代码(再次停止在s3 = boto3.resource(' s3')行)
def main():
"""to be run on by each instance as a startup command"""
import boto3, argparse, sys
from s3interface import S3Interface
from manifest import Manifest
from instancemanager import InstanceManager
from loghelper import LogHelper
parser = argparse.ArgumentParser(
description="AWS Instance bootstrapper" +
"Loads manifest which contains data and commands to run on this instance,"+
"downloads data from S3, runs commands, and uploads results to S3")
parser.add_argument("--bucketName", help = "the name of the S3 bucket to work with", required=True)
parser.add_argument("--manifestKey", help = "the key pointing to the manifest file in the s3 bucket", required=True)
parser.add_argument("--instanceId", help = "the id of this instance as defined in the manifest file", required=True)
parser.add_argument("--localWorkingDir", help = "a directory to store working files, it will be created if it does not exist on the instance", required=True)
try:
boto3.set_stream_logger(name='botocore')
args = vars(parser.parse_args())
bootstrapper = None
bucketName = args["bucketName"]
manifestKey = args["manifestKey"]
instanceId = int(args["instanceId"])
localWorkingDir = args["localWorkingDir"]
if not os.path.exists(localWorkingDir):
os.makedirs(localWorkingDir)
logPath = os.path.join(localWorkingDir, "log_instance{0}.txt".format(instanceId))
LogHelper.start_logging(logPath)
logging.info("startup")
logging.info("creating boto3 s3 resource")
s3 = boto3.resource('s3')
logging.info("creating S3Interface")
s3interface = S3Interface(s3, bucketName, localWorkingDir)
localManifestPath = os.path.join(localWorkingDir, "manifest.json")
logging.info("downloading manifest from S3")
s3interface.downloadFile(manifestKey, localManifestPath)
manifest = Manifest(localManifestPath)
instancemanager = InstanceManager(s3interface, manifest)
bootstrapper = AWSInstanceBootStrapper(instanceId,
manifest,
s3interface,
instancemanager,
logPath)
bootstrapper.DownloadS3Documents()
bootstrapper.RunCommands()
bootstrapper.UploadS3Documents()
except Exception as ex:
logging.exception("error in bootstrapper")
if bootstrapper is not None:
bootstrapper.UploadLog()
sys.exit(1)
if __name__ == "__main__":
main()
答案 0 :(得分:0)
答案肯定是拼写错误或不正确的区域。我将区域设置为us_east-1
而不是us-east-1
,并且得到了Starting new HTTP connection (1): 169.254.169.254
。谢谢https://stackoverflow.com/users/174777/john-rotenstein