Skip to content

Commit

Permalink
terraform package, output local deployed resources json for chalice cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kapilt committed Aug 5, 2019
1 parent fb82f6b commit 5a11112
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
34 changes: 34 additions & 0 deletions chalice/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {}},
Expand All @@ -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'):
Expand All @@ -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', {})[
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down

0 comments on commit 5a11112

Please sign in to comment.