<commit_before>---
AWSTemplateFormatVersion: "2010-09-09"

Parameters:
  LambdaS3Bucket:
    Type: "String"
    Description: "The S3 bucket where the lambda zip file is stored."

  LambdaS3Key:
    Type: "String"
    Description: "The S3 key of the lambda zip file."

  LambdaTimeout:
    Type: "Number"
    Description: "The timeout in seconds for the lambda function."
    Default: 10

  LambdaMemorySize:
    Type: "Number"
    Description: "The memory allocation for the lambda function in megabytes."
    Default: 128

  APIName:
    Type: "String"
    Description: "The name of the API."
    Default: "API"

  APIDescription:
    Type: "String"
    Description: "A description of the API."
    Default: ""

  {% for stage in stages %}
  {% if stage != devstage %}
  {{stage|title}}Version:
    Type: "String"
    Description: "The lambda version number associated with the {{stage}} stage."
    Default: "$LATEST"

  {% endif %}
  {% endfor %}
Resources:
  FunctionExecutionRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service:
                - "lambda.amazonaws.com"
            Action: "sts:AssumeRole"
      ManagedPolicyArns:
        - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
      {% if dynamodb_tables %}
      Policies:
        - PolicyName: "DynamoDBPolicy"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action:
                  - "dynamodb:*"
                Resource:
                  - "*"
      {% endif %}

  Function:
    Type: "AWS::Lambda::Function"
    DependsOn:
      - "FunctionExecutionRole"
    Properties:
      Code:
        S3Bucket: !Ref LambdaS3Bucket
        S3Key: !Ref LambdaS3Key
      Role: !GetAtt ["FunctionExecutionRole", "Arn"]
      Timeout: !Ref LambdaTimeout
      Handler: "handler.lambda_handler"
      Runtime: "python2.7"
      MemorySize: !Ref LambdaMemorySize

  API:
    Type: "AWS::ApiGateway::RestApi"
    Properties:
      Body:
        swagger: "2.0"
        info:
          title: !Ref APIName
          description: !Ref APIDescription
        schemes:
          - "https"
        paths:
          /:
            x-amazon-apigateway-any-method:
              responses: {}
              x-amazon-apigateway-integration:
                responses:
                  default:
                    statusCode: "200"
                uri: !Join ["", ["arn:aws:apigateway:", !Ref "AWS::Region", ":lambda:path/2015-03-31/functions/", !GetAtt ["Function", "Arn"], ":${stageVariables.STAGE}", "/invocations"]]
                passthroughBehavior: "when_no_match"
                httpMethod: "POST"
                type: "aws_proxy"
          /{proxy+}:
            x-amazon-apigateway-any-method:
              parameters:
                - name: "proxy"
                  in: "path"
                  required: true
                  type: "string"
              responses: {}
              x-amazon-apigateway-integration:
                responses:
                  default:
                    statusCode: "200"
                uri: !Join ["", ["arn:aws:apigateway:", !Ref "AWS::Region", ":lambda:path/2015-03-31/functions/", !GetAtt ["Function", "Arn"], ":${stageVariables.STAGE}", "/invocations"]]
                passthroughBehavior: "when_no_match"
                httpMethod: "POST"
                type: "aws_proxy"

  {% for stage in stages %}
  {{stage|title}}FunctionAlias:
    Type: "AWS::Lambda::Alias"
    Properties:
      Name: {{stage}}
      FunctionName: !Ref Function
      {% if stage != devstage %}
      FunctionVersion: !Ref {{stage|title}}Version
      {% else %}
      FunctionVersion: "$LATEST"
      {% endif %}

  {{stage|title}}APIDeployment:
    Type: "AWS::ApiGateway::Deployment"
    Properties:
      RestApiId: !Ref API
      StageName: {{stage}}
      StageDescription:
        Variables:
          STAGE: "{{stage}}"
          {% for v in vars[stage] %}
          {{v}}: "{{vars[stage][v]}}"
          {% endfor %}

  {{stage|title}}APILambdaPermission:
    Type: "AWS::Lambda::Permission"
    DependsOn:
      - "{{stage|title}}FunctionAlias"
    Properties:
      Action: "lambda:InvokeFunction"
      FunctionName: !Ref {{stage|title}}FunctionAlias
      Principal: "apigateway.amazonaws.com"
      SourceArn: !Join ["", ["arn:aws:execute-api:", !Ref "AWS::Region", ":", !Ref "AWS::AccountId", ":", !Ref "API", "/*/*/*"]]

  {% for name, table in dynamodb_tables.items() %}
  {{stage|title}}DynamoDBTable{{loop.index}}:
    Type: "AWS::DynamoDB::Table"
    Properties:
      TableName: "{{stage}}.{{name}}"
      AttributeDefinitions:
        {% for attr, type in table.attributes.items() %}
        - AttributeName: "{{attr}}"
          AttributeType: "{{type}}"
        {% endfor %}
      KeySchema:
        {% if table.key is iterable and table.key is not string %}
        {% for key in table.key %}
        - AttributeName: "{{key}}"
          KeyType: "{% if loop.index == 1 %}HASH{% else %}RANGE{% endif %}"
        {% endfor %}
        {% else %}
        - AttributeName: "{{table.key}}"
          KeyType: "HASH"
        {% endif %}
      ProvisionedThroughput:
        {% if table.provisioned_throughput %}
        ReadCapacityUnits: {{table.provisioned_throughput[0]}}
        WriteCapacityUnits: {{table.provisioned_throughput[1]}}
        {% else %}
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
        {% endif %}<commit_msg>Stricter permissions for lambda access to dynamodb databases<commit_after>---
