diff --git a/lib/plugins/aws/package/compile/events/api-gateway/lib/rest-api.js b/lib/plugins/aws/package/compile/events/api-gateway/lib/rest-api.js index 4243969012..894fca97ed 100644 --- a/lib/plugins/aws/package/compile/events/api-gateway/lib/rest-api.js +++ b/lib/plugins/aws/package/compile/events/api-gateway/lib/rest-api.js @@ -47,15 +47,27 @@ module.exports = { const DisableExecuteApiEndpoint = apiGateway.disableDefaultEndpoint == null ? undefined : apiGateway.disableDefaultEndpoint; + const properties = { + Name: this.provider.naming.getApiGatewayName(), + BinaryMediaTypes, + DisableExecuteApiEndpoint, + EndpointConfiguration, + }; + + // Tags + if (this.serverless.service.provider.tags) { + properties.Tags = Object.entries(this.serverless.service.provider.tags).map( + ([Key, Value]) => ({ + Key, + Value, + }) + ); + } + _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { [this.apiGatewayRestApiLogicalId]: { Type: 'AWS::ApiGateway::RestApi', - Properties: { - Name: this.provider.naming.getApiGatewayName(), - BinaryMediaTypes, - DisableExecuteApiEndpoint, - EndpointConfiguration, - }, + Properties: properties, }, }); diff --git a/test/unit/lib/plugins/aws/package/compile/events/api-gateway/lib/rest-api.test.js b/test/unit/lib/plugins/aws/package/compile/events/api-gateway/lib/rest-api.test.js index ffd6ee5390..c8b0fe4184 100644 --- a/test/unit/lib/plugins/aws/package/compile/events/api-gateway/lib/rest-api.test.js +++ b/test/unit/lib/plugins/aws/package/compile/events/api-gateway/lib/rest-api.test.js @@ -56,6 +56,34 @@ describe('#compileRestApi()', () => { }); }); + it('should create a REST API resource with tags', () => { + awsCompileApigEvents.serverless.service.provider.tags = { + tagKey1: 'tagValue1', + tagKey2: 'tagValue2', + }; + + awsCompileApigEvents.compileRestApi(); + const resources = + awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources; + + expect(resources.ApiGatewayRestApi).to.deep.equal({ + Type: 'AWS::ApiGateway::RestApi', + Properties: { + BinaryMediaTypes: undefined, + DisableExecuteApiEndpoint: undefined, + Name: 'dev-new-service', + EndpointConfiguration: { + Types: ['EDGE'], + }, + Policy: '', + Tags: [ + { Key: 'tagKey1', Value: 'tagValue1' }, + { Key: 'tagKey2', Value: 'tagValue2' }, + ], + }, + }); + }); + it('should create a REST API resource with resource policy', () => { awsCompileApigEvents.serverless.service.provider.apiGateway = { resourcePolicy: [