2017-03-12 9 views
0

私はAWS request spot fleetを作成し、ユーザデータとしてjinjaテンプレートを指定してインスタンスに渡すためにしようとしていますし、私はこの文書を、次の午前: http://boto3.readthedocs.io/en/latest/reference/services/ec2.htmlAWS boto3 request_spot_fleetユーザデータとして神社テンプレートを渡す - ユーザーデータの不正BASE64エンコーディング

見て - request_spot_fleet(**kwargs)

'UserData': 'string',

UserData (string) -- The user data to make available to the instances. If you are using an AWS SDK or command line tool, Base64-encoding is performed for you, and you can load the text from a file. Otherwise, you must provide Base64-encoded text.

 template_file = (current_dir + '/config/user.jinja') 
    template = templateEnv.get_template(template_file) 
    template_vars = template_vars = { 'var1' : var1 } 
    output_template = template.render(template_vars) 
    self.output_template = base64.b64encode(output_template).decode("ascii") 

エラー:私は文字列にUserDataのを変更した場合

self.output_template = output_template

com.amazonaws.services.ec2.model.AmazonEC2Exception: 
Invalid BASE64 encoding of user data 
(Service: AmazonEC2; Status Code: 400; Error Code: InvalidParameterValue) 

すべてがうまく機能::

self.output_template = base64.b64encode(b'test').decode("ascii") 
'UserData': self.output_template, 

self.output_template = base64.b64encode(output_template).decode("ascii") 
    File "/usr/lib/python3.5/base64.py", line 59, in b64encode 
    encoded = binascii.b2a_base64(s)[:-1] 
    TypeError: a bytes-like object is required, not 'str' 

であるように私はjinjaテンプレートを渡す場合助言がありますか?

+0

AWS SDKを使用していて、AWS SDK(Boto3)を使用している場合は、「Base64エンコーディングはあなたのために実行されます」と明記されています。では、なぜbase64でエンコードされた文字列を渡しているのですか?文字列を渡すだけです。 – helloV

+0

私はこの 'self.output_template = output_template'のようにするとjinjaテンプレートを渡すことができませんが、aws console' com.amazonaws.services.ec2.model.AmazonEC2Exceptionでエラーが表示されます:無効なBASE64エンコーディング(サービス:AmazonEC2;ステータスコード:400;エラーコード:InvalidParameterValue) – Berlin

答えて

2

Python 3では、コードページのエンコードの問題を防ぐため、バイトと文字列オブジェクトを明示的に指定する必要があります。

# this line only works in python2 
self.output_template = base64.b64encode(output_template).decode("ascii") 

# You must convert str to bytes in Python3 
self.output_template = base64.b64encode(output_template.encode("ascii")).decode("ascii") 

注:質問するときは、必ずPythonバージョンを指定してください。

関連する問題