AWSTemplateFormatVersion: '2010-09-09'
Description: >-
  The storage and messaging layer used by the earlier tutorials, declared once
  instead of created by hand. Deploy it twice with different EnvName values to
  get two independent copies.

Parameters:
  EnvName:
    Type: String
    Default: dev
    AllowedValues: [dev, staging, prod]
    Description: Name every resource after this, so environments cannot collide.

Resources:
  # Receipts and exports. Tutorial 01.
  ReceiptsBucket:
    Type: AWS::S3::Bucket
    Properties:
      # !Sub substitutes parameters into a string. This is what makes one
      # template reusable across environments.
      BucketName: !Sub "orders-${EnvName}-receipts"

  # The orders themselves. Tutorial 02 and 05.
  OrdersTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Sub "orders-${EnvName}"
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST

  # Work waiting to be processed. Tutorial 04.
  OrderQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub "orders-${EnvName}-queue"

Outputs:
  # !Ref on most resources returns its name. Outputs are how one stack hands
  # values to a human, or to another stack.
  BucketName:
    Description: Where receipts are written
    Value: !Ref ReceiptsBucket

  # !GetAtt reaches into a resource for an attribute that is not its name.
  TableArn:
    Description: ARN of the orders table
    Value: !GetAtt OrdersTable.Arn

  QueueName:
    Description: Queue holding unprocessed orders
    Value: !Ref OrderQueue
