LoginSignup
3
2

More than 3 years have passed since last update.

Serverless Frameworkのlambdaアプリにカスタムドメインを付与する

Last updated at Posted at 2020-03-21

前回作成したLaravelのサーバレスアプリにカスタムドメインでアクセスできるようにします。
ACMで証明書を取得します。Route53経由の方はDNS経由で簡単に取得できます。サブドメインはワイルドカードで申請するのみです。
Lambda関数がEdgeの場合は、us-east-1(バージニア北部)のACMである必要があります。東京のACMは関係ありません。

スクリーンショット 2020-03-21 10.54.52.png

発行済になったらserverless-domain-managerのインストールします。a4e6e25d

$ npm install serverless-domain-manager --save-dev

serverless.ymlを編集します。8c313faf

serverless.yml
+ custom:
+   customDomain:
+   domainName: laravel-demo.umihi.co
+     certificateName: umihi.co
+     basePath: ''
+     stage: ${opt:stage, self:provider.stage}
+     createRoute53Record: true
+     endpointType: 'edge'
+     securityPolicy: tls_1_2

provider:
  name: aws
  region: ap-northeast-1
  runtime: provided

plugins:
  - ./vendor/bref/bref
+   - serverless-domain-manager

最後にsls create_domainしてsls deployしますが、create_domain実行後は最大40分待つ必要があります。

$ sls create_domain
Serverless: Custom domain laravel-demo.umihi.co was created.
            New domains may take up to 40 minutes to be initialized.

$ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service laravel-demo.zip file to S3 (14.2 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: laravel-demo
stage: dev
region: ap-northeast-1
stack: laravel-demo-dev
resources: 12
api keys:
  None
endpoints:
  ANY - https://td3rzowchc.execute-api.ap-northeast-1.amazonaws.com/dev
  ANY - https://td3rzowchc.execute-api.ap-northeast-1.amazonaws.com/dev/{proxy+}
functions:
  website: laravel-demo-dev-website
layers:
  None
Serverless: Created basepath mapping.
Serverless Domain Manager Summary
Domain Name
  laravel-demo.umihi.co
Distribution Domain Name
  Target Domain: aaaaabbbbbcccc.cloudfront.net
  Hosted Zone Id: XXXXYYYYYZZZZZ
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

https://laravel-demo.umihi.co にアクセスできるようになりました。
カスタムドメイン+証明書+サーバレス+Laravelの完成です。

Cloudfrontの文字が見えますが、当該IDは、 https://console.aws.amazon.com/cloudfront
には無く、API Gateway配下の https://ap-northeast-1.console.aws.amazon.com/apigateway/main/publish/domain-names
で確認できます。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2