AWSTemplateFormatVersion: "2010-09-09" Transform: 'AWS::Serverless-2016-10-31' Parameters: Environment: Type: String Default: dev Description: Environment name LogLevel: Type: String Default: INFO RetentionInDays: Type: Number Default: 30 Description: CloudWatch Logs retention period for Lambda functions EventBusArn: Type: AWS::SSM::Parameter::Value Description: EventBridge Event Bus ARN EventBusName: Type: AWS::SSM::Parameter::Value Description: EventBridge Event Bus Name Globals: Function: Runtime: python3.9 Architectures: - arm64 Handler: main.handler Timeout: 30 Tracing: Active Environment: Variables: ENVIRONMENT: !Ref Environment EVENT_BUS_NAME: !Ref EventBusName TABLE_NAME: !Ref Table POWERTOOLS_SERVICE_NAME: products POWERTOOLS_TRACE_DISABLED: "false" LOG_LEVEL: !Ref LogLevel Layers: - !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension-Arm64:1" Resources: ######### # TABLE # ######### Table: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: productId AttributeType: S - AttributeName: category AttributeType: S BillingMode: PAY_PER_REQUEST KeySchema: - AttributeName: productId KeyType: HASH GlobalSecondaryIndexes: - IndexName: category KeySchema: - AttributeName: category KeyType: HASH - AttributeName: productId KeyType: RANGE Projection: ProjectionType: ALL StreamSpecification: StreamViewType: NEW_AND_OLD_IMAGES TableParameter: Type: AWS::SSM::Parameter Properties: Name: !Sub /ecommerce/${Environment}/products/table/name Type: String Value: !Ref Table ############# # FUNCTIONS # ############# ValidateFunction: Type: AWS::Serverless::Function Properties: CodeUri: src/validate/ Events: BackendApi: Type: Api Properties: Path: /backend/validate Method: POST RestApiId: !Ref Api Policies: - arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy - DynamoDBReadPolicy: TableName: !Ref Table ValidateLogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub "/aws/lambda/${ValidateFunction}" RetentionInDays: !Ref RetentionInDays TableUpdateFunction: Type: AWS::Serverless::Function Properties: Handler: main.handler CodeUri: src/table_update/ Events: DynamoDB: Type: DynamoDB Properties: Stream: !GetAtt Table.StreamArn StartingPosition: TRIM_HORIZON DestinationConfig: OnFailure: Destination: !GetAtt DeadLetterQueue.Outputs.QueueArn Policies: - arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy - Version: "2012-10-17" Statement: - Effect: Allow Action: - events:PutEvents Resource: !Ref EventBusArn Condition: StringEquals: events:source: "ecommerce.products" - Effect: Allow Action: - sqs:SendMessage Resource: !GetAtt DeadLetterQueue.Outputs.QueueArn TableUpdateLogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub "/aws/lambda/${TableUpdateFunction}" RetentionInDays: !Ref RetentionInDays ############### # API GATEWAY # ############### Api: Type: AWS::Serverless::Api Properties: DefinitionBody: Fn::Transform: Name: "AWS::Include" Parameters: Location: "resources/openapi.yaml" EndpointConfiguration: REGIONAL StageName: prod TracingEnabled: true ApiArnParameter: Type: AWS::SSM::Parameter Properties: Name: !Sub /ecommerce/${Environment}/products/api/arn Type: String Value: !Sub "arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${Api}/prod" ApiUrlParameter: Type: AWS::SSM::Parameter Properties: Name: !Sub /ecommerce/${Environment}/products/api/url Type: String Value: !Sub "https://${Api}.execute-api.${AWS::Region}.amazonaws.com/prod" ##################### # DEAD LETTER QUEUE # ##################### DeadLetterQueue: Type: AWS::CloudFormation::Stack Properties: # The path starts with '../..' as this will be evaluated from the # products/build folder, not the products folder. TemplateURL: ../../shared/templates/dlq.yaml