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

provider/aws: API Gateway custom domains and base path mappings #8353

Merged
merged 3 commits into from
Aug 22, 2016

Conversation

apparentlymart
Copy link
Contributor

@apparentlymart apparentlymart commented Aug 21, 2016

This is a continuation of what @jarrodjackson started in #7708. It begins with his commit from that PR to add aws_api_gateway_domain_name, and then builds on it with the aws_api_gateway_base_path_mapping implementation from #6175 and documentation for both.

This pair of resources allow an API to be presented at a given path prefix on a custom hostname. The separation between domain name and base path mapping means that multiple APIs or API stages from a single AWS account can be presented on a single domain name.

Once complete, this should fix #7421.

@radeksimko
Copy link
Member

FYI: #6175 contains both resources. That said nobody had time to review and/or merge that PR yet and it would need to be rebased 1st either way.

The domain resource in this PR looks like it's ready for review, so to prevent another stale WIP PR, would you mind keeping this PR focused on aws_api_gateway_domain_name only and then open another one for aws_api_gateway_base_path_mapping?

@apparentlymart apparentlymart force-pushed the f-aws-api-gateway-custom-domain branch from abcd233 to 091da7f Compare August 21, 2016 20:45
@apparentlymart
Copy link
Contributor Author

@radeksimko The domain name resource is pointless without the base path mapping, so my intent was to finish them both up here today (based on the work done by Jarrod and Ryan) and hopefully get these things finally merged.

I've just finished rebasing the base path mapping part of Ryan's mega-PR (and adapting it a little so it can work without the other resources he implemented) and I'm just wrapping up the docs now, so I should be able to get this out of WIP today.

@apparentlymart apparentlymart force-pushed the f-aws-api-gateway-custom-domain branch 2 times, most recently from e9d31dd to 332bfaf Compare August 21, 2016 21:19
@apparentlymart apparentlymart changed the title [WIP] provider/aws: API Gateway custom domains and base path mappings provider/aws: API Gateway custom domains and base path mappings Aug 21, 2016
@apparentlymart
Copy link
Contributor Author

@radeksimko I think this is ready for a review pass now. 😀

if err != nil {
if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" {
d.SetId("")
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be IMO helpful to have a WARN log message here (like we have in some other resources), so that if we ever end up debugging missing resources we'd be able to track it down.

@radeksimko
Copy link
Member

@apparentlymart I left you some comments there, but vast majority of them are nitpicks. The only real biggie is one failing acceptance test due to uniqueness of domain names within AWS.

Otherwise this is looking pretty good.

certificate_chain = "%v"
certificate_name = "tf-acc-apigateway-domain-name"
certificate_private_key = "%v"
domain_name = "test-api-gateway-domain-cert0.com"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this should be randomised as well.

@radeksimko radeksimko added the waiting-response An issue/pull request is waiting for a response from the community label Aug 22, 2016
@apparentlymart
Copy link
Contributor Author

Thanks for the feedback, @radeksimko. Agreed that randomizing that domain name seems unavoidable since it needs to be unique across the whole of AWS... will need to do some more work here to generate certs on the fly, and hope that API Gateway isn't also checking that the certificate chain leads back to some "real" CA that it trusts.... 😟

@apparentlymart
Copy link
Contributor Author

Chatted a bit with @radeksimko elsewhere. He suggested that we should just replace the static, self-signed cert here with a wildcard one and then randomly generate a subdomain, which makes a lot of sense and should be easier than what I suggested.

@jarrodjackson pointed out that he made these certs using a Terraform config, which should be easy enough to adapt to make a wildcard cert: https://github.com/jarrodjackson/cert-generator

@radeksimko radeksimko removed the waiting-response An issue/pull request is waiting for a response from the community label Aug 22, 2016
@apparentlymart apparentlymart force-pushed the f-aws-api-gateway-custom-domain branch from 332bfaf to 6aacd66 Compare August 22, 2016 16:54
@apparentlymart
Copy link
Contributor Author

Using a wildcard cert seems to have worked to address the hostname conflict problem. I'll fix up the rest of this in a little while.

API Gateway allows users to "claim" a domain name for use as a custom
hostname for deployed API endpoints. The domain name resource just claims
the domain name; a user would then use a "base path mapping" resource
(to be implemented in a later commit) to map a particular API to a
particular path prefix on that domain.

The acceptance tests contain some TLS certificates that expire in 2026;
we'll need to generate some more certificates before we get there.
@apparentlymart apparentlymart force-pushed the f-aws-api-gateway-custom-domain branch from 6aacd66 to 72dde55 Compare August 22, 2016 18:45
@apparentlymart
Copy link
Contributor Author

@radeksimko I've addressed the remaining comments now, except the one about doing updates on the base path mapping resource. I agree we should do this, but I also agree that it'd be good to address that in a separate PR since I think this approach will work for 90% of situations and make API Gateway setup with Terraform more complete.

(The main situation I see where it could potentially cause problems is if I want to change the stage of a pre-existing API. As implemented here Terraform will delete the old mapping and then create a replacement, which is likely to cause a brief outage where no mapping is present. Doing an update in-place would presumably avoid that. But I don't expect people will be frequently changing the stage of a deployed API.)

})

id := fmt.Sprintf("%s/%s", d.Get("domain_name").(string), d.Get("base_path").(string))
d.SetId(id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ID should be set only if there are no errors - i.e. below the condition, not above. Theoretically Read() will wipe the resource during next plan, but it would be cleaner if it didn't ever end up in the state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, of course. 🌴

@radeksimko
Copy link
Member

Besides one last nitpick this LGTM.

ryansroberts and others added 2 commits August 22, 2016 15:36
API Gateway allows users to "claim" a domain name for use as a custom
hostname for deployed API endpoints, and then use this base path mapping
resource to expose a particular API deployment at a path on such a domain.

The acceptance tests use certificates from the aws_api_gateway_domain_name
tests which expire in 2026; we'll need to generate some more certificates
before we get there.
@apparentlymart apparentlymart force-pushed the f-aws-api-gateway-custom-domain branch from 72dde55 to c4255f1 Compare August 22, 2016 22:37
@apparentlymart
Copy link
Contributor Author

Thanks, @radeksimko. I addressed that last comment so I'm going to merge once the tests have passed.

@apparentlymart
Copy link
Contributor Author

Thanks to @jarrodjackson and @ryansroberts for doing all the work here! 😀

@radeksimko radeksimko deleted the f-aws-api-gateway-custom-domain branch August 23, 2016 06:04
@OliverCole
Copy link

While I wait for this to land in a relase - one question for @apparentlymart - you were talking about generating a certificate on the fly? What if we have a fully valid one? Will we be able to use that?

@apparentlymart
Copy link
Contributor Author

@OliverCole that issue with certs was specific to Terraform's acceptance tests. For real-world use, you can use any certificate you have as long as Terraform has access the PEM-encoded cert, cert chain and private key.

API Gateway is unfortunately divergent from the "usual" AWS approach: normally we can register a certificate separately in IAM/ACM and then reference it from other services, but for API Gateway it's necessary to upload the cert to the API Gateway API and then it will create a cert in IAM on your behalf. We can't do anything about this different workflow, but the Terraform resources will give you the same capabilities as you get when registering domains and path mappings via the UI.

@OliverCole
Copy link

@apparentlymart Great, thanks for clarifying, and thanks for working on it!

@ghost
Copy link

ghost commented Apr 22, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AWS provider: API gateway custom domain names
5 participants