# This is the SAM template that represents the architecture of your serverless application
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html

# The AWSTemplateFormatVersion identifies the capabilities of the template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
AWSTemplateFormatVersion: 2010-09-09
Description: >-
  {{cookiecutter.project_name}}

# Transform section specifies one or more macros that AWS CloudFormation uses to process your template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html
Transform:
- AWS::Serverless-2016-10-31

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3
    MemorySize: {{ cookiecutter.options[cookiecutter.runtime].memory_size }}
    Runtime: {{ cookiecutter.runtime }}
    {%- if cookiecutter.architectures.value != []%}
    Architectures:
    {%- for arch in cookiecutter.architectures.value %}
      - {{arch}}
    {%- endfor %}
    {%- endif %}
    Environment:
      Variables:
        # Make table name accessible as environment variable from function code during execution
        SAMPLE_TABLE: !Ref SampleTable

# Resources declares the AWS resources that you want to include in the stack
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Resources:
  # Each Lambda function is defined by properties:
  # https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction

  GetAllItemsFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/get_all_items.handler
      Description: A simple example includes a HTTP get method to get all items from a DynamoDB table.
      Policies:
        # Give Read Permissions to the SampleTable
        - DynamoDBReadPolicy:
            TableName: !Ref SampleTable
      Events:
        Api:
          Type: Api
          Properties:
            Path: /
            Method: GET

  CreateItemFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/create_item.handler
      Description: A simple example includes a HTTP post method to add one item to a DynamoDB table.
      Policies:
        # Give Create/Read/Update/Delete Permissions to the SampleTable
        - DynamoDBCrudPolicy:
            TableName: !Ref SampleTable
      Events:
        Api:
          Type: Api
          Properties:
            Path: /
            Method: POST

  GetItemByIdFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/get_item_by_id.handler
      Description: A simple example includes a HTTP get method to get one item by id from a DynamoDB table.
      Policies:
        # Give Read Permissions to the SampleTable
        - DynamoDBReadPolicy:
            TableName: !Ref SampleTable
      Events:
        Api:
          Type: Api
          Properties:
            Path: /{id}
            Method: GET

  DeleteItemFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/delete_item.handler
      Description: A simple example includes a HTTP delete method to delete one item by id from a DynamoDB table.
      Policies:
        # Give Create/Read/Update/Delete Permissions to the SampleTable
        - DynamoDBCrudPolicy:
            TableName: !Ref SampleTable
      Events:
        Api:
          Type: Api
          Properties:
            Path: /{id}
            Method: DELETE

  UpdateItemFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/update_item.handler
      Description: A simple example includes a HTTP put method to update one item by id from a DynamoDB table.
      Policies:
        # Give Create/Read/Update/Delete Permissions to the SampleTable
        - DynamoDBCrudPolicy:
            TableName: !Ref SampleTable
      Events:
        Api:
          Type: Api
          Properties:
            Path: /{id}
            Method: PUT

  # Simple syntax to create a DynamoDB table with a single attribute primary key, more in
  # https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlesssimpletable

  # DynamoDB table to store item: {id: &lt;ID&gt;, name: &lt;NAME&gt;}
  SampleTable:
    Type: AWS::Serverless::SimpleTable
    Properties:
      PrimaryKey:
        Name: id
        Type: String
      ProvisionedThroughput:
        ReadCapacityUnits: 2
        WriteCapacityUnits: 2

Outputs:
  WebEndpoint:
    Description: "API Gateway endpoint URL for Prod stage"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
