Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch exports from cloudformation if used Fn::ImportValue #220

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions index.ts
Expand Up @@ -527,11 +527,51 @@ class ServerlessCustomDomain {
}
}

/**
* Gets value by name from cloudformation exports
*/
public async getImportValue(importValueName, nextToken = null): Promise<string> {
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<string> {
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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "serverless-domain-manager",
"version": "3.2.1",
"version": "3.2.2",
"engines": {
"node": ">=4.0"
},
Expand Down