AWSTemplateFormatVersion: "2010-09-09"

Parameters:
  LambdaS3Bucket:
    Type: "String"
    Description: "The S3 bucket where the lambda zip file is stored."

  LambdaS3Key:
    Type: "String"
    Description: "The S3 key of the lambda zip file."

  LambdaTimeout:
    Type: "Number"
    Description: "The timeout in seconds for the lambda function."
    Default: 10

  LambdaMemorySize:
    Type: "Number"
    Description: "The memory allocation for the lambda function in megabytes."
    Default: 128

  APIName:
    Type: "String"
    Description: "The name of the API."
    Default: "API"

  APIDescription:
    Type: "String"
    Description: "A description of the API."
    Default: ""

  {% for stage in stages %}
  {% if stage != devstage %}
  {{stage|title}}Version:
    Type: "String"
    Description: "The lambda version number associated with the {{stage}} stage."
    Default: "$LATEST"

  {% endif %}
  {% endfor %}
Resources:
  FunctionExecutionRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service:
                - "lambda.amazonaws.com"
            Action: "sts:AssumeRole"
      ManagedPolicyArns:
        - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
      {% if dynamodb_tables %}
      Policies:
        - PolicyName: "DynamoDBPolicy"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action:
                  - "dynamodb:DeleteItem"
                  - "dynamodb:GetItem"
                  - "dynamodb:PutItem"
                  - "dynamodb:Query"
                  - "dynamodb:Scan"
                  - "dynamodb:UpdateItem"
                Resource:
                  {% for stage in stages %}
                  {% for name, table in dynamodb_tables.items() %}
                  - !Join ["", ["arn:aws:dynamodb:", !Ref "AWS::Region", ":", !Ref "AWS::AccountId", ":table/", !Ref "{{stage|title}}{{name|title}}DynamoDBTable"]]
                  {% endfor %}
                  {% endfor %}
      {% endif %}

  Function:
    Type: "AWS::Lambda::Function"
    DependsOn:
      - "FunctionExecutionRole"
    Properties:
      Code:
        S3Bucket: !Ref LambdaS3Bucket
        S3Key: !Ref LambdaS3Key
      Role: !GetAtt ["FunctionExecutionRole", "Arn"]
      Timeout: !Ref LambdaTimeout
      Handler: "handler.lambda_handler"
      Runtime: "python2.7"
      MemorySize: !Ref LambdaMemorySize

  API:
    Type: "AWS::ApiGateway::RestApi"
    Properties:
      Body:
        swagger: "2.0"
        info:
          title: !Ref APIName
          description: !Ref APIDescription
        schemes:
          - "https"
        paths:
          /:
            x-amazon-apigateway-any-method:
              responses: {}
              x-amazon-apigateway-integration:
                responses:
                  default:
                    statusCode: "200"
                uri: !Join ["", ["arn:aws:apigateway:", !Ref "AWS::Region", ":lambda:path/2015-03-31/functions/", !GetAtt ["Function", "Arn"], ":${stageVariables.STAGE}", "/invocations"]]
                passthroughBehavior: "when_no_match"
                httpMethod: "POST"
                type: "aws_proxy"
          /{proxy+}:
            x-amazon-apigateway-any-method:
              parameters:
                - name: "proxy"
                  in: "path"
                  required: true
                  type: "string"
              responses: {}
              x-amazon-apigateway-integration:
                responses:
                  default:
                    statusCode: "200"
                uri: !Join ["", ["arn:aws:apigateway:", !Ref "AWS::Region", ":lambda:path/2015-03-31/functions/", !GetAtt ["Function", "Arn"], ":${stageVariables.STAGE}", "/invocations"]]
                passthroughBehavior: "when_no_match"
                httpMethod: "POST"
                type: "aws_proxy"

  {% set last_table = [] %}
  {% for stage in stages %}
  {{stage|title}}FunctionAlias:
    Type: "AWS::Lambda::Alias"
    Properties:
      Name: {{stage}}
      FunctionName: !Ref Function
      {% if stage != devstage %}
      FunctionVersion: !Ref {{stage|title}}Version
      {% else %}
      FunctionVersion: "$LATEST"
      {% endif %}

  {{stage|title}}APIDeployment:
    Type: "AWS::ApiGateway::Deployment"
    Properties:
      RestApiId: !Ref API
      StageName: {{stage}}
      StageDescription:
        Variables:
          STAGE: "{{stage}}"
          {% for v in vars[stage] %}
          {{v}}: "{{vars[stage][v]}}"
          {% endfor %}

  {{stage|title}}APILambdaPermission:
    Type: "AWS::Lambda::Permission"
    DependsOn:
      - "{{stage|title}}FunctionAlias"
    Properties:
      Action: "lambda:InvokeFunction"
      FunctionName: !Ref {{stage|title}}FunctionAlias
      Principal: "apigateway.amazonaws.com"
      SourceArn: !Join ["", ["arn:aws:execute-api:", !Ref "AWS::Region", ":", !Ref "AWS::AccountId", ":", !Ref "API", "/*/*/*"]]

  {% for name, table in dynamodb_tables.items() %}
  {{stage|title}}{{name|title}}DynamoDBTable:
    Type: "AWS::DynamoDB::Table"
    {% if last_table %}
    DependsOn: "{{last_table[-1]}}"
    {% endif %}
    {% set _ = last_table.append(stage.title() + name.title() + "DynamoDBTable") %}
    Properties:
      TableName: "{{stage}}.{{name}}"
      AttributeDefinitions:
        {% for attr, type in table.attributes.items() %}
        - AttributeName: "{{attr}}"
          AttributeType: "{{type}}"
        {% endfor %}
      KeySchema:
        {% if table.key is iterable and table.key is not string %}
        {% for key in table.key %}
        - AttributeName: "{{key}}"
          KeyType: "{% if loop.index == 1 %}HASH{% else %}RANGE{% endif %}"
        {% endfor %}
        {% else %}
        - AttributeName: "{{table.key}}"
          KeyType: "HASH"
        {% endif %}
      ProvisionedThroughput:
        {% if table.provisioned_throughput %}
        ReadCapacityUnits: {{table.provisioned_throughput[0]}}
        WriteCapacityUnits: {{table.provisioned_throughput[1]}}
        {% else %}
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
        {% endif %}
{'fraction_non_alphanumeric': 0.14029077540106952, 'fraction_numerical': 0.011447192513368985, 'mean_word_length': 2.0331981753674606, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7522409', 'n_tokens_mistral': 3812, 'n_tokens_neox': 3494, 'n_words': 783}