def test_sam_injects_policy(self, sample_app): function = models.LambdaFunction( resource_name='foo', function_name='app-dev-foo', environment_variables={}, runtime='python27', handler='app.app', tags={'foo': 'bar'}, timeout=120, memory_size=128, deployment_package=models.DeploymentPackage(filename='foo.zip'), role=models.ManagedIAMRole( resource_name='role', role_name='app-role', trust_policy={}, policy=models.AutoGenIAMPolicy(document={'iam': 'policy'}), )) template = self.template_gen.generate_sam_template([function]) cfn_resource = list(template['Resources'].values())[0] assert cfn_resource == { 'Type': 'AWS::Serverless::Function', 'Properties': { 'CodeUri': 'foo.zip', 'Handler': 'app.app', 'MemorySize': 128, 'Role': { 'Fn::GetAtt': ['Role', 'Arn'] }, 'Runtime': 'python27', 'Tags': { 'foo': 'bar' }, 'Timeout': 120 }, }
def _build_lambda_function( self, config, # type: Config name, # type: str handler_name, # type: str deployment, # type: models.DeploymentPackage role, # type: models.IAMRole ): # type: (...) -> models.LambdaFunction function_name = '%s-%s-%s' % (config.app_name, config.chalice_stage, name) security_group_ids, subnet_ids = self._get_vpc_params(name, config) function = models.LambdaFunction( resource_name=name, function_name=function_name, environment_variables=config.environment_variables, runtime=config.lambda_python_version, handler=handler_name, tags=config.tags, timeout=config.lambda_timeout, memory_size=config.lambda_memory_size, deployment_package=deployment, role=role, security_group_ids=security_group_ids, subnet_ids=subnet_ids, ) self._inject_role_traits(function, role) return function
def test_role_arn_inserted_when_necessary(self): function = models.LambdaFunction( resource_name='foo', function_name='app-dev-foo', environment_variables={}, runtime='python27', handler='app.app', tags={'foo': 'bar'}, timeout=120, memory_size=128, deployment_package=models.DeploymentPackage(filename='foo.zip'), role=models.PreCreatedIAMRole(role_arn='role:arn'), ) template = self.template_gen.generate_sam_template([function]) cfn_resource = list(template['Resources'].values())[0] assert cfn_resource == { 'Type': 'AWS::Serverless::Function', 'Properties': { 'CodeUri': 'foo.zip', 'Handler': 'app.app', 'MemorySize': 128, 'Role': 'role:arn', 'Runtime': 'python27', 'Tags': { 'foo': 'bar' }, 'Timeout': 120 }, }
def test_can_build_lambda_function_app_with_vpc_config( self, sample_app_lambda_only): @sample_app_lambda_only.lambda_function() def foo(event, context): pass builder = ApplicationGraphBuilder() config = self.create_config(sample_app_lambda_only, iam_role_arn='role:arn', security_group_ids=['sg1', 'sg2'], subnet_ids=['sn1', 'sn2']) application = builder.build(config, stage_name='dev') assert application.resources[0] == models.LambdaFunction( resource_name='myfunction', function_name='lambda-only-dev-myfunction', environment_variables={}, runtime=config.lambda_python_version, handler='app.myfunction', tags=config.tags, timeout=None, memory_size=None, deployment_package=models.DeploymentPackage( models.Placeholder.BUILD_STAGE), role=models.PreCreatedIAMRole('role:arn'), security_group_ids=['sg1', 'sg2'], subnet_ids=['sn1', 'sn2'], layers=[], reserved_concurrency=None, xray=None, )
def test_no_injection_when_values_are_set(self): injector = InjectDefaults( lambda_timeout=100, lambda_memory_size=512, ) function = models.LambdaFunction( # The timeout/memory_size are set to # None, so the injector should fill them # in the with the default values above. timeout=1, memory_size=1, resource_name='foo', function_name='app-stage-foo', environment_variables={}, runtime='python2.7', handler='app.app', tags={}, xray=None, deployment_package=None, role=None, security_group_ids=[], subnet_ids=[], layers=[], reserved_concurrency=None, ) config = Config.create() injector.handle(config, function) assert function.timeout == 1 assert function.memory_size == 1
def test_can_build_lambda_function_app_with_reserved_concurrency( self, sample_app_lambda_only): # This is the simplest configuration we can get. builder = ApplicationGraphBuilder() config = self.create_config(sample_app_lambda_only, iam_role_arn='role:arn', reserved_concurrency=5) application = builder.build(config, stage_name='dev') # The top level resource is always an Application. assert isinstance(application, models.Application) assert len(application.resources) == 1 assert application.resources[0] == models.LambdaFunction( resource_name='myfunction', function_name='lambda-only-dev-myfunction', environment_variables={}, runtime=config.lambda_python_version, handler='app.myfunction', tags=config.tags, timeout=None, memory_size=None, deployment_package=models.DeploymentPackage( models.Placeholder.BUILD_STAGE), role=models.PreCreatedIAMRole('role:arn'), security_group_ids=[], subnet_ids=[], layers=[], reserved_concurrency=5, xray=None, )
def create_function_resource(name, function_name=None, environment_variables=None, runtime='python2.7', handler='app.app', tags=None, timeout=60, memory_size=128, deployment_package=None, role=None): if function_name is None: function_name = 'appname-dev-%s' % name if environment_variables is None: environment_variables = {} if tags is None: tags = {} if deployment_package is None: deployment_package = models.DeploymentPackage(filename='foo') if role is None: role = models.PreCreatedIAMRole(role_arn='role:arn') return models.LambdaFunction( resource_name=name, function_name=function_name, environment_variables=environment_variables, runtime=runtime, handler=handler, tags=tags, timeout=timeout, memory_size=memory_size, deployment_package=deployment_package, role=role, security_group_ids=[], subnet_ids=[], reserved_concurrency=None, )
def lambda_function(): return models.LambdaFunction( resource_name='foo', function_name='app-stage-foo', deployment_package=None, environment_variables={}, runtime='python2.7', handler='app.app', tags={}, timeout=None, memory_size=None, role=models.PreCreatedIAMRole(role_arn='foobar'))
def lambda_function(self): return models.LambdaFunction( resource_name='foo', function_name='app-dev-foo', environment_variables={}, runtime='python27', handler='app.app', tags={'foo': 'bar'}, timeout=120, memory_size=128, deployment_package=models.DeploymentPackage(filename='foo.zip'), role=models.PreCreatedIAMRole(role_arn='role:arn'), )
def create_function_resource(name): return models.LambdaFunction( resource_name=name, function_name='appname-dev-%s' % name, environment_variables={}, runtime='python2.7', handler='app.app', tags={}, timeout=60, memory_size=128, deployment_package=models.DeploymentPackage(filename='foo'), role=models.PreCreatedIAMRole(role_arn='role:arn'), security_group_ids=[], subnet_ids=[], layers=[])
def lambda_function(): return models.LambdaFunction( resource_name='foo', function_name='app-stage-foo', deployment_package=None, environment_variables={}, runtime='python2.7', handler='app.app', tags={}, timeout=None, memory_size=None, role=models.PreCreatedIAMRole(role_arn='foobar'), security_group_ids=[], subnet_ids=[], reserved_concurrency=None, )
def lambda_function(self): return models.LambdaFunction( resource_name='foo', function_name='app-dev-foo', environment_variables={}, runtime='python27', handler='app.app', tags={'foo': 'bar'}, timeout=120, memory_size=128, deployment_package=models.DeploymentPackage(filename='foo.zip'), role=models.PreCreatedIAMRole(role_arn='role:arn'), security_group_ids=[], subnet_ids=[], reserved_concurrency=None, layers=None, )
def create_function_resource(name): return models.LambdaFunction( resource_name=name, function_name='appname-dev-%s' % name, environment_variables={}, runtime='python2.7', handler='app.app', tags={}, timeout=60, memory_size=128, deployment_package=models.DeploymentPackage( models.Placeholder.BUILD_STAGE), xray=False, role=models.PreCreatedIAMRole(role_arn='role:arn'), security_group_ids=[], subnet_ids=[], layers=[], reserved_concurrency=None, )
def _build_lambda_function( self, config, # type: Config name, # type: str handler_name, # type: str deployment, # type: models.DeploymentPackage role, # type: models.IAMRole ): # type: (...) -> models.LambdaFunction function_name = '%s-%s-%s' % (config.app_name, config.chalice_stage, name) return models.LambdaFunction( resource_name=name, function_name=function_name, environment_variables=config.environment_variables, runtime=config.lambda_python_version, handler=handler_name, tags=config.tags, timeout=config.lambda_timeout, memory_size=config.lambda_memory_size, deployment_package=deployment, role=role, )