本文整理汇总了Python中zappa.zappa.Zappa.create_lambda_zip方法的典型用法代码示例。如果您正苦于以下问题:Python Zappa.create_lambda_zip方法的具体用法?Python Zappa.create_lambda_zip怎么用?Python Zappa.create_lambda_zip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zappa.zappa.Zappa
的用法示例。
在下文中一共展示了Zappa.create_lambda_zip方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_lambda_package
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
def test_create_lambda_package(self):
# mock the pip.get_installed_distributions() to include a package in lambda_packages so that the code
# for zipping pre-compiled packages gets called
mock_named_tuple = collections.namedtuple('mock_named_tuple', ['project_name'])
mock_return_val = [mock_named_tuple(lambda_packages.keys()[0])] # choose name of 1st package in lambda_packages
with mock.patch('pip.get_installed_distributions', return_value=mock_return_val):
z = Zappa()
path = z.create_lambda_zip(handler_file=os.path.realpath(__file__))
self.assertTrue(os.path.isfile(path))
os.remove(path)
示例2: test_upload_remove_s3
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
def test_upload_remove_s3(self, session):
bucket_name = 'test_zappa_upload_s3'
z = Zappa(session)
zip_path = z.create_lambda_zip()
res = z.upload_to_s3(zip_path, bucket_name)
os.remove(zip_path)
self.assertTrue(res)
s3 = session.resource('s3')
# will throw ClientError with 404 if bucket doesn't exist
s3.meta.client.head_bucket(Bucket=bucket_name)
# will throw ClientError with 404 if object doesn't exist
s3.meta.client.head_object(
Bucket=bucket_name,
Key=zip_path,
)
res = z.remove_from_s3(zip_path, bucket_name)
self.assertTrue(res)
示例3: handle
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
def handle(self, *args, **options):
"""
Execute the command.
"""
if not options.has_key('environment'):
print("You must call deploy with an environment name. \n python manage.py deploy <environment>")
return
from django.conf import settings
if not 'ZAPPA_SETTINGS' in dir(settings):
print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
return
zappa_settings = settings.ZAPPA_SETTINGS
# Set your configuration
project_name = settings.BASE_DIR.split(os.sep)[-1]
api_stage = options['environment'][0]
if api_stage not in zappa_settings.keys():
print("Please make sure that the environment '" + api_stage + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
return
# Make your Zappa object
zappa = Zappa()
# Load environment-specific settings
s3_bucket_name = zappa_settings[api_stage]['s3_bucket']
vpc_config = zappa_settings[api_stage].get('vpc_config', {})
memory_size = zappa_settings[api_stage].get('memory_size', 512)
settings_file = zappa_settings[api_stage]['settings_file']
if '~' in settings_file:
settings_file = settings_file.replace('~', os.path.expanduser('~'))
if not os.path.isfile(settings_file):
print("Please make sure your settings_file is properly defined.")
return
custom_settings = [
'http_methods',
'parameter_depth',
'integration_response_codes',
'method_response_codes',
'role_name',
'aws_region'
]
for setting in custom_settings:
if zappa_settings[api_stage].has_key(setting):
setattr(zappa, setting, zappa_settings[api_stage][setting])
# Load your AWS credentials from ~/.aws/credentials
zappa.load_credentials()
# Make sure the necessary IAM execution roles are available
zappa.create_iam_roles()
# Create the Lambda zip package (includes project and virtualenvironment)
# Also define the path the handler file so it can be copied to the zip root for Lambda.
current_file = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
handler_file = os.sep.join(current_file.split(os.sep)[0:-2]) + os.sep + 'handler.py'
lambda_name = project_name + '-' + api_stage
zip_path = zappa.create_lambda_zip(lambda_name, handler_file=handler_file)
# Add this environment's Django settings to that zipfile
with open(settings_file, 'r') as f:
contents = f.read()
all_contents = contents
if not zappa_settings[api_stage].has_key('domain'):
script_name = api_stage
else:
script_name = ''
if not "ZappaMiddleware" in all_contents:
print("\n\nWARNING!\n")
print("You do not have ZappaMiddleware in your remote settings's MIDDLEWARE_CLASSES.\n")
print("This means that some aspects of your application may not work!\n\n")
all_contents = all_contents + '\n# Automatically added by Zappa:\nSCRIPT_NAME=\'/' + script_name + '\'\n'
f.close()
with open('zappa_settings.py', 'w') as f:
f.write(all_contents)
with zipfile.ZipFile(zip_path, 'a') as lambda_zip:
lambda_zip.write('zappa_settings.py', 'zappa_settings.py')
lambda_zip.close()
os.unlink('zappa_settings.py')
# Upload it to S3
zip_arn = zappa.upload_to_s3(zip_path, s3_bucket_name)
# Register the Lambda function with that zip as the source
# You'll also need to define the path to your lambda_handler code.
lambda_arn = zappa.create_lambda_function(bucket=s3_bucket_name,
s3_key=zip_path,
function_name=lambda_name,
handler='handler.lambda_handler',
vpc_config=vpc_config,
memory_size=memory_size)
#.........这里部分代码省略.........
示例4: handle
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
def handle(self, *args, **options):
"""
Execute the command.
"""
if not options.has_key("environment"):
print("You must call deploy with an environment name. \n python manage.py deploy <environment>")
return
from django.conf import settings
if not "ZAPPA_SETTINGS" in dir(settings):
print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
return
zappa_settings = settings.ZAPPA_SETTINGS
# Set your configuration
project_name = settings.BASE_DIR.split(os.sep)[-1]
api_stage = options["environment"][0]
if api_stage not in zappa_settings.keys():
print(
"Please make sure that the environment '"
+ api_stage
+ "' is defined in your ZAPPA_SETTINGS in your settings file before deploying."
)
return
# Make your Zappa object
zappa = Zappa()
# Load environment-specific settings
s3_bucket_name = zappa_settings[api_stage]["s3_bucket"]
settings_file = zappa_settings[api_stage]["settings_file"]
if "~" in settings_file:
settings_file = settings_file.replace("~", os.path.expanduser("~"))
if not os.path.isfile(settings_file):
print("Please make sure your settings_file is properly defined.")
return
custom_settings = [
"http_methods",
"parameter_depth",
"integration_response_codes",
"method_response_codes",
"role_name",
"aws_region",
]
for setting in custom_settings:
if zappa_settings[api_stage].has_key(setting):
setattr(zappa, setting, zappa_settings[api_stage][setting])
# Load your AWS credentials from ~/.aws/credentials
zappa.load_credentials()
# Create the Lambda zip package (includes project and virtualenvironment)
# Also define the path the handler file so it can be copied to the zip root for Lambda.
current_file = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
handler_file = os.sep.join(current_file.split(os.sep)[0:-2]) + os.sep + "handler.py"
lambda_name = project_name + "-" + api_stage
zip_path = zappa.create_lambda_zip(lambda_name, handler_file=handler_file)
# Add this environment's Django settings to that zipfile
with open(settings_file, "r") as f:
contents = f.read()
all_contents = contents
if not zappa_settings[api_stage].has_key("domain"):
script_name = api_stage
else:
script_name = ""
if not "ZappaMiddleware" in all_contents:
print("\n\nWARNING!\n")
print("You do not have ZappaMiddleware in your remote settings's MIDDLEWARE_CLASSES.\n")
print("This means that some aspects of your application may not work!\n\n")
all_contents = all_contents + "\n# Automatically added by Zappa:\nSCRIPT_NAME='/" + script_name + "'\n"
f.close()
with open("zappa_settings.py", "w") as f:
f.write(all_contents)
with zipfile.ZipFile(zip_path, "a") as lambda_zip:
lambda_zip.write("zappa_settings.py", "zappa_settings.py")
lambda_zip.close()
os.unlink("zappa_settings.py")
# Upload it to S3
zip_arn = zappa.upload_to_s3(zip_path, s3_bucket_name)
# Register the Lambda function with that zip as the source
# You'll also need to define the path to your lambda_handler code.
lambda_arn = zappa.update_lambda_function(s3_bucket_name, zip_path, lambda_name)
# Finally, delete the local copy our zip package
if zappa_settings[api_stage].get("delete_zip", True):
os.remove(zip_path)
# Remove the uploaded zip from S3, because it is now registered..
#.........这里部分代码省略.........
示例5: ZappaCommand
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
#.........这里部分代码省略.........
:param s3_key: Name of the file hosted on S3 (string)
:param output_filename: Name of the file the download operation
will create (string)
:return: False or the value of output_filename
"""
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
try:
s3.meta.client.head_object(Bucket=bucket_name, Key=s3_key)
except botocore.exceptions.ClientError:
return False
print(u'Downloading the settings file ({0}) from S3'.format(s3_key))
new_file = bucket.download_file(s3_key, output_filename)
return output_filename
def parse_s3_url(self, s3_url):
"""
Parse the S3 url. Format: s3://mybucket:path/to/my/key
Example: s3://settings-bucket:/production_settings.py
:param s3_url: Path to the file hosted on S3
:return:
"""
return s3_url.replace('s3://', '').split(':')
def load_credentials(self):
session = None
profile_name = self.zappa_settings[self.api_stage].get('profile_name')
region_name = self.zappa_settings[self.api_stage].get('aws_region')
if profile_name is not None:
session = boto3.Session(profile_name=profile_name, region_name=region_name)
self.zappa.load_credentials(session)
def create_package(self):
"""
Ensure that the package can be properly configured,
and then create it.
"""
# Create the Lambda zip package (includes project and virtualenvironment)
# Also define the path the handler file so it can be copied to the zip
# root for Lambda.
current_file = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
handler_file = os.sep.join(current_file.split(os.sep)[
0:-2]) + os.sep + 'handler.py'
exclude = ['static', 'media']
self.zip_path = self.zappa.create_lambda_zip(
self.lambda_name,
handler_file=handler_file,
use_precompiled_packages=self.zappa_settings.get('use_precompiled_packages', True),
exclude=exclude
)
# Add this environment's Django settings to that zipfile
with open(self.settings_file, 'r') as f:
contents = f.read()
all_contents = contents
if not self.zappa_settings[self.api_stage].has_key('domain'):
script_name = self.api_stage
else:
script_name = ''
all_contents = all_contents + \
'\n# Automatically added by Zappa:\nSCRIPT_NAME=\'/' + script_name + '\'\n'
f.close()
with open('zappa_settings.py', 'w') as f:
f.write(all_contents)
with zipfile.ZipFile(self.zip_path, 'a') as lambda_zip:
lambda_zip.write('zappa_settings.py', 'zappa_settings.py')
lambda_zip.close()
os.unlink('zappa_settings.py')
def remove_s3_local_settings(self):
# Remove the settings file if downloaded from S3
if self.get_settings_location().startswith('s3://'):
os.remove(self.settings_file)
def remove_local_zip(self):
"""
Remove our local zip file.
"""
if self.zappa_settings[self.api_stage].get('delete_zip', True):
os.remove(self.zip_path)
def remove_uploaded_zip(self):
"""
Remove the local and S3 zip file after uploading and updating.
"""
# Remove the uploaded zip from S3, because it is now registered..
self.zappa.remove_from_s3(self.zip_path, self.s3_bucket_name)
# Finally, delete the local copy our zip package
self.remove_local_zip()
示例6: test_create_lambda_package
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
def test_create_lambda_package(self):
self.assertTrue(True)
z = Zappa()
path = z.create_lambda_zip(handler_file=os.path.realpath(__file__))
self.assertTrue(os.path.isfile(path))
os.remove(path)
示例7: test_create_lambda_package
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
def test_create_lambda_package(self):
self.assertTrue(True)
z = Zappa()
path = z.create_lambda_zip()
self.assertTrue(os.path.isfile(path))
os.remove(path)
示例8: handle
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
def handle(self, *args, **options):
"""
Execute the command.
"""
if not options.has_key('environment'):
print("You must call deploy with an environment name. \n python manage.py deploy <environment>")
return
from django.conf import settings
if not 'ZAPPA_SETTINGS' in dir(settings):
print("Please define your ZAPPA_SETTINGS in your settings file before deploying.")
return
zappa_settings = settings.ZAPPA_SETTINGS
# Set your configuration
project_name = settings.BASE_DIR.split(os.sep)[-1]
api_stage = options['environment'][0]
if api_stage not in zappa_settings.keys():
print("Please make sure that the environment '" + api_stage + "' is defined in your ZAPPA_SETTINGS in your settings file before deploying.")
return
# Make your Zappa object
zappa = Zappa()
# Load environment-specific settings
s3_bucket_name = zappa_settings[api_stage]['s3_bucket']
settings_file = zappa_settings[api_stage]['settings_file']
if '~' in settings_file:
settings_file = settings_file.replace('~', os.path.expanduser('~'))
if not os.path.isfile(settings_file):
print("Please make sure your settings_file is properly defined.")
return
custom_settings = [
'http_methods',
'parameter_depth',
'integration_response_codes',
'method_response_codes',
'role_name',
'aws_region'
]
for setting in custom_settings:
if zappa_settings[api_stage].has_key(setting):
setattr(zappa, setting, zappa_settings[api_stage][setting])
# Load your AWS credentials from ~/.aws/credentials
zappa.load_credentials()
# Create the Lambda zip package (includes project and virtualenvironment)
# Also define the path the handler file so it can be copied to the zip root for Lambda.
current_file = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
handler_file = os.sep.join(current_file.split(os.sep)[0:-2]) + os.sep + 'handler.py'
lambda_name = project_name + '-' + api_stage
zip_path = zappa.create_lambda_zip(lambda_name, handler_file=handler_file)
# Add this environment's Django settings to that zipfile
with zipfile.ZipFile(zip_path, 'a') as lambda_zip:
lambda_zip.write(settings_file, 'zappa_settings.py')
lambda_zip.close()
# Upload it to S3
zip_arn = zappa.upload_to_s3(zip_path, s3_bucket_name)
# Register the Lambda function with that zip as the source
# You'll also need to define the path to your lambda_handler code.
lambda_arn = zappa.update_lambda_function(s3_bucket_name, zip_path, lambda_name)
# Get the URL!
endpoint_url = zappa.get_api_url(lambda_name)
# Finally, delete the local copy our zip package
os.remove(zip_path)
# Remove the uploaded zip from S3, because it is now registered..
zappa.remove_from_s3(zip_path, s3_bucket_name)
print("Your updated Zappa deployment is live!")
return
示例9: ZappaCommand
# 需要导入模块: from zappa.zappa import Zappa [as 别名]
# 或者: from zappa.zappa.Zappa import create_lambda_zip [as 别名]
#.........这里部分代码省略.........
raise ImproperlyConfigured
# Load environment-specific settings
self.s3_bucket_name = self.zappa_settings[self.api_stage]['s3_bucket']
self.vpc_config = self.zappa_settings[
self.api_stage].get('vpc_config', {})
self.memory_size = self.zappa_settings[
self.api_stage].get('memory_size', 512)
self.settings_file = self.zappa_settings[
self.api_stage]['settings_file']
if '~' in self.settings_file:
self.settings_file = self.settings_file.replace(
'~', os.path.expanduser('~'))
if not os.path.isfile(self.settings_file):
print("Please make sure your settings_file is properly defined.")
raise ImproperlyConfigured
custom_settings = [
'http_methods',
'parameter_depth',
'integration_response_codes',
'method_response_codes',
'role_name',
'aws_region'
]
for setting in custom_settings:
if self.zappa_settings[self.api_stage].has_key(setting):
setattr(self.zappa, setting, self.zappa_settings[
self.api_stage][setting])
def create_package(self):
"""
Ensure that the package can be properly configured,
and then create it.
"""
# Create the Lambda zip package (includes project and virtualenvironment)
# Also define the path the handler file so it can be copied to the zip
# root for Lambda.
current_file = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
handler_file = os.sep.join(current_file.split(os.sep)[
0:-2]) + os.sep + 'handler.py'
exclude = ['static', 'media']
self.zip_path = self.zappa.create_lambda_zip(
self.lambda_name,
handler_file=handler_file,
use_precompiled_packages=self.zappa_settings.get('use_precompiled_packages', True),
exclude=exclude
)
# Add this environment's Django settings to that zipfile
with open(self.settings_file, 'r') as f:
contents = f.read()
all_contents = contents
if not self.zappa_settings[self.api_stage].has_key('domain'):
script_name = self.api_stage
else:
script_name = ''
if not "ZappaMiddleware" in all_contents:
print("\n\nWARNING!\n")
print(
"You do not have ZappaMiddleware in your remote settings's MIDDLEWARE_CLASSES.\n")
print(
"This means that some aspects of your application may not work!\n\n")
all_contents = all_contents + \
'\n# Automatically added by Zappa:\nSCRIPT_NAME=\'/' + script_name + '\'\n'
f.close()
with open('zappa_settings.py', 'w') as f:
f.write(all_contents)
with zipfile.ZipFile(self.zip_path, 'a') as lambda_zip:
lambda_zip.write('zappa_settings.py', 'zappa_settings.py')
lambda_zip.close()
os.unlink('zappa_settings.py')
def remove_local_zip(self):
"""
Remove our local zip file.
"""
if self.zappa_settings[self.api_stage].get('delete_zip', True):
os.remove(self.zip_path)
def remove_uploaded_zip(self):
"""
Remove the local and S3 zip file after uploading and updating.
"""
# Remove the uploaded zip from S3, because it is now registered..
self.zappa.remove_from_s3(self.zip_path, self.s3_bucket_name)
# Finally, delete the local copy our zip package
self.remove_local_zip()