serverless-certificate-creator
Advanced tools
Comparing version 1.0.2 to 1.0.3
83
index.js
@@ -35,5 +35,8 @@ 'use strict'; | ||
this.domain = this.serverless.service.custom.customCertificate.certificateName; | ||
this.hostedZoneId = this.serverless.service.custom.customCertificate.hostedZoneId; | ||
this.hostedZoneName = this.serverless.service.custom.customCertificate.hostedZoneName; | ||
const acmCredentials = Object.assign({}, credentials, { region: this.region }); | ||
this.acm = new this.serverless.providers.aws.sdk.ACM(acmCredentials); | ||
this.idempotencyToken = this.serverless.service.custom.customCertificate.idempotencyToken; | ||
} | ||
@@ -104,2 +107,3 @@ | ||
if (existingCert) { | ||
@@ -147,3 +151,3 @@ this.serverless.cli.log(`Certificate for ${this.domain} in ${this.region} already exists. Skipping ...`); | ||
waitUntilCertificateIsValidated(certificateArn){ | ||
waitUntilCertificateIsValidated(certificateArn) { | ||
this.serverless.cli.log('waiting until certificate is validated...'); | ||
@@ -162,2 +166,24 @@ var params = { | ||
getHostedZoneId() { | ||
return this.route53.listHostedZones({}).promise().then(data => { | ||
if (this.hostedZoneId) { | ||
return this.hostedZoneId; | ||
} | ||
let hostedZone = data.HostedZones.filter(x => x.Name == this.hostedZoneName); | ||
if (hostedZone.length == 0) { | ||
throw "no hosted zone for domain found" | ||
} | ||
this.hostedZoneId = hostedZone[0].Id.replace(/\/hostedzone\//g, ''); | ||
return this.hostedZoneId; | ||
}).catch(error => { | ||
this.serverless.cli.log('certificate validation failed', error); | ||
console.log('problem', error); | ||
throw error; | ||
}); | ||
} | ||
/** | ||
@@ -168,30 +194,31 @@ * create the record set required for valdiation type dns. the certificate has the necessary information. | ||
createRecordSetForDnsValidation(certificate) { | ||
var params = { | ||
ChangeBatch: { | ||
Changes: [ | ||
{ | ||
Action: "CREATE", | ||
ResourceRecordSet: { | ||
Name: certificate.Certificate.DomainValidationOptions[0].ResourceRecord.Name, | ||
ResourceRecords: [ | ||
{ | ||
Value: certificate.Certificate.DomainValidationOptions[0].ResourceRecord.Value | ||
} | ||
], | ||
TTL: 60, | ||
Type: certificate.Certificate.DomainValidationOptions[0].ResourceRecord.Type | ||
return this.getHostedZoneId().then((hostedZoneId) => { | ||
var params = { | ||
ChangeBatch: { | ||
Changes: [ | ||
{ | ||
Action: "CREATE", | ||
ResourceRecordSet: { | ||
Name: certificate.Certificate.DomainValidationOptions[0].ResourceRecord.Name, | ||
ResourceRecords: [ | ||
{ | ||
Value: certificate.Certificate.DomainValidationOptions[0].ResourceRecord.Value | ||
} | ||
], | ||
TTL: 60, | ||
Type: certificate.Certificate.DomainValidationOptions[0].ResourceRecord.Type | ||
} | ||
} | ||
} | ||
], | ||
Comment: `DNS Validation for certificate ${certificate.Certificate.DomainValidationOptions[0].DomainName}` | ||
}, | ||
HostedZoneId: this.serverless.service.custom.customCertificate.hostedZoneId | ||
}; | ||
return this.route53.changeResourceRecordSets(params).promise().then(recordSetResult => { | ||
this.serverless.cli.log('dns validation record created - soon the certificate is functional'); | ||
}).catch(error => { | ||
this.serverless.cli.log('could not create record set for dns validation', error); | ||
console.log('problem', error); | ||
throw error; | ||
], | ||
Comment: `DNS Validation for certificate ${certificate.Certificate.DomainValidationOptions[0].DomainName}` | ||
}, | ||
HostedZoneId: hostedZoneId | ||
}; | ||
return this.route53.changeResourceRecordSets(params).promise().then(recordSetResult => { | ||
this.serverless.cli.log('dns validation record created - soon the certificate is functional'); | ||
}).catch(error => { | ||
this.serverless.cli.log('could not create record set for dns validation', error); | ||
console.log('problem', error); | ||
throw error; | ||
}); | ||
}); | ||
@@ -198,0 +225,0 @@ } |
{ | ||
"name": "serverless-certificate-creator", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "creates a certificate that can be used for custom domains for your api gateway", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,3 +7,3 @@ # serverless-certificate-creator | ||
npm i serverless-certificate-creator --save | ||
npm i serverless-certificate-creator --save-dev | ||
@@ -19,3 +19,4 @@ open serverless.yml and add the following: | ||
idempotencyToken: 'abcsomedomainio' //optional | ||
hostedZoneId: 'XXXXXXXXX' //required | ||
hostedZoneName: 'somedomain.io.' //required if hostedZoneId is not set | ||
hostedZoneId: 'XXXXXXXXX' //required if hostedZoneName is not set | ||
region: eu-west-1 // optional - default is us-east-1 which is required for custom api gateway domains of Type Edge (default) | ||
@@ -22,0 +23,0 @@ |
9868
203
25