diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.js index ffa5f89dcd2..bec9aa77294 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.js @@ -20,6 +20,9 @@ module.exports = { }); } + const apiGatewayPermission = this.provider.naming.getLambdaApiGatewayPermissionLogicalId( + event.functionName + ); const template = { Type: 'AWS::ApiGateway::Method', Properties: { @@ -29,6 +32,7 @@ module.exports = { RestApiId: this.provider.getApiGatewayRestApiId(), OperationName: event.http.operationId, }, + DependsOn: [apiGatewayPermission], }; if (event.http.private) { diff --git a/test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js b/test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js index 3fcdb65f8fa..285fa7c7cb8 100644 --- a/test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js +++ b/test/unit/lib/plugins/aws/package/compile/events/apiGateway/lib/method/index.test.js @@ -1816,4 +1816,23 @@ describe('#compileMethods v2()', () => { expect(apiGatewayTokenMethodConfig.Properties.AuthorizerId).to.deep.equal('another-id'); }); }); + + it('should depends on permission resource', async () => { + const { + awsNaming, + cfTemplate: { Resources: cfResources }, + } = await runServerless({ + command: 'package', + fixture: 'apiGateway', + }); + const FooApiGatewayMethodConfig = cfResources[awsNaming.getMethodLogicalId('Foo', 'GET')]; + const OtherApiGatewayMethodConfig = + cfResources[awsNaming.getMethodLogicalId('BarMarkoVar', 'GET')]; + + const permNameFoo = awsNaming.getLambdaApiGatewayPermissionLogicalId('Foo'); + const permNameOther = awsNaming.getLambdaApiGatewayPermissionLogicalId('Other'); + + expect(FooApiGatewayMethodConfig.DependsOn).to.include(permNameFoo); + expect(OtherApiGatewayMethodConfig.DependsOn).to.include(permNameOther); + }); });