UNPKG

@kumologica/builder

Version:

Kumologica build and deploy module

125 lines (111 loc) 3.52 kB
// api gw triggers function isNewApi(params) { return params && params.apiId && params.apiId === "create new"; } function getApiId(params) { return isNewApi(params)? {Ref: "ApiGW"}: params.apiId; } function getParentId(params) { return isNewApi(params)? {"Fn::GetAtt": "ApiGW.RootResourceId"}: params.parentId; } function trigger(params) { let templateResources = {}; if (isNewApi(params)) { templateResources.ApiGW = { Type: "AWS::ApiGateway::RestApi", Properties: { Name: params.apiName || "Rest Api Gateway", ApiKeySourceType: "HEADER", EndpointConfiguration: { Types: ["REGIONAL"] } } } } // Note ${Lambda.Arn} - the flow lambda must have resource defined as Lambda templateResources.ApiGWPermission = { Type: "AWS::Lambda::Permission", Properties: { FunctionName: { "Fn::GetAtt": [ "Lambda", "Arn" ] }, Action: "lambda:InvokeFunction", Principal: 'apigateway.amazonaws.com', SourceArn: { "Fn::Sub": "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:" + (isNewApi(params)? "${ApiGW}": params.apiId) + "/*/*/*" } } }; if (params.resource) { templateResources.ServiceResource = { Type: "AWS::ApiGateway::Resource", Properties: { RestApiId: getApiId(params), ParentId: getParentId(params), PathPart: params.resource } }; } // Note ${Lambda.Arn} - the flow lambda must have resource defined as Lambda templateResources.ServiceResourceANY = { Type: "AWS::ApiGateway::Method", Properties: { RestApiId: getApiId(params), ResourceId: params.resource? {"Ref": "ServiceResource"}: getParentId(params), HttpMethod: "ANY", AuthorizationType: "NONE", ApiKeyRequired: params.apiKeyRequired, AuthorizationScopes: params.scopes, Integration: { Type: "AWS_PROXY", IntegrationHttpMethod: "POST", Uri: { "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Lambda.Arn}/invocations" } } } } templateResources.ProxyResource = { Type: "AWS::ApiGateway::Resource", Properties: { RestApiId: getApiId(params), ParentId: params.resource? {"Ref": "ServiceResource"}: getParentId(params), PathPart: "{proxy+}" } }; // Note ${Lambda.Arn} - the flow lambda must have resource defined as Lambda templateResources.ProxyResourceANY = { Type: "AWS::ApiGateway::Method", Properties: { RestApiId: getApiId(params), ResourceId: {Ref: "ProxyResource"}, HttpMethod: "ANY", AuthorizationType: "NONE", ApiKeyRequired: params.apiKeyRequired, AuthorizationScopes: params.scopes, Integration: { Type: "AWS_PROXY", IntegrationHttpMethod: "POST", Uri: { "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Lambda.Arn}/invocations" } } } } if (params.authorizerId) { templateResources.ProxyResourceANY.Properties.AuthorizationType = params.authorizerType || 'COGNITO_USER_POOLS'; templateResources.ProxyResourceANY.Properties.AuthorizerId = params.authorizerId; templateResources.ServiceResourceANY.Properties.AuthorizationType = params.authorizerType || 'COGNITO_USER_POOLS'; templateResources.ServiceResourceANY.Properties.AuthorizerId = params.authorizerId; } templateResources.Deployment = { Type: "AWS::ApiGateway::Deployment", DependsOn: ["ProxyResourceANY", "ServiceResourceANY"], Properties: { RestApiId: getApiId(params), StageName: params.stage || 'test' } } return templateResources; } module.exports = { trigger }