LoginSignup
8
6

More than 5 years have passed since last update.

Serverless Frameworkで独自ドメインを設定する

Posted at

定番ですが、自分用に改めて覚書。

serverless-domain-managerプラグイン

インストール

$ sls create -t aws-nodejs -p domain-test
$ cd domain-test
$ yarn add -D serverless-domain-manager

serverless.yml

service:
  name: domain-test

plugins:
  - serverless-domain-manager

provider:
  name: aws
  runtime: nodejs8.10
  stage: development
  logRetentionInDays: 14

custom:
  stage: ${opt:stage, self:provider.stage}
  prune:
    automatic: true
    number: 3
  endpoint:
    production: "api-test.example.com"
    development: "devapi-test.example.com"
    default: "devapi-test.example.com"
  domainDeploy:
    production: true
    development: true
    default: false
  customDomain:
    domainName: ${self:custom.endpoint.${self:custom.stage}, self:custom.endpoint.default}
    stage: ${self:custom.stage}
    basePath: ''
    certificateArn: 'arn:aws:acm:us-east-1:xxxxxxx:certificate/XXXXXXX'
    createRoute53Record: true
    enabled: ${self:custom.domainDeploy.${self:custom.stage}, self:custom.domainDeploy.default}

ドメイン作成

# developmentドメイン
$ sls create_domain

# productionドメイン
$ sls create_domain --stage production

「つながるまで40分程度かかるかも」ということなので、これだけ先行してやっておくのが良さそう。

デプロイ

$ sls deploy

# productionドメイン
$ sls deploy --stage production

削除

$ sls remove
$ sls delete_domain


$ sls remove --stage production
$ sls delete_domain --stage production

IAM

AWS CLIでadmin権限つけてる環境とかなら特に意識する必要はない。
ただ、CIとか通すなら以下の権限をつけよう。

acm:ListCertificates                *
apigateway:GET                      /domainnames/*
apigateway:DELETE                   /domainnames/*
apigateway:POST                     /domainnames
apigateway:POST                     /domainnames/*/basepathmappings
cloudfront:UpdateDistribution       *
route53:ListHostedZones             *
route53:ChangeResourceRecordSets    hostedzone/{HostedZoneId}
route53:GetHostedZone               *
route53:ListResourceRecordSets      *
iam:CreateServiceLinkedRole         arn:aws:iam::${AWS::AccountId}: role/aws-service-role/ops.apigateway.amazonaws.com/AWSServiceRoleForAPIGateway
8
6
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
8
6