Skip to content

Commit

Permalink
[cli][tf] lambda tf module generation now expects the lambda config f…
Browse files Browse the repository at this point in the history
…rom caller
  • Loading branch information
ryandeivert committed Mar 29, 2018
1 parent 42e93f3 commit 55afe2e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
4 changes: 3 additions & 1 deletion stream_alert_cli/terraform/alert_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def generate_alert_merger(config):

# Set variables for the Lambda module
result['module']['alert_merger_lambda'] = generate_lambda(
ALERT_MERGER_NAME, config,
'{}_streamalert_{}'.format(config['global']['account']['prefix'], ALERT_MERGER_NAME),
config['lambda']['alert_merger_config'],
config,
environment={
'ALERTS_TABLE': '{}_streamalert_alerts'.format(prefix),
'ALERT_PROCESSOR': '{}_streamalert_alert_processor'.format(prefix),
Expand Down
9 changes: 7 additions & 2 deletions stream_alert_cli/terraform/alert_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ def generate_alert_processor(config):

# Set variables for the Lambda module
result['module']['alert_processor_lambda'] = generate_lambda(
ALERT_PROCESSOR_NAME, config,
environment={'ALERTS_TABLE': '{}_streamalert_alerts'.format(prefix)})
'{}_streamalert_{}'.format(config['global']['account']['prefix'], ALERT_PROCESSOR_NAME),
config['lambda']['alert_processor_config'],
config,
environment={
'ALERTS_TABLE': '{}_streamalert_alerts'.format(prefix)
}
)

return result
4 changes: 2 additions & 2 deletions stream_alert_cli/terraform/app_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def generate_app_integrations(cluster_name, cluster_dict, config):
# Format the lambda module with 'app_<app_name_<cluster>_lambda'
cluster_dict['module']['{}_lambda'.format(module_prefix)] = generate_lambda(
'{}_app'.format(func_prefix),
config,
cluster=cluster_name
config['clusters'][cluster_name]['modules']['stream_alert_apps'][function_name],
config
)
24 changes: 3 additions & 21 deletions stream_alert_cli/terraform/lambda_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,10 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from stream_alert import shared
from stream_alert.shared import metrics
from stream_alert_cli.terraform.common import monitoring_topic_arn


def _lambda_config(function_name, config, cluster=None):
"""Find the config specific to this Lambda function."""
if function_name == shared.ALERT_MERGER_NAME:
return config['lambda']['alert_merger_config']
elif function_name == shared.ALERT_PROCESSOR_NAME:
return config['lambda']['alert_processor_config']
elif cluster is not None and function_name.endswith('_app'):
return config['clusters'][cluster]['modules']['stream_alert_apps'][function_name]
else:
raise NotImplementedError(
'Lambda modules are not yet supported for {}'.format(function_name))


def _tf_metric_alarms(lambda_config, sns_arn):
"""Compute metric alarm Terraform configuration from the Lambda config."""
result = {}
Expand Down Expand Up @@ -82,7 +68,7 @@ def _tf_vpc_config(lambda_config):
return result


def generate_lambda(function_name, config, **kwargs):
def generate_lambda(function_name, lambda_config, config, environment=None):
"""Generate an instance of the Lambda Terraform module.
Args:
Expand Down Expand Up @@ -130,24 +116,20 @@ def generate_lambda(function_name, config, **kwargs):
Returns:
dict: Terraform config for an instance of the tf_lambda module.
"""
lambda_config = _lambda_config(function_name, config, kwargs.get('cluster'))

# Add logger level to any custom environment variables
environment_variables = {
# Convert True/False to "1" or "0", respectively
'ENABLE_METRICS': str(int(lambda_config.get('enable_metrics', False))),
'LOGGER_LEVEL': lambda_config.get('log_level', 'info')
}

environment = kwargs.get('environment')
if environment:
environment_variables.update(environment)

lambda_module = {
'source': 'modules/tf_lambda',
'function_name': '{}_streamalert_{}'.format(config['global']['account']['prefix'],
function_name),
'description': 'StreamAlert {}'.format(function_name.replace('_', ' ').title()),
'function_name': function_name,
'description': function_name.replace('_', ' ').title(),
'handler': lambda_config['handler'],
'memory_size_mb': lambda_config['memory'],
'timeout_sec': lambda_config['timeout'],
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/stream_alert_cli/terraform/test_alert_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_generate_all_options(self):
'alert_processor_lambda': {
'alarm_actions': ['arn:aws:sns:us-west-1:12345678910:stream_alert_monitoring'],
'aliased_version': '$LATEST',
'description': 'StreamAlert Alert Processor',
'description': 'Unit-Testing Streamalert Alert Processor',
'environment_variables': {
'ALERTS_TABLE': 'unit-testing_streamalert_alerts',
'ENABLE_METRICS': '0',
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_generate_minimal_options(self):
},
'alert_processor_lambda': {
'aliased_version': '$LATEST',
'description': 'StreamAlert Alert Processor',
'description': 'Unit-Testing Streamalert Alert Processor',
'environment_variables': {
'ALERTS_TABLE': 'unit-testing_streamalert_alerts',
'ENABLE_METRICS': '0',
Expand Down

0 comments on commit 55afe2e

Please sign in to comment.