-
Notifications
You must be signed in to change notification settings - Fork 1k
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
terraform support #1129
terraform support #1129
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1129 +/- ##
==========================================
+ Coverage 95.89% 95.98% +0.09%
==========================================
Files 28 28
Lines 5068 5183 +115
Branches 641 660 +19
==========================================
+ Hits 4860 4975 +115
Misses 135 135
Partials 73 73
Continue to review full report at Codecov.
|
Also worth noting.. This targets terraform versions under < 0.12 at the moment. Terraform just had a major release (0.12) last week which has some significant incompatibilities with previous versions. |
c1dfa13
to
4f8ddd0
Compare
fwiw, tested against 0.12 seems to work fine, i've removed that restriction in the generated output. to elaborate on some of the converged/combined provisioning capability, you can create a dynamodb table, sqs queue in terraform, and then direct reference them as env vars for the chalice app.. an example from a sample app i'm using (config.json).
at least for me this removes the need for post processing scripts, or splitting the app into multi stack deployments, etc. the one item i've been considering for enabling better/forward backward navigation of references is capturing the app name / chalice stage name as terraform variables for easier referencing, the extant generator interface doesn't allow for that at the moment though, so it will require some additional refactoring, perhaps as a followup. |
This would be really cool to add. What do you think about integration tests for terraform? I will have to go through this in more detail since I'm not very familiar with terraform so I gave it a quick once over. But Ill have to block off some time to play around with it. I wonder as we add more backends if we need to add some documentation matrix of features to backend support. |
I'm a little unfamiliar with the integration tests in chalice, and how one actually executes them or what the ci harness is for them to run, i've mostly been using Wrt to additional backends, i'm not sure if there's others that are meaningful to support (maybe cdk?), terraform integration covers off on a wide variety of use cases that cfn doesn't handle by itself well. there's definitely costs to each backend, as there will be differential feature support by backend, and new features need to be added to all of them. Your websocket branch is an example of one such, as terraform doesnt yet have support for that. update there's a pr open for websocket support in terraform now hashicorp/terraform-provider-aws#8881 One note, i do suspect it would be possible to enable automatic transition from deploy/destroy to terraform, just not sure its worth the effort, would require a separate script to transition the client side state from one to the other. |
@kapilt Is this already merged? If I don't understand it wrong, with this terraform support feature, aws chalice should be able to not only generate a cloudformation template but also terraform scripts? is this correct? |
@haduart its not merged yet, pip has solid support for installing from a branch, example below. yes this branch effectively supports provisioning/deploying a chalice app with just terraform
There's a readme with some examples in this pr, but real world uses. Also fwiw, since its taking some time to merge, i've started to maintain an integration branch with most of my pull requests here integrated ( cloud watch events support ) etc called |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome. To test it out more fully, I converted one of my workshop examples that previously relied on deploying a separate CFN stack to use terraform: kyleknap/chalice-workshop@73bcfec. It worked out really well in the end. The ability to cross reference resources from other terraform modules is super useful. It definitely cleaned up my workshop application. I did run into a couple of issues trying to convert the application. I noted it in the review. I still need to go through most of the code. So I will have a follow up review with anything I find.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went through the code more. It's looking pretty good. I had some more comments that I wanted to capture to keep the feedback process moving. I think in general like the interface and how the template gets filled out. The two more things that I want to spend more time on are: 1) Double checking the resource names currently being used are the ones we want. Specifically, "logical" resource names are something we can't change given you can cross-reference resources across terraform files. 2) Try deploying more chalice resources of different types and configuration to make sure we did not miss anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's looking good to me. I just had a few more comments based on feedback. The last thing I still wanted to do is test more of the various decorators/resources/configurations chalice supports.
def _generate_managediamrole(self, resource, template): | ||
# type: (models.ManagedIAMRole, Dict[str, Any]) -> None | ||
template['resource'].setdefault('aws_iam_role', {})[ | ||
resource.resource_name] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just making a general comment for future development. I went through the resource names we are generating. They look fine too me. The one thing that we should address (but do not need to now) is add tests to make sure that we do not accidentally change resource_name
s and have that cause a change in the resource name in the generated template. This is especially true for terraform as people will be able to reference resources from the generated chalice template to other templates. The backwards compatibility guarantee will be needed for this PR as well: #1195
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I just finished testing some of the sample applications with terraform. Overall, it worked pretty well. There were a couple of issues that I ran into that I posted in the review, but I say we are good to merge this once:
- Fix (if possible) the issues I ran into
- Add a change log entry
- Squash all of the commits into a single commit to clean up the history a bit
re 3. squashing commits should be done ideally on merge within the GitHub ui, and can be setup as mandatory for pull requests merge on GitHub project settings. The chalice project has not historically done this to date, and rather than doing so arbitrarily on some pull requests it would be better to do so uniformly by doing this via a project setting. Generally doing rebases often on pull requests is typically considered an anti-pattern by project reviewers once they've commented/reviewed as they have to reconsider the whole diff on changes as to opposed post review changes. currently looking into 1 & 2 |
I’ve been noodling again if we should be outputting the stage name as a variable here, so that other resources can use it as a prefix when provisioning to have isolated stacks. It effectively it would become a terraform variable, but we’d have to note that it can’t be user supplied via terraform in the chalice docs though. |
Could you elaborate or give an example on how the stage variable would look in the generated terraform template? |
Otherwise. I don't think I have any more feedback on the code. Confirmed that the updates resolve the two issues I ran into. |
re stage-name, so when provisioning physical assets with terraform its useful to prefix them with app-name, stage-name, so that multiple chalice stages can be deployed in the same account.
as an example in one of my terraform'd chalice apps i have a kinesis delivery stream (non relevant parts omitted), atm its currently using static names, the change would be to introduce chalice stage and app name variables so they can be referenced when constructing physical asset names as below. resource "aws_kinesis_firehose_delivery_stream" "gitter_index" {
name = "${var.chalice_app_name}-{var.chalice_stage_name}-gitter-s3-archive"
destination = "elasticsearch"
# omit various non germane configuration
} To support this we would add terraform variables with default values to the terraform module. Generators don't have access to the app name or chalice stage name atm (though they can infer from rest api stage name or lambda info tags). The clean way of doing this would be updating generator class constructor signature to also take a chalice Config. The caveat is that these chalice prefix'd variables can't be user defined via the cli, because other chalice generate assets embeds the literal values of those variables into output'd resources. The description for the variables and the chalice documentation would both note this restriction. -- One other item in looking at the chalice package cli, it feels like this needs a bit more help text. $ chalice package --help
Usage: chalice package [OPTIONS] OUT
Options:
--single-file Create a single packaged file. By default,
the 'out' argument specifies a directory in
which the package assets will be placed. If
this argument is specified, a single zip
file will be created instead.
--stage TEXT
--pkg-format [cloudformation|terraform]
--merge-template TEXT Specify a JSON template to be merged into
the generated template. This is useful for
adding resources to a Chalice template or
modify values in the template.
--help Show this message and exit. ie pkg-format and also note for --single-file and --merge-template options that they only apply to cloudformation engine. |
i've gone ahead and added in terraform variables for chalice app_name and stage_name, and updated the cli help. |
5a11112
to
5388f1f
Compare
added cli compatibility with via local addition of deployed_resources json stub to support subcommands logs/invoke/url post terraform apply. |
Nice! I've been playing around with this. I noticed that the |
@kapilt Thanks for the updates. I had a some comments/ideas about the updates:
In general, I think it would be nice to address the variable and |
|
3e2aadb
to
ee19516
Compare
Just replying back...
|
|
Despite the longer name, I think I'd prefer the null data source provider here to remove the ambiguity. |
61370bf
to
36ebccd
Compare
switched to null data provider, i'm still of the opinion that variables are better for this usage. the documentation on the null provider reinforces that perspective https://www.terraform.io/docs/providers/null/index.html i think there's option here for future improvements to make app and stage actual variables by unwinding all the chalice app-stage prefixing in the deployer when packaging (aka regenerating resource names), but that can be done in a backwards compatible way to this null data provider. also travis seems to be experiencing issues today, i've had numerous build failures do to network issues https://www.traviscistatus.com/incidents/hl4vqb7hvv5n |
Yep. I agree. Looking over the diff. I think the only outstanding point was removing the deployed state file generation for now. |
hmm.. sorry thought i had gotten that removed, switching laptops when force pushing is leading to some oddities where it was reintroduced. just pushed the removal of that again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Thanks again for all of the work on this!
Squashed commit of the following: commit 9ea78cba5919fab817efa22752974ab1cc70ce9c Author: Kapil Thangavelu <kapilt@gmail.com> Date: Fri Aug 2 17:14:52 2019 -0400 terraform-package rebase 1 commit a4936fc Merge: 22a3463 60d0217 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Fri Aug 2 13:28:19 2019 -0400 terraform support, merge upstream and resolve conflicts commit 22a3463 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Fri Aug 2 08:44:20 2019 -0400 terraform packaging, explicit error message on websocket support commit 50d1880 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Thu Aug 1 17:15:29 2019 -0400 terraform support, handle a tf ref for bucket notifications commit 6cf5a54 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Thu Aug 1 09:32:29 2019 -0400 terraform packaging test with private api gateway resource and min compression size commit 77d4840 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Wed Jul 31 17:54:37 2019 -0400 fix tests and lint commit b3e324b Author: Kapil Thangavelu <kapilt@gmail.com> Date: Wed Jul 31 14:44:46 2019 -0400 terraform support, address review comments commit 124f3d9 Merge: 373d2f3 bd7fa76 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Tue Jul 30 10:00:53 2019 -0400 Merge branch 'upstream' into terraform-package commit 373d2f3 Merge: 8fbd4b1 4853c6c Author: Kapil Thangavelu <kapilt@gmail.com> Date: Thu Jun 20 16:08:07 2019 -0400 Merge branch 'upstream' into terraform-package commit 8fbd4b1 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Fri Jun 7 10:52:21 2019 -0400 update sam layer gen to new generator interface commit cbc4a64 Merge: 3084852 07ca537 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Fri Jun 7 10:41:24 2019 -0400 Merge branch 'upstream' into terraform-package commit 07ca537 Merge: 4f8ddd0 31acf9e Author: Kapil Thangavelu <kapilt@gmail.com> Date: Thu Jun 6 13:01:32 2019 -0400 Merge branch 'master' of https://github.com/aws/chalice into upstream commit 3084852 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Thu Jun 6 12:53:40 2019 -0400 address review comments on docs commit 0fd9d87 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Tue Jun 4 14:19:44 2019 -0400 test default generator method commit 2dd988a Author: Kapil Thangavelu <kapilt@gmail.com> Date: Tue Jun 4 14:05:43 2019 -0400 relax terraform version requirement commit cf2a95a Author: Kapil Thangavelu <kapilt@gmail.com> Date: Mon Jun 3 19:10:35 2019 -0400 add layer support commit 4f8ddd0 Author: Kapil Thangavelu <kapilt@gmail.com> Date: Sat May 25 07:32:14 2019 -0400 cli package terraform support
Cool, I'll go ahead and squash/rebase and merge. |
8ce7575
to
53a55a8
Compare
PR: #1129 * terraform-package: Add support for terraform packaging
Issue: closes #1121
This adds in terraform support for chalice package. There's a few goals underlying.. one was to provide provisioning flexibility and extensibility instead of having to layer in multiple provisioning executions for an app (ie reference an s3 bucket, or use chalice lambda function for a step function, etc). Another was to enable chalice use more naturally in orgs that mandate/prefer terraform for infrastructure provisioning.
I've done a few manual e2e tests on a sample app (api gw, 5 lambdas, scheduled func, managed role). The end result is actually a bit faster than chalice's direct api calls for deploy/destroy.
https://gist.github.com/kapilt/1794c092cd2f17e0faadb3e00a44bc33