Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to customize naming convention #1927

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion chalice/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from chalice.app import Chalice # noqa
from chalice.constants import DEFAULT_STAGE_NAME
from chalice.constants import DEFAULT_HANDLER_NAME
from chalice.constants import DEFAULT_LAMBDA_NAME_PREFIX_CONVENTION


StrMap = Dict[str, Any]
Expand Down Expand Up @@ -431,7 +432,10 @@ def _load_json_file(self, deployed_file):
def _upgrade_deployed_values(self, chalice_stage_name, data):
# type: (str, Any) -> DeployedResources
deployed = data[chalice_stage_name]
prefix = '%s-%s-' % (self.app_name, chalice_stage_name)
prefix = DEFAULT_LAMBDA_NAME_PREFIX_CONVENTION.format(
app_name=self.app_name,
chalice_stage=chalice_stage_name,
)
resources = [] # type: List[Dict[str, Any]]
self._upgrade_lambda_functions(resources, deployed, prefix)
self._upgrade_rest_api(resources, deployed)
Expand Down
7 changes: 7 additions & 0 deletions chalice/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,10 @@ def index():
],
"Resource": "arn:*:execute-api:*:*:*/@connections/*"
}

DEFAULT_LAMBDA_NAME_PREFIX_CONVENTION = "{app_name}-{chalice_stage}"
DEFAULT_LAMBDA_FUNC_NAME_CONVENTION = "{app_name}-{chalice_stage}-{func_name}"
DEFAULT_LAMBDA_LAYER_NAME_CONVENTION = "{app_name}-{chalice_stage}-{layer_name}"
DEFAULT_LAMBDA_ROLE_NAME_CONVENTION = "{app_name}-{chalice_stage}-{func_name}"
DEFAULT_LAMBDA_DEFAULT_ROLE_NAME_CONVENTION = "{app_name}-{chalice_stage}"
DEFAULT_EVENT_RULE_NAME_CONVENTION = "{app_name}-{chalice_stage}-{resource_name}"
50 changes: 38 additions & 12 deletions chalice/deploy/appgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

from chalice.config import Config # noqa
from chalice import app
from chalice.constants import LAMBDA_TRUST_POLICY
from chalice.constants import (
LAMBDA_TRUST_POLICY,
DEFAULT_LAMBDA_FUNC_NAME_CONVENTION,
DEFAULT_LAMBDA_LAYER_NAME_CONVENTION,
DEFAULT_LAMBDA_ROLE_NAME_CONVENTION,
DEFAULT_LAMBDA_DEFAULT_ROLE_NAME_CONVENTION,
DEFAULT_EVENT_RULE_NAME_CONVENTION,
)
from chalice.deploy import models
from chalice.utils import UI # noqa

Expand Down Expand Up @@ -278,8 +285,11 @@ def _create_cwe_subscription(
)

resource_name = event_source.name + '-event'
rule_name = '%s-%s-%s' % (config.app_name, config.chalice_stage,
resource_name)
rule_name = DEFAULT_EVENT_RULE_NAME_CONVENTION.format(
app_name=config.app_name,
chalice_stage=config.chalice_stage,
resource_name=resource_name,
)
cwe = models.CloudWatchEvent(
resource_name=resource_name,
rule_name=rule_name,
Expand Down Expand Up @@ -314,8 +324,12 @@ def _create_scheduled_model(self,
expression = event_source.schedule_expression.to_string()
else:
expression = event_source.schedule_expression
rule_name = '%s-%s-%s' % (config.app_name, config.chalice_stage,
resource_name)

rule_name = DEFAULT_EVENT_RULE_NAME_CONVENTION.format(
app_name=config.app_name,
chalice_stage=config.chalice_stage,
resource_name=resource_name,
)
scheduled_event = models.ScheduledEvent(
resource_name=resource_name,
rule_name=rule_name,
Expand Down Expand Up @@ -376,8 +390,11 @@ def _get_managed_lambda_layer(self, config):
if self._managed_layer is None:
self._managed_layer = models.LambdaLayer(
resource_name='managed-layer',
layer_name='%s-%s-%s' % (
config.app_name, config.chalice_stage, 'managed-layer'),
layer_name=DEFAULT_LAMBDA_LAYER_NAME_CONVENTION.format(
app_name=config.app_name,
chalice_stage=config.chalice_stage,
layer_name='managed-layer',
),
runtime=config.lambda_python_version,
deployment_package=models.DeploymentPackage(
models.Placeholder.BUILD_STAGE)
Expand Down Expand Up @@ -418,8 +435,11 @@ def _create_role_reference(self, config, stage_name, function_name):
policy = models.IAMPolicy(document=models.Placeholder.BUILD_STAGE)
if not config.autogen_policy:
resource_name = '%s_role' % function_name
role_name = '%s-%s-%s' % (config.app_name, stage_name,
function_name)
role_name = DEFAULT_LAMBDA_ROLE_NAME_CONVENTION.format(
app_name=config.app_name,
chalice_stage=stage_name,
func_name=function_name,
)
if config.iam_policy_file is not None:
filename = os.path.join(config.project_dir,
'.chalice',
Expand All @@ -432,7 +452,10 @@ def _create_role_reference(self, config, stage_name, function_name):
filename=filename, document=models.Placeholder.BUILD_STAGE)
else:
resource_name = 'default-role'
role_name = '%s-%s' % (config.app_name, stage_name)
role_name = DEFAULT_LAMBDA_DEFAULT_ROLE_NAME_CONVENTION.format(
app_name=config.app_name,
chalice_stage=stage_name,
)
policy = models.AutoGenIAMPolicy(
document=models.Placeholder.BUILD_STAGE,
traits=set([]),
Expand Down Expand Up @@ -475,8 +498,11 @@ def _build_lambda_function(self,
role, # type: models.IAMRole
):
# type: (...) -> models.LambdaFunction
function_name = '%s-%s-%s' % (
config.app_name, config.chalice_stage, name)
function_name = DEFAULT_LAMBDA_FUNC_NAME_CONVENTION.format(
app_name=config.app_name,
chalice_stage=config.chalice_stage,
func_name=name,
)
security_group_ids, subnet_ids = self._get_vpc_params(name, config)
lambda_layers = self._get_lambda_layers(config)
function = models.LambdaFunction(
Expand Down