AWSTemplateFormatVersion: '2010-09-09' Transform: 'AWS::Serverless-2016-10-31' Description: Create API for getting meter consumption forecast. (qs-1r18anahd) Metadata: AWS::CloudFormation::Interface: ParameterGroups: - Label: default: AWS Quick Start configuration Parameters: - QSS3BucketName - QSS3KeyPrefix ParameterLabels: QSS3BucketName: default: Quick Start S3 bucket name QSS3KeyPrefix: default: Quick Start S3 key prefix Parameters: QSS3BucketName: AllowedPattern: '^[0-9a-zA-Z]+([0-9a-zA-Z-]*[0-9a-zA-Z])*$' ConstraintDescription: >- Quick Start bucket name can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-). Default: aws-quickstart Description: >- S3 bucket name for the Quick Start assets. Only change this value if you customize or extend the Quick Start for your own use. This string can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-). Type: String QSS3KeyPrefix: AllowedPattern: '^[0-9a-zA-Z-/]*[/]$' ConstraintDescription: >- Quick Start key prefix can include numbers, lowercase letters, uppercase letters, hyphens (-), and forward slash (/) and must terminate in a forward slash. Default: quickstart-aws-utility-meter-data-analytics-platform/ Type: String Description: S3 key prefix for the Quick Start assets. Quick Start key prefix can include numbers, lowercase letters, uppercase letters, hyphens (-), and forward slash (/). AthenaQueryBucket: AllowedPattern: '^[0-9a-zA-Z]+([0-9a-zA-Z-]*[0-9a-zA-Z])*$' ConstraintDescription: >- Athena Query bucket name can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-). Description: >- S3 bucket name to save Athena Query results. This string can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-). Type: String WorkingBucket: Description: >- S3 bucket name to save intermediate configuration. Type: String WithWeather: Description: >- Enables or disables the use of weather data. 0 = weather data won't be used 1 = enable weather data use Type: Number DBName: ConstraintDescription: >- Name of the glue data catalog database Default: meterdata Type: String ConfigTable: Type: String Globals: Function: Runtime: python3.7 Environment: Variables: Athena_bucket: !Ref AthenaQueryBucket With_weather_data: !Ref WithWeather Db_schema: !Ref DBName config_table: !Ref ConfigTable Conditions: UsingDefaultBucket: !Equals [!Ref QSS3BucketName, 'aws-quickstart'] Resources: # # Http API # HttpApi: Type: AWS::Serverless::HttpApi # # IAM # RunLambdaRole: Type: 'AWS::IAM::Role' Metadata: cfn-lint: config: ignore_checks: - E9101 ignore_reasons: E9101: Managed policy name Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: 'lambda.amazonaws.com' Action: 'sts:AssumeRole' ManagedPolicyArns: - !Sub 'arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' - !Sub 'arn:${AWS::Partition}:iam::aws:policy/AmazonS3FullAccess' - !Sub 'arn:${AWS::Partition}:iam::aws:policy/AmazonAthenaFullAccess' - !Sub 'arn:${AWS::Partition}:iam::aws:policy/AmazonSageMakerReadOnly' - !Sub 'arn:${AWS::Partition}:iam::aws:policy/AmazonDynamoDBReadOnlyAccess' Policies: - PolicyName: sagemaker_invoke_endpoint PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: 'sagemaker:InvokeEndpoint' Resource: !Sub 'arn:${AWS::Partition}:sagemaker:${AWS::Region}:${AWS::AccountId}:endpoint/*' AnomalyPredictionApi: Type: 'AWS::Serverless::Function' Properties: Handler: app.lambda_handler CodeUri: Bucket: !If [UsingDefaultBucket, !Sub '${QSS3BucketName}-${AWS::Region}', !Ref QSS3BucketName] Key: Fn::Sub: '${QSS3KeyPrefix}assets/functions/packages/get_anomaly.zip' Description: Lambda function to get anomaly for certain meter MemorySize: 128 Timeout: 180 Role: !GetAtt 'RunLambdaRole.Arn' Layers: - !Ref DependencyLayer Events: ApiEvent: Type: HttpApi Properties: ApiId: !Ref HttpApi Method: GET Path: /anomaly/{meter_id} OutageApi: Type: 'AWS::Serverless::Function' Properties: Handler: index.handler CodeUri: Bucket: !If [UsingDefaultBucket, !Sub '${QSS3BucketName}-${AWS::Region}', !Ref QSS3BucketName] Key: Fn::Sub: '${QSS3KeyPrefix}assets/functions/packages/outage_info.zip' Description: Lambda function to get outage for a certain timeframe MemorySize: 128 Timeout: 180 Runtime: "nodejs12.x" Role: !GetAtt 'RunLambdaRole.Arn' Events: ApiEvent: Type: HttpApi Properties: ApiId: !Ref HttpApi Method: GET Path: /outage MeterForecastPredictionApi: Type: 'AWS::Serverless::Function' Properties: Handler: app.lambda_handler CodeUri: Bucket: !If [UsingDefaultBucket, !Sub '${QSS3BucketName}-${AWS::Region}', !Ref QSS3BucketName] Key: Fn::Sub: '${QSS3KeyPrefix}assets/functions/packages/meter_forecast.zip' Description: Lambda function to get meter consumption forecast MemorySize: 128 Timeout: 180 Role: !GetAtt 'RunLambdaRole.Arn' Layers: - !Ref DependencyLayer Events: ApiEvent: Type: HttpApi Properties: ApiId: !Ref HttpApi Method: GET Path: /forecast/{meter_id} Environment: Variables: WORKING_BUCKET: !Ref WorkingBucket DependencyLayer: Type: 'AWS::Serverless::LayerVersion' Properties: LayerName: !Sub "api-dependencies-${AWS::Region}" Description: Dependencies for sam app ContentUri: Bucket: !If [UsingDefaultBucket, !Sub '${QSS3BucketName}-${AWS::Region}', !Ref QSS3BucketName] Key: Fn::Sub: '${QSS3KeyPrefix}assets/functions/packages/dependencies/common/common-layer.zip' CompatibleRuntimes: - python3.7 LicenseInfo: 'Available under the MIT-0 license.' Outputs: MeterForecastPredictionApi: Description: Prediction function. Value: !Ref MeterForecastPredictionApi AnomalyPredictionApi: Description: Anomaly API. Value: !Ref AnomalyPredictionApi