diff --git a/index.ts b/index.ts index ed8f0471..3614b188 100644 --- a/index.ts +++ b/index.ts @@ -527,11 +527,51 @@ class ServerlessCustomDomain { } } + /** + * Gets value by name from cloudformation exports + */ + public async getImportValue(importValueName, nextToken = null): Promise { + const params = nextToken ? { NextToken: nextToken } : {}; + const result = await this.cloudformation.listExports(params).promise(); + const importValue = result.Exports.find((val: any) => val.Name === importValueName); + if (importValue) { + return importValue.Value; + } + + if (result.NextToken) { + return this.getImportValue(importValueName, result.NextToken); + } + + return null; + } + /** * Gets rest API id from CloudFormation stack */ public async getRestApiId(): Promise { if (this.serverless.service.provider.apiGateway && this.serverless.service.provider.apiGateway.restApiId) { + if (typeof this.serverless.service.provider.apiGateway.restApiId === "object" && + this.serverless.service.provider.apiGateway.restApiId["Fn::ImportValue"]) { + try { + const importValueName = this.serverless.service.provider.apiGateway.restApiId["Fn::ImportValue"]; + const importValue = await this.getImportValue(importValueName); + if (importValue) { + return importValue; + } + + throw new Error(`Error: CloudFormation ImportValue not found + by ${this.serverless.service.provider.apiGateway.restApiId["Fn::ImportValue"]}\n`); + } catch (err) { + this.logIfDebug(err); + throw new Error(`Error: Failed to find CloudFormation ImportValue + by ${this.serverless.service.provider.apiGateway.restApiId["Fn::ImportValue"]}\n`); + } + } + + if (typeof this.serverless.service.provider.apiGateway.restApiId === "object") { + throw new Error("Error: Unsupported restApiId object"); + } + this.serverless.cli.log(`Mapping custom domain to existing API ${this.serverless.service.provider.apiGateway.restApiId}.`); return this.serverless.service.provider.apiGateway.restApiId; diff --git a/package.json b/package.json index 4d22bb3b..5248f774 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-domain-manager", - "version": "3.2.1", + "version": "3.2.2", "engines": { "node": ">=4.0" },