diff --git a/chalice/package.py b/chalice/package.py index 58da8b33b4..dd365445b4 100644 --- a/chalice/package.py +++ b/chalice/package.py @@ -597,6 +597,8 @@ def generate(self, resources): }, 'provider': { 'aws': {'version': '> 2.0.0'}, + 'local': {'version': '> 1.0'}, + 'template': {'version': '> 2.0'}, }, 'data': { 'aws_caller_identity': {'chalice': {}}, @@ -606,6 +608,8 @@ def generate(self, resources): for resource in resources: self.dispatch(resource, template) + if self._config.project_dir: + self._generate_deployed_resources(template) return template def _fref(self, lambda_function, attr='arn'): @@ -621,6 +625,36 @@ def _arnref(self, arn_template, **kw): d.update(kw) return arn_template % d + def _generate_deployed_resources(self, template): + # Create enough deployment resource output for the chalice + # cli subcommands to work (invoke, logs, url). + resources = [] + for k, v in template['resource']['aws_lambda_function'].items(): + resources.append({ + 'name': k, + 'resource_type': 'lambda_function', + 'lambda_arn': ( + '${aws_lambda_function.%s.arn}' % k) + }) + resources.append({ + 'name': 'rest_api', + 'resource_type': 'rest_api', + 'rest_api_id': '${aws_api_gateway_rest_api.rest_api.id}', + 'rest_api_url': ( + '${aws_api_gateway_deployment.rest_api.invoke_url}') + }) + template['resource'].setdefault( + 'local_file', {})['chalice_deployed_resources'] = { + 'filename': os.path.join( + self._config.project_dir, '.chalice', 'deployed', + '%s.json' % self._config.chalice_stage), + # not really sensitive but avoid dumping diffs + 'sensitive_content': json.dumps({ + 'schema_version': '2.0', + 'backend': 'terraform', + 'resources': resources}) + } + def _generate_managediamrole(self, resource, template): # type: (models.ManagedIAMRole, Dict[str, Any]) -> None template['resource'].setdefault('aws_iam_role', {})[ diff --git a/tests/unit/test_package.py b/tests/unit/test_package.py index 99297dd567..c0f828731f 100644 --- a/tests/unit/test_package.py +++ b/tests/unit/test_package.py @@ -411,6 +411,32 @@ def test_can_generate_rest_api(self, sample_app_with_auth): 'value': '${aws_api_gateway_deployment.rest_api.invoke_url}'} } + def test_can_generate_chalice_deployed_resources(self, sample_app): + config = Config.create(chalice_app=sample_app, + project_dir='.', + app_name='foo', + api_gateway_stage='api') + template = self.generate_template(config, 'dev') + assert template['resource'][ + 'local_file'][ + 'chalice_deployed_resources'][ + 'filename'] == "./.chalice/deployed/dev.json" + assert json.loads(template['resource']['local_file'][ + 'chalice_deployed_resources']['sensitive_content']) == { + 'backend': 'terraform', + 'schema_version': '2.0', + 'resources': [ + {'lambda_arn': '${aws_lambda_function.api_handler.arn}', + 'name': 'api_handler', + 'resource_type': 'lambda_function'}, + {'name': 'rest_api', + 'resource_type': 'rest_api', + 'rest_api_id': '${aws_api_gateway_rest_api.rest_api.id}', + 'rest_api_url': ( + '${aws_api_gateway_deployment.rest_api.invoke_url}')} + ] + } + def test_can_package_s3_event_handler_with_tf_ref(self, sample_app): @sample_app.on_s3_event( bucket='${aws_s3_bucket.my_data_bucket.id}')