Skip to content

Commit

Permalink
Adds manage_role option to address Miserlou#199
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Jones committed Aug 11, 2016
1 parent 7294b88 commit 639f830
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ to change Zappa's behavior. Use these at your own risk!
"keep_warm_expression": "rate(5 minutes)", // How often to execute the keep-warm, in cron and rate format. Default 5 minutes.
"lambda_handler": "your_custom_handler", // The name of Lambda handler. Default: handler.lambda_handler
"log_level": "DEBUG", // Set the Zappa log level. Default INFO, can be one of CRITICAL, ERROR, WARNING, INFO and DEBUG.
"manage_roles": true, // Have Zappa automatically create and define IAM execution roles and policies. Default true. If false, you must define your own IAM Role and role_name setting.
"memory_size": 512, // Lambda function memory in MB
"method_header_types": [ // Which headers to include in the API response
'Content-Type',
Expand All @@ -267,7 +268,7 @@ to change Zappa's behavior. Use these at your own risk!
"project_name": "MyProject", // The name of the project as it appears on AWS. Defaults to a slugified `pwd`.
"remote_env_bucket": "my-project-config-files", // optional s3 bucket where remote_env_file can be located.
"remote_env_file": "filename.json", // file in remote_env_bucket containing a flat json object which will be used to set custom environment variables.
"role_name": "MyLambdaRole", // Lambda execution Role
"role_name": "MyLambdaRole", // Name of Zappa execution role. Default ZappaExecutionRole. To use a different, pre-existing policy, you must also set manage_roles to false.
"s3_bucket": "dev-bucket", // Zappa zip bucket,
"settings_file": "~/Projects/MyApp/settings/dev_settings.py", // Server side settings file location,
"timeout_seconds": 30, // Maximum lifespan for the Lambda function (default 30)
Expand Down
31 changes: 20 additions & 11 deletions zappa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class ZappaCLI(object):
use_apigateway = None
lambda_handler = None
django_settings = None
manage_roles = True

def handle(self, argv=None):
"""
Expand Down Expand Up @@ -219,7 +220,8 @@ def deploy(self):
self.execute_prebuild_script()

# Make sure the necessary IAM execution roles are available
self.zappa.create_iam_roles()
if self.manage_iam:
self.zappa.create_iam_roles()

# Create the Lambda Zip
self.create_package()
Expand Down Expand Up @@ -292,7 +294,8 @@ def update(self):
self.execute_prebuild_script()

# Make sure the necessary IAM execution roles are available
self.zappa.create_iam_roles()
if self.manage_roles:
self.zappa.create_iam_roles()

# Create the Lambda Zip,
self.create_package()
Expand Down Expand Up @@ -493,30 +496,33 @@ def tabular_print(title, value):
Convience function for priting formatted table items.
"""
click.echo('%-*s%s' % (32, click.style("\t" + title, fg='green') + ':', str(value)))
return

# Lambda Env Details
lambda_versions = self.zappa.get_lambda_function_versions(self.lambda_name)
if not lambda_versions:
click.echo(click.style("\tNo Lambda detected - have you deployed yet?", fg='red'))
return
else:
tabular_print("Lambda Versions", len(lambda_versions))

tabular_print("Lambda Name", self.lambda_name)

function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
conf = function_response['Configuration']


tabular_print("Lambda Name", self.lambda_name)
tabular_print("Lambda ARN", conf['FunctionArn'])
tabular_print("Lambda Role", conf['Role'])
tabular_print("Lambda Role ARN", conf['Role'])
tabular_print("Lambda Handler", conf['Handler'])
tabular_print("Lambda Code Size", conf['CodeSize'])
tabular_print("Lambda Version", conf['Version'])
tabular_print("Lambda Last Modified", conf['LastModified'])
tabular_print("Lambda Memory Size", conf['MemorySize'])
tabular_print("Lambda Timeout", conf['Timeout'])
tabular_print("Lambda Runtime", conf['Runtime'])
if 'VpcConfig' in conf.keys():
tabular_print("Lambda VPC ID", conf['VpcConfig']['VpcId'])
else:
tabular_print("Lambda VPC ID", None)

# Calculated statistics
try:
function_invocations = self.zappa.cloudwatch.get_metric_statistics(
Namespace='AWS/Lambda',
Expand Down Expand Up @@ -548,22 +554,21 @@ def tabular_print(title, value):
error_rate = "{0:.2f}%".format(function_errors / function_invocations * 100)
except:
error_rate = "Error calculating"

tabular_print("Invocations (24h)", int(function_invocations))
tabular_print("Errors (24h)", int(function_errors))
tabular_print("Error Rate (24h)", error_rate)

# URLs
api_url = self.zappa.get_api_url(
self.lambda_name,
self.api_stage)
tabular_print("API Gateway URL", api_url)

domain_url = self.zappa_settings[self.api_stage].get('domain', None)
tabular_print("Domain URL", domain_url)

# Scheduled Events
event_rules = self.zappa.get_event_rules_for_arn(conf['FunctionArn'])
tabular_print("Num. Event Rules", len(event_rules))

for rule in event_rules:
rule_name = rule['Name']
print('')
Expand All @@ -572,6 +577,8 @@ def tabular_print(title, value):
tabular_print("Event Rule State", rule.get(u'State', None).title())
tabular_print("Event Rule ARN", rule.get(u'Arn', None))

# TODO: S3/SQS/etc. type events?

def print_version(self): # pragma: no cover
"""
Print the current zappa version.
Expand Down Expand Up @@ -805,6 +812,8 @@ def load_settings(self, settings_file="zappa_settings.json", session=None):
self.api_stage].get('settings_file', None)
self.django_settings = self.zappa_settings[
self.api_stage].get('django_settings', None)
self.manage_roles = self.zappa_settings[
self.api_stage].get('manage_roles', True)

self.zappa = Zappa(boto_session=session, profile_name=self.profile_name, aws_region=self.aws_region)

Expand Down

0 comments on commit 639f830

Please sign in to comment.