From b16fc8baa7fa24a15cfa5234fbc2928128e516ac Mon Sep 17 00:00:00 2001 From: pvighi Date: Thu, 2 Aug 2018 11:38:56 +0100 Subject: [PATCH 1/2] scaffolding for product catalog [WIP] --- handlers/new-product-api/cfn.yaml | 92 ++++++++++++++++++- handlers/new-product-api/riff-raff.yaml | 1 + .../api/productcatalog/Handler.scala | 25 +++++ 3 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala diff --git a/handlers/new-product-api/cfn.yaml b/handlers/new-product-api/cfn.yaml index bb814b110f..2e9cc1fb29 100644 --- a/handlers/new-product-api/cfn.yaml +++ b/handlers/new-product-api/cfn.yaml @@ -64,7 +64,36 @@ Resources: - sqs:GetQueueUrl - sqs:SendMessage Resource: !Sub "arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:contributions-thanks" - + ProductCatalogRole: + Type: AWS::IAM::Role + Properties: + AssumeRolePolicyDocument: + Statement: + - Effect: Allow + Principal: + Service: + - lambda.amazonaws.com + Action: + - sts:AssumeRole + Path: / + Policies: + - PolicyName: LambdaPolicy + PolicyDocument: + Statement: + - Effect: Allow + Action: + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + - lambda:InvokeFunction + Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/product-catalog-${Stage}:log-stream:*" + - PolicyName: ReadPrivateCredentials + PolicyDocument: + Statement: + - Effect: Allow + Action: s3:GetObject + Resource: + - !Sub arn:aws:s3:::gu-reader-revenue-private/membership/support-service-lambdas/${Stage}/trustedApi-${Stage}.*.json AddSubscriptionLambda: Type: AWS::Lambda::Function Properties: @@ -120,7 +149,60 @@ Resources: - NewProductApi - AddSubscriptionLambda - AddSubscriptionResource + ProductCatalogLambda: + Type: AWS::Lambda::Function + Properties: + Description: get description of available products + FunctionName: + !Sub product-catalog-${Stage} + Code: + S3Bucket: zuora-auto-cancel-dist + S3Key: !Sub membership/${Stage}/new-product-api/new-product-api.jar + Handler: com.gu.newproduct.api.productcatalog.Handler::apply + Environment: + Variables: + Stage: !Ref Stage + Role: + Fn::GetAtt: + - "ProductCatalogRole" + - Arn + MemorySize: 1536 + Runtime: java8 + Timeout: 300 + DependsOn: + - "ProductCatalogRole" + ProductCatalogApiPermission: + Type: AWS::Lambda::Permission + Properties: + Action: lambda:invokeFunction + FunctionName: !Sub product-catalog-${Stage} + Principal: apigateway.amazonaws.com + DependsOn: ProductCatalogLambda + + ProductCatalogResource: + Type: AWS::ApiGateway::Resource + Properties: + RestApiId: !Ref NewProductApi + ParentId: !GetAtt [NewProductApi, RootResourceId] + PathPart: product-catalog + DependsOn: NewProductApi + ProductCatalogMethod: + Type: AWS::ApiGateway::Method + Properties: + AuthorizationType: NONE + ApiKeyRequired: true + RestApiId: !Ref NewProductApi + ResourceId: !Ref ProductCatalogResource + HttpMethod: POST + Integration: + Type: AWS_PROXY + IntegrationHttpMethod: POST + Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProductCatalogLambda.Arn}/invocations + DependsOn: + - NewProductApi + - ProductCatalogLambda + - ProductCatalogResource NewProductApi: Type: "AWS::ApiGateway::RestApi" Properties: @@ -134,14 +216,18 @@ Resources: RestApiId: !Ref NewProductApi DeploymentId: !Ref NewProductApiDeployment StageName: !Sub ${Stage} - DependsOn: AddSubscriptionMethod + DependsOn: + - AddSubscriptionMethod + - ProductCatalogMethod NewProductApiDeployment: Type: AWS::ApiGateway::Deployment Properties: Description: Deploys new-product-api into an environment/stage RestApiId: !Ref NewProductApi - DependsOn: AddSubscriptionMethod + DependsOn: + - AddSubscriptionMethod + - ProductCatalogMethod NewProductApiKey: Type: AWS::ApiGateway::ApiKey diff --git a/handlers/new-product-api/riff-raff.yaml b/handlers/new-product-api/riff-raff.yaml index 5871e1b5d2..3272aed904 100644 --- a/handlers/new-product-api/riff-raff.yaml +++ b/handlers/new-product-api/riff-raff.yaml @@ -18,4 +18,5 @@ deployments: prefixStack: false functionNames: - add-subscription- + - product-catalog- dependencies: [cfn] diff --git a/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala b/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala new file mode 100644 index 0000000000..15e30fe1f3 --- /dev/null +++ b/handlers/new-product-api/src/main/scala/com/gu/newproduct/api/productcatalog/Handler.scala @@ -0,0 +1,25 @@ +package com.gu.newproduct.api.productcatalog + +import java.io.{InputStream, OutputStream} + +import com.amazonaws.services.lambda.runtime.Context +import com.gu.util.Logging +import com.gu.util.apigateway.ApiGatewayHandler.{LambdaIO, Operation} +import com.gu.util.apigateway.{ApiGatewayHandler, ApiGatewayRequest, ApiGatewayResponse} +import com.gu.util.reader.Types.ApiGatewayOp.ContinueProcessing + +object Handler extends Logging { + + // Referenced in Cloudformation + def apply(inputStream: InputStream, outputStream: OutputStream, context: Context): Unit = + ApiGatewayHandler(LambdaIO(inputStream, outputStream, context)) { + ContinueProcessing( + Operation.noHealthcheck { + Req: ApiGatewayRequest => ApiGatewayResponse.successfulExecution + } + ) + } +} + + + From 6e946cfb29658afd8a187dc7c8b8b10e41b1d329 Mon Sep 17 00:00:00 2001 From: pvighi Date: Thu, 2 Aug 2018 12:04:32 +0100 Subject: [PATCH 2/2] fixed cloudformation --- handlers/new-product-api/cfn.yaml | 36 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/handlers/new-product-api/cfn.yaml b/handlers/new-product-api/cfn.yaml index 2e9cc1fb29..dff559c927 100644 --- a/handlers/new-product-api/cfn.yaml +++ b/handlers/new-product-api/cfn.yaml @@ -171,7 +171,7 @@ Resources: Timeout: 300 DependsOn: - "ProductCatalogRole" - ProductCatalogApiPermission: + ProductCatalogApiPermission: Type: AWS::Lambda::Permission Properties: Action: lambda:invokeFunction @@ -179,7 +179,7 @@ Resources: Principal: apigateway.amazonaws.com DependsOn: ProductCatalogLambda - ProductCatalogResource: + ProductCatalogResource: Type: AWS::ApiGateway::Resource Properties: RestApiId: !Ref NewProductApi @@ -187,22 +187,22 @@ Resources: PathPart: product-catalog DependsOn: NewProductApi - ProductCatalogMethod: - Type: AWS::ApiGateway::Method - Properties: - AuthorizationType: NONE - ApiKeyRequired: true - RestApiId: !Ref NewProductApi - ResourceId: !Ref ProductCatalogResource - HttpMethod: POST - Integration: - Type: AWS_PROXY - IntegrationHttpMethod: POST - Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProductCatalogLambda.Arn}/invocations - DependsOn: - - NewProductApi - - ProductCatalogLambda - - ProductCatalogResource + ProductCatalogMethod: + Type: AWS::ApiGateway::Method + Properties: + AuthorizationType: NONE + ApiKeyRequired: true + RestApiId: !Ref NewProductApi + ResourceId: !Ref ProductCatalogResource + HttpMethod: GET + Integration: + Type: AWS_PROXY + IntegrationHttpMethod: POST + Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProductCatalogLambda.Arn}/invocations + DependsOn: + - NewProductApi + - ProductCatalogLambda + - ProductCatalogResource NewProductApi: Type: "AWS::ApiGateway::RestApi" Properties: