amplify-education / serverless-domain-manager

Serverless plugin for managing custom domains with API Gateways.
MIT License
938 stars 234 forks source link

Domain is created in the wrong region #83

Closed idmontie closed 5 years ago

idmontie commented 6 years ago

I have a serverless app that I am deploying to us-west-2. My ACM certificate is in us-east-1 like it should be.

However, when I run sls create-domain, it successfully makes the domain in us-east-1:

Serverless: 'api.example.app' was created/updated. New domains may take up to 40 minutes to be initialized.

When I run sls deploy, it fails since it cannot find the domain in us-west-2:

 Error --------------------------------------------------

  Error: Could not set up basepath mapping. Try running sls create_domain first.
Error: Error: 'api.example.app' could not be found in API Gateway.
NotFoundException: Invalid domain name identifier specified

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

Files

serverless.yml

# ...
custom:
   customDomain:
     domainName: api${self:provider.variables.apiDomainName}
     basePath: ''
     stage: ${self:provider.stage}
     createRoute53Record: true
# ...

Possible Solution?

Perhaps when the API Gateway object gets created, a region can be passed to it that is set from the custom section in the yml file?

this.apigateway = new AWS.APIGateway({ region: customRegion || 'us-east-1' });
jconstance-amplify commented 6 years ago

Hey.

I can't seem to reproduce this issue. Could you also provide a sanitized version of your provider block in your serverless.yml, and the output of aws configure list, assuming you have awscli installed?

idmontie commented 6 years ago

Ah I see now. I'm actually missing a region entry in my provider section of my yml file. I'm actually passing that in to severless through the cli:

serverless deploy --aws-profile serverless-deployer --stage $STAGE --region $AWS_REGION

Would passing them into the create_domain command work?

serverless create_domain --stage $STAGE --region $AWS_REGION
jconstance-amplify commented 6 years ago

I'm not sure off the top of my head. Wanna try it? :)

idmontie commented 6 years ago

Looks like other plugins have a similar issue (https://github.com/FidelLimited/serverless-plugin-warmup/issues/38). I'm going to add the region to my yml file.

Thanks!

donotpush commented 6 years ago

I have the same problem. any solution?

jconstance-amplify commented 6 years ago

@julianalves Is your region set in your provider block?

donotpush commented 6 years ago

yes, it is. I decide to decouple this from this my serverless project. We manage the DNS from terraform, it takes a lot of time as well to create the domain around 30/40 min. Thanks for the reply.

sohailalam2 commented 6 years ago

So I also had the same issue. After spending a lot of time digging around I got it to work. Here is the answer/solution.

  1. Certificates for CloudFront can only be in the us-east-1 region. Refer This

  2. Add region to provider as

    provider:
    name: aws
    runtime: nodejs6.10
    stage: dev
    region: ap-southeast-1
  3. Create the domain using the normal command

sls create_domain
elyobo commented 6 years ago

See also https://github.com/amplify-education/serverless-domain-manager/pull/101 - profiles specified by --aws-profile are not reliably used, only the region is, which may also cause Error: Could not set up basepath mapping problems due to the incorrect credentials being used.

ascheucher commented 6 years ago

I am also struggling with this problem. I have my certificate in us-east-1, want to host the rest in eu-central-1. I use the [default] access & secret key to be on the safe side.

stage dev to us-east-1

When creating the domain with sls create_domain --region us-east-1 --stage dev, I can deploy the function later on and it is working as expected:

Serverless: 'myservice-dev.api-example.cc' was created/updated. New domains may take up to 40 minutes to be initialized.

Try to deploy to eu-central-1 with sls deploy -v --stage dev fails as expected:

Serverless: Packaging service...
Serverless: Excluding development dependencies...

  Error --------------------------------------------------

  Error: Could not set up basepath mapping. Try running sls create_domain first.
Error: Error: 'myservice-dev.api-example.cc' could not be found in API Gateway.
NotFoundException: Invalid domain name identifier specified

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

Deploying with sls deploy -v --region us-east-1 then works as expected...

stage dev to eu-central-1

When I create the domain without region in the default region sls create_domain --stage stage, it fails with:

  Error --------------------------------------------------

  Error: 'myservice-staging.api-example.cc' was not created in API Gateway.
Error: Error: Could not find the certificate api-example.cc.

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                     darwin
     Node Version:           6.10.3
     Serverless Version:     1.26.1

This is my serverless.yml file:

service: myservice-service

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: eu-central-1

functions:
  myservice:
    handler: handler.myservice
    memorySize: 256
    reservedConcurrency: 5
    events:
      - http: PUT myservice

plugins:
  - serverless-domain-manager

custom:
  stage: ${opt:stage, self:provider.stage}
  domains:
    prod: myservice.api-example.cc
    stage: myservice-staging.api-example.cc
    dev: myservice-dev.api-example.cc

  customDomain:
    basePath: ""
    domainName: ${self:custom.domains.${self:custom.stage}}
    certificateName: api-example.cc
    stage: "${self:custom.stage}"
    createRoute53Record: true
    endpointType: regional

And this my aws-configure list output:

      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************4MOA shared-credentials-file
secret_key     ****************xNvW shared-credentials-file
    region             eu-central-1      config-file    ~/.aws/config

I can't find anything different than described above. What is the thing I miss here?

maikokuppe commented 6 years ago

In my case, I got the error because i did not have any URI routes setup in my serverless.yml:

Bad:

functions:
  get:
    handler: handler.get

Good:

functions:
  get:
    handler: handler.get
    events:
      - http:
          path: some/path
          method: get
          cors: true
JohanShogun commented 6 years ago

I had the exact same issue as ascheucher, one solution is to use the certificateArn property instead of the certificateName property, then the certificate can be in any region while you still do the api gateway calls in the correct region.

jordanboston commented 6 years ago

Using the certificateArn saved the day @JohanShogun . -Thanks!! 👊 fistbump

captainsidd commented 5 years ago

Closing due to workarounds being found.