diff --git a/docs/providers/aws/events/http-api.md b/docs/providers/aws/events/http-api.md index 9cdd65207f7..3cf9ee13423 100644 --- a/docs/providers/aws/events/http-api.md +++ b/docs/providers/aws/events/http-api.md @@ -271,3 +271,15 @@ provider: httpApi: payload: '1.0' ``` + +### Detailed Metrics + +With HTTP API we may configure detailed metrics that can be used setup monitoring and alerting in Cloudwatch. + +Detailed Metrics can be turned on with: + +```yaml +provider: + httpApi: + metrics: true +``` diff --git a/lib/plugins/aws/package/compile/events/httpApi/index.js b/lib/plugins/aws/package/compile/events/httpApi/index.js index f1b35f14109..453c040317b 100644 --- a/lib/plugins/aws/package/compile/events/httpApi/index.js +++ b/lib/plugins/aws/package/compile/events/httpApi/index.js @@ -130,7 +130,11 @@ class HttpApiEvents { ApiId: { Ref: this.provider.naming.getHttpApiLogicalId() }, StageName: '$default', AutoDeploy: true, + DefaultRouteSettings: { + DetailedMetricsEnabled: this.config.metrics, + }, }; + const resource = (this.cfTemplate.Resources[this.provider.naming.getHttpApiStageLogicalId()] = { Type: 'AWS::ApiGatewayV2::Stage', Properties: properties, @@ -226,7 +230,11 @@ Object.defineProperties( const routes = new Map(); const providerConfig = this.serverless.service.provider; const userConfig = providerConfig.httpApi || {}; - this.config = { routes, id: userConfig.id }; + this.config = { + routes, + id: userConfig.id, + metrics: userConfig.metrics || false, + }; let cors = null; let shouldFillCorsMethods = false; const userCors = userConfig.cors; diff --git a/lib/plugins/aws/package/compile/events/httpApi/index.test.js b/lib/plugins/aws/package/compile/events/httpApi/index.test.js index 62b7028ad82..d3deed0ec96 100644 --- a/lib/plugins/aws/package/compile/events/httpApi/index.test.js +++ b/lib/plugins/aws/package/compile/events/httpApi/index.test.js @@ -60,6 +60,10 @@ describe('HttpApiEvents', () => { expect(resource.Properties.StageName).to.equal('$default'); expect(resource.Properties.AutoDeploy).to.equal(true); }); + it('Should not have detailed metrics when not asked to', () => { + const resource = cfResources[naming.getHttpApiStageLogicalId()]; + expect(resource.Properties.DefaultRouteSettings.DetailedMetricsEnabled).to.equal(false); + }); it('Should configure output', () => { const output = cfOutputs.HttpApiUrl; expect(output).to.have.property('Value'); @@ -460,7 +464,7 @@ describe('HttpApiEvents', () => { }); }); - describe('Access logs', () => { + describe('Access logs and detailed metrics', () => { let cfResources; let naming; before(() => @@ -468,6 +472,7 @@ describe('HttpApiEvents', () => { fixture: 'httpApi', configExt: { provider: { + httpApi: { metrics: true }, logs: { httpApi: true, }, @@ -492,6 +497,11 @@ describe('HttpApiEvents', () => { expect(stageResourceProps.AccessLogSettings).to.have.property('Format'); }); + + it('Should configure detailed metrics', () => { + const resource = cfResources[naming.getHttpApiStageLogicalId()]; + expect(resource.Properties.DefaultRouteSettings.DetailedMetricsEnabled).to.equal(true); + }); }); describe('External HTTP API', () => { diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index 4a3be0e00c5..f283682f2ad 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -698,6 +698,7 @@ class AwsProvider { }, name: { type: 'string' }, payload: { type: 'string' }, + metrics: { type: 'boolean' }, }, additionalProperties: false, },