如何为无服务器1.0.0框架定义/部署的API添加自定义域? [英] How to add a custom domain for a serverless-1.0.0 framework defined/deployed API?

查看:86
本文介绍了如何为无服务器1.0.0框架定义/部署的API添加自定义域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Serverless-1.0.0-rc.1 允许部署可通过AWS访问的API API网关.

Serverless-1.0.0-rc.1 enables to deploy an API to be accessible via a AWS API Gateway.

问题:我是否需要通过具有SSL证书的自定义域而不是随机分配的API(URL https://qwertylgbtqert.execute-api....)公开已部署的API?

The question: I need the deployed API exposed via a custom domain with SSL certificate instead of the randomly assigned one (URL https://qwertylgbtqert.execute-api....)?

这可以在serverless.yml或无服务器框架内完成吗?

Can that be done from within serverless.yml or serverless framework?

这是我的简单服务serverless.yml定义:

Here is my simple service serverless.yml definition:

service: my-service
provider:
  name: aws
  runtime: nodejs4.3

functions:
  generate:
    handler: handler.generate
    events:
      - http:
         method: get
         path: url
         cors: true

存在与此相关的问题:此处的答案,但未提供适用的答案.尤其是如何从serverless.yml本身内部分配自定义域.

There is an question which relates to this: the answer here, but does not provide an applicable answers. Especially how to assign the custom domain from within the serverless.yml itself.

它认为是否有可能创建一个AWS :: Route53资源并加以利用,但是我不知道如何在无服务器环境中做到这一点.

It thought if it is possible to create a AWS::Route53 resource and leverage that, however I don't know how to do that in serverless.

推荐答案

您需要先创建自定义域并上传证书.这不应该是此代码的一部分,也不应该是您对此软件的部署的一部分.

You need to create the custom domain first and upload the certificates. This should not be part of this code or your deployment of this piece of software.

拥有自定义域后,您需要考虑CloudFormation.

After you have a custom domain you need to think in terms of CloudFormation.

与无服务器框架中的所有其他内容一样;您可以利用CloudFormation模板来管理AWS中的资源.

Like with everything else in Serverless Framework; you can leverage CloudFormation templates to administer resources in AWS.

因此,您的问题变成了如何在CloudFormation中将API网关添加到自定义域中(提示:如果您搜索CloudFormation而不是Serverless Framework,那么Google会提供更多帮助)?通过在CloudFormation中创建AWS::ApiGateway::BasePathMapping就是答案.

So your question becomes how can you add your API Gateway to your Custom Domain in CloudFormation (hint: there is much more help on Google if you search for CloudFormation instead of Serverless Framework)? By creating a AWS::ApiGateway::BasePathMappingin CloudFormation is the answer.

这是在serverless.yml文件中的资源"中完成的.例如:

This is done in the Resources in your serverless.yml file. Like this for example:

resources:
    Resources:
        pathmapping:
            Type: AWS::ApiGateway::BasePathMapping
            Properties:
                BasePath: oauth2
                DomainName: ${self:vars.domainName}
                RestApiId: 
                    Ref: ApiGatewayRestApi
                Stage: ${self:vars.stage}

这要求您在serverless.env.yml文件中为使用的阶段提供domainNamestageName的变量.

This requires you to have variables for the domainName and stageName in the serverless.env.yml file for the stages you use.

对于rc1之前的Serverless Framework 1版本,必须将DependsOn: IamPolicyLambda添加到路径映射资源.此问题已修复: https://github.com/serverless/serverless/pull/1783

For versions of Serverless Framework 1 before rc1 you have to add DependsOn: IamPolicyLambda to the pathmapping resource. This was fixed in: https://github.com/serverless/serverless/pull/1783

在rc1之前,您应该使用RestApiApigEvent而不是ApiGatewayRestApi

Before rc1 you should use RestApiApigEvent instead of ApiGatewayRestApi

这篇关于如何为无服务器1.0.0框架定义/部署的API添加自定义域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